------------------------------------------------------------------------------- Injecting text into a terminals input stream That is a script puts something onto the command line when it exits. For example, a command user can edit, before pressing return, or it can do the return itself. This is different to a 'paste attack', where a block of text that is pasted runs a command that is hidden in the text. ------------------------------------------------------------------------------- From... https://unix.stackexchange.com/questions/213799 In X windows you can do this... xdotool type "ls -l" For normal terminals you can use TIOCSTI injection (TIOCSTI : "Terminal IOCtl Send Terminal Input") perl -e 'require "sys/ioctl.ph"; ioctl(STDIN, &TIOCSTI, $_); for split "", join " ", @ARGV' ls -l or perl -e 'ioctl(STDIN, 0x5412, $_) for split "", join " ", @ARGV' to generate the "ioctl.ph" file use (cd /usr/include; h2ph -r -l .) You can also use @mike's Terminology approach https://unix.stackexchange.com/a/213805/22565 this sends the terminal a "query status report" escape sequence which the shell then replaces using a input macro... bind '"\e[0n": "echo test"' printf '\e[5n' GNU screen's "stuff" command can also do it screen -X stuff 'echo test' Using Tmux tmux send-key -t session:pane ls NOTE: All but the ioctl method also echos to the screen if done before the bash "readline" takes over terminal input. But a script can turn off echo, and wait for the input to be available, then re-enabling and exiting. In bash script this is done using "read -t0" bind '"\e[0n": "echo test"' saved_settings=$(stty -g) stty -echo -icanon min 1 time 0 printf '\e[5n' until read -t0; do sleep 0.02 done stty "$saved_settings" ------------------------------------------------------------------------------- Inject into another terminal you have access to! See https://unix.stackexchange.com/questions/213799 inject --tty /dev/pts/22 -- ls -lrt without the --tty option it defaults to current terminal The package "console-tools" has a "writevt" command that does this. ------------------------------------------------------------------------------- Psuedo terms This creates a pty running its own bash in such a way that it can inject a string into its own input stream. =======8<-------- /* Create a psuedo terminal that is unlocked... compile as gcc -o pts pts.c Example use... exec 3<>/dev/ptmx (setsid -c bash -i 2>&1 | tee log) <>"$(./pts <&3)" 3>&- >&0 & echo 'some input' >&3 # write to subshell cat <&3 # read results from subshell */ #include int main(int argc, char *argv[]) { if(unlockpt(0)) return 2; char *ptsname(int fd); printf("%s\n",ptsname(0)); return argc - 1; } =======8<-------- All in one... https://github.com/johnlane/random-toolbox/blob/master/examples/bash-injectable -------------------------------------------------------------------------------