我需要将命令从一个终端写入另一个终端。
我试过这些:
echo -e "ls\n" > /proc/pid/fd/0
echo -e "ls\n" > /dev/pts/4
这只是印刷 ls
作为输出而不执行。
我试过这些:
chmod 777 /dev/tty4 ;echo "ls" > /dev/tty4
chmod 777 /dev/tty40 ;echo "ls" > /dev/tty40
哪个似乎什么也没做
有任何想法吗?
[请注意,我不想触摸第二个终端来完成此任务。只有第一个]
Python代码:
#!/usr/bin/python
import sys,os,fcntl,termios
if len(sys.argv) != 3:
sys.stderr.write("usage: ttyexec.py tty command\n")
sys.exit(1)
fd = os.open("/dev/" + sys.argv[1], os.O_RDWR)
cmd=sys.argv[2]
for i in range(len(cmd)):
fcntl.ioctl(fd, termios.TIOCSTI, cmd[i])
fcntl.ioctl(fd, termios.TIOCSTI, '\n')
os.close(fd)
可以使用以下脚本同时显示多个终端上的命令输出。它适用于所有控制台程序,包括编辑器。例如:
execmon.bash 'nano hello.txt' 5
打开一个编辑器,我们引入的输出和文本将被重定向到虚拟终端号5.你可以看到你的终端:
ls /dev/pts
每个虚拟终端都有一个相关的号码。
使用普通终端,konsole和xterm,只需创建execmon.bash文件并将其放入:
#! / bin / bash
# execmon.bash
# Script to run a command in a terminal and display the output
# in the terminal used and an additional one.
param = $ #
if [$ param-eq 2]; Then
echo $ 1 | tee a.out a.out && cat> / dev / pts / $ 2 && exec `cat` a.out | tee / dev / pts / $ 2 && rm a.out
else
echo "Usage:"
echo "execmon 'command' num '
echo "-command is the command to run (you have to enter ')"
echo "-num is the number of virtual console to output to the"
fi
例:
execmon.bash 'ls-l' 5
execmon.bash 'nano Hello.txt' 5
这很毛茸茸。您尝试使用的proc中的stdin文件是终端设备的符号链接(可能是/ dev / pts / something)。有 二 打开该设备的进程:shell(您的目标)和终端仿真器(例如gnome-terminal),它们像套接字一样使用它来向两个方向传递数据。据推测,后者正在窃取输出并显示它,因此shell永远不会看到它。我不认为这种技术会起作用。
你想达到什么目的?您不能使用像popen()这样的传统工具作为孩子运行该过程?如果它是GUI终端仿真器,您可以尝试通过X事件或内核的uinput API来伪造键盘输入。
打开2个终端
然后在要写入的终端上键入ttd
ttd将显示终端的地址
移动到另一个终端并输入cat>(第二个终端的地址)
并按Enter键
这是错误的方法 - 你可能会把它显示在
终端,但没有执行。
你需要做一些事情,比如告诉shell从命名管道或netcat / socat读取。或者您可以尝试以root身份或使用xtest注入击键(在X下我有时会忘记另一种方式)。
命令> dev / pts /#
其中#是另一个终端的名称