linux - 将命令从一个终端写入另一个终端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10376251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux - write commands from one terminal to another
提问by user1364700
I need to write commands from one terminal to another terminal.
我需要将命令从一个终端写入另一个终端。
I tried these:
我试过这些:
echo -e "ls\n" > /proc/pid/fd/0
echo -e "ls\n" > /dev/pts/4
Which just prints the ls
as output and doesn't execute.
它只是打印ls
as 输出而不执行。
I tried these:
我试过这些:
chmod 777 /dev/tty4 ;echo "ls" > /dev/tty4
chmod 777 /dev/tty40 ;echo "ls" > /dev/tty40
Which don't seem to do anything
似乎没有做任何事情
Any ideas?
有任何想法吗?
[note that I don't want to touch the second terminal to accomplish this. only the first one]
[请注意,我不想触摸第二个终端来完成此操作。只有第一个]
回答by Chris Stratton
This is the wrong way to go about it - you might get it displayed in the terminal, but not executed.
这是错误的处理方式 - 您可能会在终端中显示它,但不会执行。
You will need to do something like tell a shell to read from a named pipe, or from netcat/socat. Or you could try injecting keystrokes as root or using xtest (there's also sometimes another way under X which I forget).
您需要执行一些操作,例如告诉 shell 从命名管道或 netcat/socat 读取。或者您可以尝试以 root 身份或使用 xtest 注入击键(有时在 X 下还有另一种方式,我忘记了)。
回答by Andy Ross
This is hairy. The stdin file in proc you're trying to use is a symlink to the terminal device (probably /dev/pts/something). There are twoprocesses that have that device open: the shell (your target) and the terminal emulator (e.g. gnome-terminal), and they use it like a socket to pass data in both directions. Presumably the latter is stealing the output and displaying it, so the shell never sees it. I don't think this technique will work.
这是毛茸茸的。您尝试使用的 proc 中的 stdin 文件是终端设备的符号链接(可能是 /dev/pts/something)。有两个进程可以打开该设备:shell(您的目标)和终端仿真器(例如 gnome-terminal),它们像套接字一样使用它在两个方向上传递数据。据推测,后者正在窃取输出并显示它,因此外壳永远不会看到它。我认为这种技术行不通。
What are you trying to accomplish? You can't run the process as a child using conventional tools like popen()? If it's a GUI terminal emulator, you could try to forge keyboard input via X events or the kernel's uinput API.
你想达到什么目的?您不能使用像 popen() 这样的传统工具在孩提时代运行这个过程吗?如果它是一个 GUI 终端模拟器,您可以尝试通过 X 事件或内核的 uinput API 伪造键盘输入。
回答by Voislav Sauca
Is posible to show the output of a command on multiple terminals simultaneously with the following script., and it works with all console programs, including the editors. For example doing:
可以使用以下脚本在多个终端上同时显示命令的输出。它适用于所有控制台程序,包括编辑器。例如做:
execmon.bash 'nano hello.txt' 5
Open an editor and both the output and the text that we introduce will be redirected to the virtual terminal number 5. You can see your terminals:
打开编辑器,我们引入的输出和文本都将重定向到虚拟终端号 5。您可以看到您的终端:
ls /dev/pts
Each virtual terminal has an associated number.
每个虚拟终端都有一个相关联的号码。
Is work with the normal terminal, konsole and xterm, just create the file execmon.bash and put this:
使用普通终端,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
Example:
例子:
execmon.bash 'ls-l' 5
execmon.bash 'nano Hello.txt' 5
回答by esoriano
Python code:
蟒蛇代码:
#!/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)
回答by ???? ??? ????
open 2 terminals then type ttd on the terminal which you want to write on ttd will show you the address of the terminal move to the another terminal and type cat > (address of the 2nd terminal) and hit enter
打开 2 个终端,然后在你想写在 ttd 上的终端上输入 ttd 将显示终端的地址移动到另一个终端并输入 cat >(第二个终端的地址)并按回车
回答by Manith Shetty
command > dev/pts/# where # is the other terminal's name
命令 > dev/pts/# 其中 # 是另一个终端的名称
回答by Robert
look at:
看着:
man 1 script
for example:
例如:
script -f /dev/tty1