Executing String Sent from One Terminal in Another in Linux Pseudo-Terminal

Executing string sent from one terminal in another in Linux pseudo-terminal

No; terminals don't execute commands. They're just channels for data.

You can sort of run a command and attach it to another terminal like this, though:

ls </dev/pts/2 >/dev/pts/2 2>/dev/pts/2

It won't behave exactly like you ran it from that terminal, though, as it won't have that device set as its controlling terminal. It's reasonably close, though.

Pseudo-terminal will not be allocated because stdin is not a terminal

Try ssh -t -t(or ssh -tt for short) to force pseudo-tty allocation even if stdin isn't a terminal.

See also: Terminating SSH session executed by bash script

From ssh manpage:

-T      Disable pseudo-tty allocation.

-t Force pseudo-tty allocation. This can be used to execute arbitrary
screen-based programs on a remote machine, which can be very useful,
e.g. when implementing menu services. Multiple -t options force tty
allocation, even if ssh has no local tty.

Giving a command in a embedded terminal

You can interact with a shell by writing in the pseudo-terminal slave child. Here is a demo of how it could works. This answer is somewhat based on an answer to Linux pseudo-terminals: executing string sent from one terminal in another.

The point is to get the pseudo-terminal used by xterm (through tty command) and redirect output and input of your method to this pseudo-terminal file. For instance ls < /dev/pts/1 > /dev/pts/1 2> /dev/pts/1

Note that

  1. xterm child processed are leaked (the use of os.system is not recommended, especially for & instructions. See suprocess module).
  2. it may not be possible to find programmatically which tty is used
  3. each commands are executed in a new suprocess (only input and output is displayed), so state modification command such as cd have no effect, as well as context of the xterm (cd in the xterm)

from Tkinter import *
from os import system as cmd

root = Tk()
termf = Frame(root, height=700, width=1000)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()

f=Frame(root)
Label(f,text="/dev/pts/").pack(side=LEFT)
tty_index = Entry(f, width=3)
tty_index.insert(0, "1")
tty_index.pack(side=LEFT)
Label(f,text="Command:").pack(side=LEFT)
e = Entry(f)
e.insert(0, "ls -l")
e.pack(side=LEFT,fill=X,expand=1)

def send_entry_to_terminal(*args):
"""*args needed since callback may be called from no arg (button)
or one arg (entry)
"""
command=e.get()
tty="/dev/pts/%s" % tty_index.get()
cmd("%s <%s >%s 2> %s" % (command,tty,tty,tty))

e.bind("<Return>",send_entry_to_terminal)
b = Button(f,text="Send", command=send_entry_to_terminal)
b.pack(side=LEFT)
f.pack(fill=X, expand=1)

cmd('xterm -into %d -geometry 160x50 -sb -e "tty; sh" &' % wid)

root.mainloop()


Related Topics



Leave a reply



Submit