Chinaunix
标题:
[求助]except 远程到cisco设备上如何使用for循环?
[打印本页]
作者:
钟大發
时间:
2013-11-28 14:00
标题:
[求助]except 远程到cisco设备上如何使用for循环?
最近因为有些事情,需要在半夜进行cisco网络设备的大包ping测试,于是我在linux上写了个except的登录脚本进行测试,
脚本如下:
ping.exp:
#! /usr/bin/expect
set ip [lindex $argv 0]
set passwd [lindex $argv 1]
set enpasswd [lindex $argv 2]
set timeout 60
spawn telnet $ip
expect "Password:"
send "$passwd\r"
expect ">"
send "en\r"
expect "Password:"
send "$enpasswd\r"
expect "#"
send "ping 10.35.195.1 source 10.35.195.245 size 1500 repeat 100\r"
expect "#"
send "exit\r"
expect eof
save.sh:
#! /bin/sh
a=`cat /tmp/ip.txt`
for ip in $a;
do
/tmp/ping.exp $ip passwd passwd;
done
ip.txt
10.35.195.245
10.35.195.246
当前能够实现的就是从我的linux服务器,通过ip.txt上的IP去登陆进行ping测,而后我会>>到某个log文件中,但是问题是我登陆的每台cisco网络设备都不一样,即ping的地址都是不一样的,
即exp脚本中我如何能将
send "ping 10.35.195.1 source 10.35.195.245 size 1500 repeat 100\r"
这行的目的地址和原地址用变量替代掉?类似shell中的for in的循环那样。expect不是太懂,求大师们点化。。。
多谢多谢~~~~
作者:
HH106
时间:
2013-11-28 14:11
回复
1#
钟大發
ping.exp加入
set dip [lindex $argv 3]
set sip [lindex $argv 4]
send "ping 10.35.195.1 source 10.35.195.245 size 1500 repeat 100\r"
==>
send "ping $dip source $sip size 1500 repeat 100\r"
复制代码
作者:
钟大發
时间:
2013-11-28 15:13
谢谢提醒,我之前想过这样做,然后将ip.txt文件进行修改,比如这样:
ping.exp
#! /usr/bin/expect
set ip [lindex $argv 0]
set passwd [lindex $argv 1]
set enpasswd [lindex $argv 2]
set dip [lindex $argv 3]
set sip [lindex $argv 4]
set timeout 60
spawn telnet $ip
expect "Password:"
send "$passwd\r"
expect ">"
send "en\r"
expect "Password:"
send "$enpasswd\r"
expect "#"
send "ping $dip source $sip size 1500 repeat 100\r"
expect "#"
send "exit\r"
expect eof
save.sh:
#! /bin/sh
a=`cat /tmp/ip.txt`
for par in $a;
do
/tmp/ping.exp $par;
done
IP.txt
10.35.195.245 netstar01 netstar01 10.35.195.1 10.35.195.245
但是运行的时候到输入密码那部就卡住了。。sh-x也看不出什么。不知道有什么问题吗。。。。
回复
2#
HH106
作者:
HH106
时间:
2013-11-28 15:22
回复
3#
钟大發
for par in $a
==> for par in "$a"
复制代码
作者:
钟大發
时间:
2013-11-28 15:28
。。竟然忘记这点了。。多谢大师点化!给你32个赞!!!
回复
4#
HH106
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2