Monday, March 5, 2012

expect - programmed dialogue with interactive programs

Expect  is a program that "talks" to other interactive programs according to a script.  Following  the  script, Expect  knows  what  can  be expected  from  a  program and what the correct response should be. An interpreted language provides branching and high-level  control  structures  to  direct the dialogue.  In addition, the user can take control and interact directly when desired, afterward returning control to  the script.




Example:

#!/usr/bin/expect -f

#expect to ssh
set user <user_name>
set userpass <user_password>
set ipadd <ip_address>

spawn ssh $user@$ipadd;
expect "Password: "
send "$userpass\r"
expect "$user@hostname # "
send "<command> \r"
send "exit \r"
expect eof
 
#!/usr/bin/expect -f

#expect to telnet
set user <user_name>
set userpass <user_password>

spawn telnet <ip_address>
expect "Username:"
send "$user\r"
expect "password:"
send "$userpass\r"
expect "Hostname#"
send "<command> \r"
send "exit\r"
expect eof

No comments:

Post a Comment