node.js - 远程运行 bash 命令并模拟击键
问题描述
在 NodeJS 中,我可以生成一个进程并使用spawn().stdout
. 我正在尝试创建一个将与这个 shell 交互的在线终端。我不知道如何将击键发送到 shell 进程。
我试过这个:
echo -e "ls\n" > /proc/{bash pid}/fd/0
除了输出 ls 和换行符之外,这实际上并没有做任何事情。当我尝试这样做时tail -f /proc/{bash pid}/fd/0
,我什至无法将击键发送到打开的bash
终端。
我真的只是在试图了解该bash
过程将如何解释 ENTER 键。我不知道这是否通过标准输入完成,因为换行符不起作用。
解决方案
I don't believe you can "remote control" an already started normal Bash session in any meaningful way. What you can do is start a new shell which reads from a named pipe; you can then write to that pipe to run commands:
$ cd "$(mktemp --directory)"
$ mkfifo pipe1
$ bash < pipe1 &
$ echo touch foo > pipe1
[1]+ Done bash < pipe1
$ ls
foo pipe1
See How to write several times to a fifo without having to reopen it? for more details.
推荐阅读
- reactjs - 如何在反应js中将引导按钮替换为汉堡包按钮
- spring-data-jpa - JPA SQL 查询不等于
- java - 有没有办法将 csv 文件读入 JAVA 中特定索引的数组中?
- android - 当同一应用程序在后台运行定位服务时,Firebase Realtime onDisconnect 不起作用(location.enableBackgroundMode(enable:true);)
- javascript - 我如何为回调或下面显示的 .where() 函数设置条件?
- php - Yii2 未知模型映射键
- tensorflow - 如何使用 distilbert 模型预测测试序列?
- web-scraping - Scrape BSCScan 代币持有页面
- c - 反转偶数的子列表
- c++ - 如何使用 Apache Arrow 做“a + b + c*5 + d*3”?