求助一个用Expect实现的自动交互问题
首先感谢关注本帖的朋友。
在一个项目中需要telnet登录到交换机上执行一个命令,然后抓取该命令的显示结果,
登上交换机之后会显示一个类型BMMU-MGX8850-R01>格式的交换机提示符,然后执行一个dsppncons命令,该命令的显示结果可能分多屏显示,如果当前显示的不是最后一屏就会在最后一行提示Type <CR> to continue, Q<CR> to stop:
此时如按回车则继续显示,显示完所有的信息之后,会重新显示出交换机提示符
目前我的做法是用expect 写一个ant.exp脚本,然后再写一个read.sh,然后在read.sh中调用ant.exp 并且把执行结果重定向到一个文件,达到抓取命令的显示结果的目的,该方法完全可以实现抓取该命令的显示结果的要求。
ant.exp脚本
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn telnet $ip
expect "Login:"
send "$username\r"
expect "password:"
send "$password\r"
sleep 3
expect -re "\[A-Z\]\{4,\}-\[m|g|x|M|G|X\]\{3,\}88\[3|5\]\{1\}0"
send "dsppncons\r"
expect {
"*stop:" {exp_send "\r";exp_continue }
-re "\[A-Z\]\{4,\}-\[m|g|x|M|G|X\]\{3,\}88\[3|5\]\{1\}0" { exp_send "logout\r";exit }
}
read.sh 脚本
#! /bin/bash
./ant.exp $1 $2 $3 >$4
例如执行
./read.sh 192.168.1.30 uname password 30.txt
30.txt的结果如下:
spawn telnet 20.2.8.1
Trying 20.2.8.1...
Connected to 20.2.8.1 (20.2.8.1).
Escape character is '^]'.
Login: tdrxr
password:
BMMU-MGX8850-R01> dsppncons
Port VPI VCI CallRef:Flag X-Port VPI VCI CallRef:Flag Type OAM-Type Pri
11.2 11 120 1:0 1:1.9:9 0 35 1:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 121 2:0 1:1.9:9 0 36 2:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 122 3:0 1:1.9:9 0 37 3:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 123 4:0 1:1.9:9 0 38 4:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 124 5:0 1:1.9:9 0 39 5:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 125 6:0 1:1.9:9 0 40 6:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 126 7:0 1:1.9:9 0 41 7:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
Type <CR> to continue, Q<CR> to stop:
Port VPI VCI CallRef:Flag X-Port VPI VCI CallRef:Flag Type OAM-Type Pri
11.2 11 120 1:0 1:1.9:9 0 35 1:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 121 2:0 1:1.9:9 0 36 2:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 122 3:0 1:1.9:9 0 37 3:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 123 4:0 1:1.9:9 0 38 4:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 124 5:0 1:1.9:9 0 39 5:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 125 6:0 1:1.9:9 0 40 6:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 126 7:0 1:1.9:9 0 41 7:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
BMMU-MGX8850-R01>
目前的情况是可以抓取出指定命令的显示结果信息,但是用户担心无法彻底退出交换机,要求在信息显示完之后,重新出现交换机提示符时,
输入5个回车然后发送5个logout再退出交换机(提示:在交换机提示符后面直接敲回车,仍然会出现提示符,与在dos方式下,进入c:\>敲回车
的情况完全相同),其实5个logout在网络正常的情况下,第一个就已经起作用了。
因此抓取的中间文件的最后几行应该是这个样子:
【 此处省略 】
11.2 11 126 7:0 1:1.9:9 0 41 7:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
现在的问题在于,不知道如何修改ant.exp,才能使得发送5个回车,发5个logout,目前我的做法是一旦再遇到匹配
"\[A-Z\]\{4,\}-\[m|g|x|M|G|X\]\{3,\}88\[3|5\]\{1\}0" 这个正则的信息就发送logout退出了,我试过
在这之前面发多个"\r"也不起作用,刚开始用expect,不懂得如何控制这个,请各位指点迷津,谢谢
在一个项目中需要telnet登录到交换机上执行一个命令,然后抓取该命令的显示结果,
登上交换机之后会显示一个类型BMMU-MGX8850-R01>格式的交换机提示符,然后执行一个dsppncons命令,该命令的显示结果可能分多屏显示,如果当前显示的不是最后一屏就会在最后一行提示Type <CR> to continue, Q<CR> to stop:
此时如按回车则继续显示,显示完所有的信息之后,会重新显示出交换机提示符
目前我的做法是用expect 写一个ant.exp脚本,然后再写一个read.sh,然后在read.sh中调用ant.exp 并且把执行结果重定向到一个文件,达到抓取命令的显示结果的目的,该方法完全可以实现抓取该命令的显示结果的要求。
ant.exp脚本
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn telnet $ip
expect "Login:"
send "$username\r"
expect "password:"
send "$password\r"
sleep 3
expect -re "\[A-Z\]\{4,\}-\[m|g|x|M|G|X\]\{3,\}88\[3|5\]\{1\}0"
send "dsppncons\r"
expect {
"*stop:" {exp_send "\r";exp_continue }
-re "\[A-Z\]\{4,\}-\[m|g|x|M|G|X\]\{3,\}88\[3|5\]\{1\}0" { exp_send "logout\r";exit }
}
read.sh 脚本
#! /bin/bash
./ant.exp $1 $2 $3 >$4
例如执行
./read.sh 192.168.1.30 uname password 30.txt
30.txt的结果如下:
spawn telnet 20.2.8.1
Trying 20.2.8.1...
Connected to 20.2.8.1 (20.2.8.1).
Escape character is '^]'.
Login: tdrxr
password:
BMMU-MGX8850-R01> dsppncons
Port VPI VCI CallRef:Flag X-Port VPI VCI CallRef:Flag Type OAM-Type Pri
11.2 11 120 1:0 1:1.9:9 0 35 1:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 121 2:0 1:1.9:9 0 36 2:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 122 3:0 1:1.9:9 0 37 3:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 123 4:0 1:1.9:9 0 38 4:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 124 5:0 1:1.9:9 0 39 5:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 125 6:0 1:1.9:9 0 40 6:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 126 7:0 1:1.9:9 0 41 7:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
Type <CR> to continue, Q<CR> to stop:
Port VPI VCI CallRef:Flag X-Port VPI VCI CallRef:Flag Type OAM-Type Pri
11.2 11 120 1:0 1:1.9:9 0 35 1:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 121 2:0 1:1.9:9 0 36 2:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 122 3:0 1:1.9:9 0 37 3:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 123 4:0 1:1.9:9 0 38 4:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 124 5:0 1:1.9:9 0 39 5:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 125 6:0 1:1.9:9 0 40 6:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
11.2 11 126 7:0 1:1.9:9 0 41 7:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
BMMU-MGX8850-R01>
目前的情况是可以抓取出指定命令的显示结果信息,但是用户担心无法彻底退出交换机,要求在信息显示完之后,重新出现交换机提示符时,
输入5个回车然后发送5个logout再退出交换机(提示:在交换机提示符后面直接敲回车,仍然会出现提示符,与在dos方式下,进入c:\>敲回车
的情况完全相同),其实5个logout在网络正常的情况下,第一个就已经起作用了。
因此抓取的中间文件的最后几行应该是这个样子:
【 此处省略 】
11.2 11 126 7:0 1:1.9:9 0 41 7:1 PTP Yes 8
Calling-Addr: 47.00000000000086080038830a.000001015b08.00
Called-Addr: 47.00000000000086080028850b.000001075b02.00
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
BMMU-MGX8850-R01>
现在的问题在于,不知道如何修改ant.exp,才能使得发送5个回车,发5个logout,目前我的做法是一旦再遇到匹配
"\[A-Z\]\{4,\}-\[m|g|x|M|G|X\]\{3,\}88\[3|5\]\{1\}0" 这个正则的信息就发送logout退出了,我试过
在这之前面发多个"\r"也不起作用,刚开始用expect,不懂得如何控制这个,请各位指点迷津,谢谢
作者: superboy20071207 发布时间: 2011-06-15
自己先关注一下
作者: superboy20071207 发布时间: 2011-06-15