Thursday, February 26, 2009

expect scripting

Nice way to start expect would be to first visit: http://expect.nist.gov/

Expect is mainly used to automate the task for some applications which require manual interaction such as telnet, ftp, passwd, fsck, rlogin, tip, etc.

Enough of the introduction part for expect.


Example(1): will telnet to a host named as `nido` it'll put login and password credentials and then will surely can run either of the commands ( I know, one command will fail)...

_______________________________________________

#!/usr/local/bin/expect
spawn telnet nido
expect "login"
send "root\r\n"
expect "Password:"
send "whoami\r\n"
expect -re {\#}
send "/usr/contrib/bin/machinfo| mailx parwatraj@yahoo.com/r"
expect -re {\#}
send "ls -l\r"
expect -re {\#}
send "/opt/ignite/bin/print_manifest| mailx parwatraj@yahoo.com/r"
expect -re {\#}
exit
_______________________________________________________
Example(2): will telnet or ssh to a list of hosts which are in a text file `/script/testpnhost.txt` and will do the same task as we've seen on the above example.
Excute testpn4 and see how it works for you.
Modify the contents of expect script as per your need.

---------------------------------------
# cat testpn4
#!/bin/sh
for A in `cat /script/testpnhost.txt`
do
echo ${A}
echo "__________________________"
/script/testpn5.exp ${A}
done
---------------------------------------------------------------

# cat testpn5.exp
#!/usr/local/bin/expect
spawn telnet [lindex $argv 0]
expect "login"
send "root\r\n"
expect "Password:"
send "whoami\r\n"
expect -re {\#}
send "/usr/contrib/bin/machinfo| mailx parwatraj@yahoo.com/r"
expect -re {\#}
send "/opt/ignite/bin/print_manifest| mailx parwatraj@yahoo.com/r"
expect -re {\#}
exit
---------------------------------------------------------------------

No comments:

Post a Comment