#!/usr/bin/env expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set bin [lindex $argv 3]
set cfck [lindex [file split $bin] end]
set port [lindex $argv 4]
# StrictHostKeyChecking
set ssh_opt "-o UserKnownHostsFile=/dev/null"
log_user 0
set timeout 10
spawn scp $ssh_opt -P $port $bin $user@$ip:/tmp/$cfck
expect {
"yes/no" { send "yes\r"; exp_continue }
"*?assword" { send "$password\r" }
"No route to host" { send_error "無(wú)法連接\n"; exit 255 }
timeout { send_error "scp 連接超時(shí)\n"; exit 2 }
}
expect {
-re "assword: *$" { send_error "密碼錯(cuò)誤\n"; exit 255 }
"100%"
}
set timeout 60
spawn -noecho ssh $ssh_opt -p $port $user@$ip "/tmp/kk$cfck;code=\$?;rm -f /tmp/$cfck;exit \$code"
log_user 0
expect {
"yes/no" { send "yes\r"; exp_continue }
"*?assword" { send "$password\r" }
"No route to host" { send_error "無(wú)法連接\n"; exit 255 }
timeout { send_error "ssh 連接超時(shí)\n"; exit 2 }
}
log_user 1
expect {
-re "assword: *$" { send_error "密碼錯(cuò)誤\n"; exit 255 }
eof {
set result [wait]
set code [lindex $result end]
# catch wait result
if {$code != 0} {
send_error "code=$code; err=[string range $expect_out(buffer) 4 end]"
exit $code
}
}
}
expect <<EOF
set passwds {foo bar baz}
set i 0
spawn ssh -t root@$server_address "$*"
expect {
"continue connecting (yes/no)?" { send "yes\r"; exp_continue }
" password: " { send "[lindex $passwds $i]\r"; incr i; exp_continue }
eof
}
EOF
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者