初探expect誤解重重

服務器多了想研究下交互

sshpass能推送密碼。但是靈活性不行

expect就是首選了

匆匆的看了下就開搞

例子:一

spawn ssh -p $port $user@$ipaddr
expect {
"yes/no)?\ " {send "yes\r";exp_continue}

"*assword:\ " {send "$guest_passwd\r";exp_continue}
"*]$\ " {send "su - root\r";exp_continue}
"*assword:\ " {send "$root_passwd\r";exp_continue}
"*]#\ " {send "server snmpd restart\r"}
"*]#\ " {send "exit\r"}
"*]#\ " {send "exit\r"}
}

以爲就是匹配在推送匹配在推送

後來感覺測試不能實現能登陸一般用戶。su - 之後提示密碼錯誤,開始以爲是密碼錯誤。測試了好久後發現還是太年輕了,不能一知半解的去測試東西。

expect我理解爲相當於一個函數,如果能把所有的匹配都放在一個expect裏面 如果有類似的就會不聽的去匹配,相當於一個死循環,後來分多個expect寫就正常了

例子:二

spawn ssh -p $port $user@$ipaddr
expect {
"yes/no)?\ " {send "yes\r";exp_continue}
"*assword:\ " {send "$guest_passwd\r";exp_continue}
}
expect {
"*]$\ " {send "su - root\r";exp_continue}
"*ssword:\ " {send "$root_passwd\r";exp_continue}
}
expect {
"*]#\ " {send "server snmpd restart\r"}
}
expect {
"*]#\ " {send "exit\r"}
"*]#\ " {send "exit\r"}
}
expect eof
~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章