使用expect实现ssh自动交互

#!/usr/bin/expect -f
#auto ssh login
set timeout 30
set sshhost [lindex $argv 0]
set sshport [lindex $argv 1]
set sshusername [lindex $argv 2]
set sshpassword [lindex $argv 3]
set sshsource_directory [lindex $argv 4]
# echo $sshhost
# set sshhost 106.3.226.132
spawn ssh -l$sshusername -p$sshport $sshhost
expect {
        "not know" {send_user "[exec echo \"not know\"]";exit}
        "(yes/no)?" {send "yes\r";exp_continue}
        "password:" {send  "$sshpassword\r"}
        "Permission denied, please try again." {
                send_user "[exec echo \"Error:Password is wrong\"]"
                exit
        }
        expect eof
}
#iexpect "password:"
#send "$sshpassword\r"
expect "]*"
send "cd $sshsource_directory\r"
send "ls |grep csv$|xargs bzip2 -z\r"

#send "tar -cjvf visitlog.2014-07-10_08.csv.tar.bz2 visitlog.2014-07-10_08.csv\r"
send "exit\r"
interact

猜你喜欢

转载自qq85609655.iteye.com/blog/2090402