Linux: How To Use Expect

Here’s a simple expect example:

#!/usr/bin/expect -f
spawn some-script-requiring-input.sh
expect "Enter passphrase:"
send ThisIsMyPassword\n
interact

Another example using variables:

#!/usr/bin/expect -f
set myhost MyCrazyBox
spawn ssh $myhost "ls"
expect $myhost
send "MyPassword"
interact

Another example installing a printer:

#!/usr/bin/expect -f
spawn hp-setup -i -a -x -d hp:/net/hp_color_LaserJet_3500?zc=NPIF944C5
expect "Enter option"
send \n
expect "Do you accept the license terms"
send \n
expect eof

The “eof” is required to end the script cleanly.


This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>