期望telnet到多个cisco设备并且执行show run [英] expect telnet to multiple cisco devices and execute show run

查看:134
本文介绍了期望telnet到多个cisco设备并且执行show run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本,应该从文件device-list.txt中提取IP地址,然后远程登录到设备,登录并执行show run.脚本成功登录到设备,但此后出现错误.有什么想法我会出错吗?

I have the following scripts which is supposed to pull the IP address from a file device-list.txt and then telnet to the device, login and execute a show run. The script logs in to the device successfully, but gives me an error after that. Any ideas where im going wrong?

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)

推荐答案

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

Your problem is due to incorrect double quotes. You have to use double quotes ", not smart quotes "

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

注意:

基本上,Expect将与两个可行的命令一起使用,例如sendexpect.如果使用send,则在以后必须具有expect(在大多数情况下). (反之则不是强制性的)

Basically, Expect will work with two feasible commands such as send and expect. If send is used, then it is mandatory to have expect (in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)

这是因为,如果没有这些内容,我们将错过在生成的过程中发生的事情,因为Expect会假定您只需要发送一个字符串值,而不希望会话中有其他任何内容.

This is because without that we will be missing out what is happening in the spawned process as Expect will assume that you simply need to send one string value and not expecting anything else from the session.

因此,我们建议在每个send之后使用expect,以确保将我们的命令实际发送到生成的进程. (发送诸如term len 0\r之类的某些命令后,您尚未使用expect).另外,使用\r代替\n.

So, we recommend to use expect after each send, to make sure our commands actually sent to the spawned process. (You have not used expect after sending some commands such as term len 0\r). Also, use \r instead of \n.

这篇关于期望telnet到多个cisco设备并且执行show run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆