#! /usr/bin/python import termios import fcntl import sys if __name__ != '__main__': exit if len(sys.argv) != 2: print("Usage: echo XYZ | echo2tty.py /dev/ttyXXX") exit(1) try: data = sys.stdin.read() except: print("NO stdin!\necho2tty.py is a filter:\nit reads from stdin and sends the data to the specified terminal.") exit(2) with open(sys.argv[1], 'w') as fd: # # Works for our own terminal (/proc/self/fd/0) # # But FAILS for any other tty, even owned by us (the same user id) # # ("echo 'xxx' > '/dev/pts/2'" does send the data to the device # # although shell process on that terminal ignores it). # # They say ioctl(TIOCSTI) requires root privileges # # (or CAP_SYS_ADMIN to be more specific, # # but that's usually the same in practice) # # BTW, the capabilities(7) man page does NOT mention this fact! for c in data: fcntl.ioctl(fd, termios.TIOCSTI, c) # end for # end with