close
需求: 使用scp下載資料,幫使用者自動輸入yes之後再輸入passward,或是直接輸入password
#!/usr/bin/expect
set ip [lindex $argv 0]
set pass "password"
spawn scp root@$ip:/folder/filename .
expect {
"(yes/no)?"
{
send "yes\r"
expect {
password: {send "$pass\r"; exp_continue}
}
}
timeout {
exit 1
}
password: {send "$pass\r"; exp_continue}
}
interact
----------------------------------------------------------------------
需求: ssh登入,輸入使用者名稱以及密碼
#!/usr/bin/expect
set timeout 10
set ip [lindex $argv 0]
set user username
set password password
spawn ssh -o StrictHostKeyChecking=no "$user\@$ip"
expect {
"assword:" {send "$password\r"}
timeout {
exit 1
}
}
interact
----------------------------------------------------------------------
需求: 如果帳號密碼多組不同且有些帳號不需要密碼
#!/usr/bin/expect
set passwords { bad1 bad2 bad3 good }
spawn ssh -o PreferredAuthentications=keyboard-interactive \
-o NumberOfPasswordPrompts=[llength $passwords] root@tree
set try 0
expect {
"Password: " {
if { $try >= [llength $passwords] } {
send_error ">>> wrong passwords\n"
exit 1
}
send [lindex $passwords $try]\r
incr try
exp_continue
}
"bash-4.2" {
interact
}
timeout {
send_error ">>> timed out\n"
exit 1
}
}
全站熱搜
留言列表