14

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]

ntzm
  • 4,663
  • 2
  • 31
  • 35
user1364700
  • 161
  • 1
  • 1
  • 4
  • What are you *really* trying to do? It may be your logic for trying this is in the first place is flawed; there might be an easier solution to get the same result. Also, terminals run in separate processes so you'd need some form of [interprocess communication](http://en.wikipedia.org/wiki/Inter-process_communication) to get them to talk to one another. –  Apr 29 '12 at 21:47

7 Answers7

11

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)
esoriano
  • 477
  • 6
  • 6
3

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
Voislav Sauca
  • 3,007
  • 2
  • 18
  • 12
  • ok., and for this reason you also downvote my answer., i just try to help – Voislav Sauca Apr 30 '12 at 00:24
  • 1
    @VoislavSauca I believe the down vote for your answer was because it was not correct. –  Apr 30 '12 at 00:31
  • You aren't running in or able to modify the environment of the other terminal's shell however. So for example the current directory previously shown there might not be the directory in which the command was executed, and a command to change directory would have no effect there. Any answer using a subshell to run commands in the other terminal wouldn't be able to change the environment of its parent, but it would at least inherit it. Question is not precise enough about what is needed. – Chris Stratton Apr 30 '12 at 01:31
  • 1
    first of all thank you for your answer. i don't want to show the output of a command. i want to execute it in the other terminal. thank you for this script though – user1364700 Apr 30 '12 at 23:03
  • @Chris Stratton all i want is to simulate the input, instead of getting it from the keyboard, getting it from another terminal. – user1364700 Apr 30 '12 at 23:08
2

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.

Andy Ross
  • 11,699
  • 1
  • 34
  • 31
  • first of all thank you for your answer. can't i direct the output to the shell then? instead of te terminal emulator? how does it get the commands from te keyboard? cant i simulate it? popen is my less favorite option. if writing the commands to the shell won't work, i will try popen. – user1364700 Apr 30 '12 at 22:48
1

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

1

look at:

man 1 script

for example:

script -f /dev/tty1
Robert
  • 11
  • 1
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Ethaan Jan 24 '15 at 11:08
0

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

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • first of all thank you for your answer. im trying to get the command executed. why i can send stdin to a process but not to a shell?.. and what did you mean with sending keystrokes? i can inject keystrokes to another shell? – user1364700 Apr 30 '12 at 23:02
-1

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