初探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
~


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