问题描述
写了段登录交换机的脚本,进行用户名和密码的判断,怎么这么写登录不上去啊正常应该是:telnet 200.2.0.3登录后提示'>',输入'en',提示'Password:'输入密码,如果密码正确则得到提示'#' 用下面代码不能实现正常登录交换机,即时密码修改正确,请问如何修改spawn telnet "200.2.0.3 23"expect { "name:" { send "ar" expect "word:" } ">" { send "enr" expect "word:" } "word" { send "ar" } }expect "#"
解决方案
这个和telnet登录的系统版本有关,可以手动登录确定各阶段的标志符, 下面是SSH登录例子,希望对你有参考价值。#!/usr/bin/expectset timeout 5set username [lindex $argv 0]set hostip [lindex $argv 1]set passwd [lindex $argv 2]set cmdfile [lindex $argv 3]spawn ssh $username@$hostipexpect {"yes/no" {send "yesn"}}expect {"password:" {send "$passwdn"}"Password:" {send "$passwdn"}}set f [open $cmdfile]while {[gets $f line]>=0} {expect {"*#" {send "$linen"}"*$ " {send "$linen"}"*]" {send "$linen"}"*: " {send "$linen"}"*> " {send "$linen"}"*>" {send "$linen"}}}close $fexpect "#"send "quitn"expect eofexit