我有以下代码:
#!/usr/bin/expect
set timeout 20
set port "23"
set user "admin"
set password "1234"
set ip_file "devices"
set fid [open $ip_file r]
while {[gets $fid ip] != -1} {
spawn telnet $ip $port
expect "'^]'." sleep .1;
send "\r";
sleep .1;
expect "login:"
send "$user\r"
expect "Password:"
send "$password\r"
expect "#\r"
send "conf\r"
send "username root privilege 15 password toor\r"
send "no username admin\r"
send "end\r"
send "wr\r"
send "y\r"
send "exit\r"
expect eof
}
close $fid
如果密码不正确,我需要知道如何从“设备”跳到下一个IP。还要在执行脚本的本地文件上写入密码不正确的设备的IP。
谢谢!
答案 0 :(得分:0)
好的,在这里您可以更高级地使用expect
命令:您向它传递了一个模式/动作对的 list :未经测试
expect "Password:"
send "$password\r"
expect {
"login:" {
# bad password: hit Ctrl-C and move on to the next IP
send \x03
expect eof
continue
}
"#\r"
}
# we've seen the prompt so you can carry on with the rest of your script
在这种情况下,expect将根据其可以找到的 first 模式进行分支。