【问题标题】:expect telnet to multiple cisco devices and execute show run期望 telnet 到多个 cisco 设备并执行 show run
【发布时间】:2015-10-28 14:08:34
【问题描述】:

我有以下脚本,它们应该从文件device-list.txt 中提取 IP 地址,然后 telnet 到设备,登录并执行显示运行。该脚本成功登录到设备,但之后给了我一个错误。任何想法我哪里出错了?

for device in `cat device-list.txt`; do ./test4 $device;done

cat test4
#!/usr/bin/expect -f


set hostname [lindex $argv 0]

set user myusername

set pass mypassword

set timeout 10


log_file -a ~/results.log


send_user "\n"

send_user ">>>>>  Working on $hostname @ [exec date] <<<<<\n"

send_user "\n"


spawn telnet $hostname

expect "Username:"

send "$user\n"

expect "Password:"

send "$pass\n"

expect "#"

send “term len 0\n”

send “show running-config\n”

expect “end\r”

send “\n”

send “exit\n”

User Access Verification

Username: myusername
Password:

ROUTER#usage: send [args] string

    while executing

"send “term len 0\n”"

    (file "./test4" line 26)

【问题讨论】:

    标签: expect


    【解决方案1】:

    您的问题是由于不正确的双引号引起的。你必须使用双引号",而不是智能引号“

    send “term len 0\n” ; # Wrong 
    send "term len 0\r";  # Correct
    

    注意:

    基本上,Expect 将使用两个可行的命令,例如 send 和 expect。如果使用send,那么之后必须使用expect(在大多数情况下)。 (反之亦然不是强制性的)

    这是因为如果不这样做,我们将错过生成过程中发生的事情,因为Expect 将假定您只需要发送一个字符串值,而不期待会话中的任何其他内容。

    因此,我们建议在每个send 之后使用expect,以确保我们的命令实际发送到生成的进程。 (发送term len 0\r等一些命令后,您还没有使用expect)。另外,请使用\r 而不是\n。

    【讨论】:

    • 谢谢迪内什!我不知道那些大括号是如何到达那里的。我还有一个关于剧本的问题。我将如何配置,以便在脚本运行时手动输入用户名和密码,而不是将它们保存在实际脚本中?
    猜你喜欢
    • 2013-05-13
    • 2011-10-31
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多