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.

JavaScript

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:

JavaScript

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