Skip to content
Advertisement

Unable to fake terminal input with termios.TIOCSTI

Most of the code samples I’ve seen are trying to read from stdin without local echo. To do this they modify the “local modes” flag to remove the setting to “Echo input characters”. I thought I could just modify the “input modes” flag to TIOCSTI which is for “Insert the given byte in the input queue.”. However, even though I run the script as root, it has no effect. anything I write to the fd seems to go to the terminal output, rather than the terminal input. Basically what I want to do is this exact thing, but in pure python.

"""
termfake.py

Usage: sudo python termfake.py /dev/ttys002

Get the tty device path of a different local termimal by running `tty`
in that terminal.
"""

import sys
import termios

fd = open(sys.argv[1], 'w')
fdno = fd.fileno()

# Returns [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]
tatters = termios.tcgetattr(fdno)
print('original', tatters)
tatters[0] = termios.TIOCSTI
print('TIOCSTI', termios.TIOCSTI)

# Set iflag
termios.tcsetattr(fdno, termios.TCSANOW, tatters)

# Verify setting change
with open('/dev/ttys002', 'w') as fd2:
    print('modified', termios.tcgetattr(fd2.fileno()))

fd.write('This is testn')
fd.close()

Advertisement

Answer

TIOCSTI is an ioctl (documented in tty_ioctl(4)), not a terminal setting, so you can’t use tcsetattr() — you need to feed each character of the fake input to ioctl() instead. Never had to do ioctl’s from Python before, but the following seems to work for running an ls in a different terminal (specified as the argument, e.g. /dev/pts/13) that’s running Bash:

import fcntl
import sys
import termios

with open(sys.argv[1], 'w') as fd:
    for c in "lsn":
        fcntl.ioctl(fd, termios.TIOCSTI, c)

TIOCSTI requires root privileges (or CAP_SYS_ADMIN to be more specific, but that’s usually the same in practice) by the way — see capabilities(7).

Advertisement