30

Is there a command that exists that can simulate keypresses? I want to pipe some data to it to make it type into a GUI program for me.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
SpecialBomb
  • 1,928
  • 9
  • 24
  • 36

2 Answers2

51

Yes, it is xdotool.

To simulate a key press, use:

xdotool key <key>

For example, to simulate pressing F2:

xdotool key F2

To simulate pressing crtl + c:

xdotool key ctrl+c

To simulate pressing ctrl + c and then a Backspace:

xdotool key ctrl+c BackSpace

Check man xdotool to get more idea.

You might need to install the xdotool package first to use xdotool command.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
heemayl
  • 54,820
  • 8
  • 124
  • 141
2

Use expect (man expect)

#!/usr/bin/expect
#set timeout 10
set clientName [lindex $argv 0];
set hostName [lindex $argv 1];
set passWord [lindex $argv 2];

spawn ssh "$hostName";
expect "Password:";
send "$passWord\r";
expect "$hostName";

send "cd /apps/bin\r";
expect " bin]";
sam
  • 22,265
  • 4
  • 22
  • 30
sbriet
  • 29
  • 1