2

I have this setup where I have computer with ssh and a display where I have a user logged in to terminal. What I want to do is send commands like I was using that local session with keyboard. I tried to echo to /dev/tty1 but it just shows what I typed instead executing it. Which makes sense. The system only has bash so no GUI or anything like that.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
hene193
  • 23
  • 3
  • 1
    Do you really need to send commands to that session, or would executing them in an independent session (i.e. `ssh remotehost "some command; another command; yet another command"`) be sufficient? – roaima Aug 30 '16 at 23:39

1 Answers1

5

The TIOCSTI ioctl can inject characters into a terminal, or see instead uinput on Linux to generate keyboard (or mouse!) input.

  • ttywrite.c - sample C implementation
  • Term::TtyWrite - Perl implementation

    $ sudo perl -MTerm::TtyWrite \
      -e 'Term::TtyWrite->new("/dev/pts/2")->write("echo hi\n")'
    
thrig
  • 34,333
  • 3
  • 63
  • 84
  • This Q&A is Linux-specific, fortunately. Note that other operating systems started to drop TIOCSTI a year after this answer was written. https://unix.stackexchange.com/q/406690/5132 – JdeBP Feb 24 '20 at 09:19