linux - write commands from one terminal to anothe

2019-02-05 00:01发布

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.

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]

7条回答
一纸荒年 Trace。
2楼-- · 2019-02-05 00:14

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

查看更多
Deceive 欺骗
3楼-- · 2019-02-05 00:15

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)
查看更多
Explosion°爆炸
4楼-- · 2019-02-05 00:23

look at:

man 1 script

for example:

script -f /dev/tty1
查看更多
甜甜的少女心
5楼-- · 2019-02-05 00:32

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:

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:

#! / 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
查看更多
男人必须洒脱
6楼-- · 2019-02-05 00:33

command > dev/pts/# where # is the other terminal's name

查看更多
\"骚年 ilove
7楼-- · 2019-02-05 00:35

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 two processes 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.

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.

查看更多
登录 后发表回答