linux - write commands from one terminal to another

前端 未结 7 1600
刺人心
刺人心 2020-12-29 09:54

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/         


        
相关标签:
7条回答
  • 2020-12-29 10:04

    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.

    0 讨论(0)
  • 2020-12-29 10:06

    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)
    
    0 讨论(0)
  • 2020-12-29 10:07

    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).

    0 讨论(0)
  • 2020-12-29 10:10

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

    0 讨论(0)
  • 2020-12-29 10:13

    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

    0 讨论(0)
  • 2020-12-29 10:15

    look at:

    man 1 script
    

    for example:

    script -f /dev/tty1
    
    0 讨论(0)
提交回复
热议问题