Monday, November 17, 2008

expect example

In bash:
#!/bin/bash
expect -c "
spawn ssh root@1.1.1.1
expect password:
send \"1234\r\"
expect ]#
send \"ls -l\r\"
expect ]#
send \"uname -r\r\"
expect -timeout 1
"
Not in bash:
#!/usr/bin/expect
spawn ssh root@1.1.1.1
expect "password: "
send "1234\r"
expect "]# "
send "uname -r\r"
expect -timeout 1

1 comment:

瘋狂帽客 said...

另一個expect的好範例:
#!/usr/bin/expect
if {[llength $argv] != 1} {
puts "usage: tmpclear remotehost"
exit
}
set timeout -1
match_max 100000
spawn ssh $argv
expect "$argv*$"
send -- "rm -rf ~/tmp/*r"
expect "$argv*$"
send -- "logoutr"
expect eof

References:
http://www.serverwatch.com/tutorials/article.php/3883871/Automation-With-Expect-an-Open-Source-Software-Utility.htm