expect 1

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.

Upload: mukhtar77624

Post on 09-May-2017

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Expect 1

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

Interactive Process Interactive Process AutomationAutomation

Igor BravermanE&M Computing Ltd.

Page 2: Expect 1

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.

Page 3: Expect 1

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 commandline with no arguments.

By default, autoexpect spawns a shell for you. Given a program name and arguments, autoexpect spawns that program.

The Tip: Autoexpect

Page 4: Expect 1

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

Autoexpect execution example:

igor@genesis > autoexpect ssh root@testautoexpect started, file is script.expEnter 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 ~] # logoutConnection to test closed.autoexpect done, file is script.exp

Page 5: Expect 1

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

Resulting Expect script

spawn ssh root@testmatch_max 100000expect -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.\rNew UNIX password: "send -- "New Password\r"expect -exact "\rRetype new UNIX password: "send -- "New Password\r"expect -exact "\rpasswd: all authentication tokens updated successfully.\r\[root@test ~\] # "send -- “logout"expect eof

Page 6: Expect 1

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@$hostmatch_max 100000expect "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

}

Page 7: Expect 1

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

Running the script

igor@genesis (~) > ./script.exp samplePhrase password dwarkinspawn ssh root@dwarkinEnter passphrase for key '/users/igor/.ssh/id_rsa': samplePhrase

Last login: Wed Jan 5 10:03:07 2005 from localhostSun Microsystems Inc. SunOS 5.9 Generic May 2002root@dwarkin (~) # passwd rootNew Password: Re-enter new Password:passwd: password successfully changed for rootroot@dwarkin (~) # exitlogoutConnection to 0 closed.igor@genesis (~) >

Page 8: Expect 1

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

THANK YOUTHANK YOU