Unable to fake terminal input with termios.TIOCSTI

前端 未结 2 642
北恋
北恋 2020-12-15 01:47

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 c

2条回答
  •  渐次进展
    2020-12-15 02:36

    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\n":
            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).

提交回复
热议问题