You are on page 1of 8

I g o r B r a v e r m a n

Interactive Process
Interactive Process
Automation
Automation
Igor Braverman
E&M Computing Ltd.
I g o r B r a v e r m a n
What is expect?
Expect is a tool primarily for automating interactive
applications such as telnet, ftp, passwd, fsck, ssh, rlogin,
tip, etc.
Expect really makes this stuff trivial.
Expect is also useful for testing applications.
I g o r B r a v e r m a n
autoexpect is a tool for generating Expect script from watching
a session.
The simplest way to use autoexpect is to call it from the command
line with no arguments.
By default, autoexpect spawns a shell for you. Given a program
name and arguments, autoexpect spawns that program.
The Tip: Autoexpect
I g o r B r a v e r m a n
Autoexpect execution example:
igor@genesis > autoexpect ssh root@test
autoexpect started, file is script.exp
Enter passphrase for key '/home/igor/.ssh/id_rsa':
Last login: Mon Jan 3 14:39:24 2005 from genesis
[root@test ~] #
Changing password for user root.
New UNIX password:
passwd root
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@test ~] # logout
Connection to test closed.
autoexpect done, file is script.exp
I g o r B r a v e r m a n
Resulting Expect script
spawn ssh root@test
match_max 100000
expect -exact "Enter passphrase for key '/home/igor/.ssh/id_rsa': "
send -- "samplePhrase\r"
expect "root@test ~\] # "
send -- "passwd root\r"
expect "Changing password for user root.\r
New UNIX password: "
send -- "New Password\r"
expect -exact "\r
Retype new UNIX password: "
send -- "New Password\r"
expect -exact "\r
passwd: all authentication tokens updated successfully.\r
\[root@test ~\] # "
send -- logout"
expect eof
I g o r B r a v e r m a n
Expect script
set pass_phrase [lrange $argv 0 0]
set new_pass [lrange $argv 1 1]
set argv [lrange $argv 2 end]
while {[llength $argv]>0} {
set host [lrange $argv 0 0]
set argv [lrange $argv 1 end]
spawn ssh root@$host
match_max 100000
expect "Enter passphrase"
send -- $pass_phrase\r"
expect "# "
send -- "passwd root\r"
expect re "New.+word: "
send -- $new_pass\r"
expect re "Re.+word: "
send -- $new_pass\r"
expect re "successfully.+# "
send -- logout"
expect eof
}
I g o r B r a v e r m a n
Running the script
igor@genesis (~) > ./script.exp samplePhrase password dwarkin
spawn ssh root@dwarkin
Enter passphrase for key '/users/igor/.ssh/id_rsa': samplePhrase
Last login: Wed Jan 5 10:03:07 2005 from localhost
Sun Microsystems Inc. SunOS 5.9 Generic May 2002
root@dwarkin (~) # passwd root
New Password:
Re-enter new Password:
passwd: password successfully changed for root
root@dwarkin (~) # exit
logout
Connection to 0 closed.
igor@genesis (~) >
I g o r B r a v e r m a n
THANK YOU
THANK YOU

You might also like