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 "ls
":
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)
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…