免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4708 | 回复: 7

[处理异常]Perl Expect 处理 ---MORE---的情况 [复制链接]

论坛徽章:
0
发表于 2009-06-06 22:30 |显示全部楼层

use Expect;
my $exp = new Expect;
sleep(1);
$exp->raw_pty(0);
$exp->exp_internal(1);
$exp->log_stdout(0);
$exp->debug(1);
my $exp = Expect->spawn("telnet 192.168.1.1");

                  sleep(2);
                 $exp->send("root\n");
                 sleep(1);
                 $exp->expect("assword");
                 sleep(1);
                 $exp->send("password\n");

                 $exp->expect("cli>");
                 sleep(1);
                 $exp->send("show product-info\n");
                 $exp->expect("--MORE--");
                 #$exp->send("show product-info\n";

                 sleep(1);
                 $exp->send(" ");
                 while(0){
                    sleep(1);
                    $exp->expect( [ qr'--MORE--'=>sub{
                                                  $exp->send(" ");
                                                  exp_continue; } ],
                                  [qr'cli>'=>{break } ]);
                 }

$exp->debug();
$exp->soft_close();



刚开始用Expect, 不用while循环可以正常显示.

debian:~# ./expect_tst.pl
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
root

BusyBox on localhost login: root
Password:
DSL Modem CLI
Copyright (c) 2004 Texas Instruments, Inc.
cli> show product-info
Product Information
   Model Number         : AR7WRD
   HW Revision          : 01000008-02010202
   Serial Number        : none
   USB PID              : N/A
   USB VID              : N/A
   Ethernet MAC         : 00:08:5C:7B:9B:78
   DSL MAC              : 00:08:5C:7B:9B:7A
   USB MAC              : N/A
   USB Host MAC         : N/A
   AP MAC               : 00:08:5C:7B:9B:79

Software Versions
   Gateway              : 3.7.0B
   ATM Driver           : 20.1 (6.00.01.00-370B.060526)

   DSL HAL              : 6.00.01.00

   DSL Datapump         : 6.00.04.00 Annex A
   SAR HAL              : 01.07.2b

   PDSP Firmware        : 0.54

   Wireless Firmware    : 3.3.0.28
   Wireless APDK        : 6.3.1.26
   Boot Loader          : 1.3.7.15

cli> Closing .
at /usr/local/share/perl/5.8.8/Expect.pm line 1431
        Expect::hard_close('Expect=GLOB(0x834e30c)') called at /usr/local/share/perl/5.8.8/Expect.pm line 1621
        Expect:ESTROY('Expect=GLOB(0x834e30c)') called at ./expect_tst.pl line 0
        eval {...} called at ./expect_tst.pl line 0
closed.


但是想使用循环去匹配 --MORE-- 的时候就不行了,
老提示:

debian:~# ./expect_tst.pl
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
root

BusyBox on localhost login: root
Password: Pattern #1 doesn't have a CODE referenceafter the pattern. at ./expect_tst.pl line 33
Closing .
at /usr/local/share/perl/5.8.8/Expect.pm line 1431
        Expect::hard_close('Expect=GLOB(0x834e300)') called at /usr/local/share/perl/5.8.8/Expect.pm line 1621
        Expect:ESTROY('Expect=GLOB(0x834e300)') called at ./expect_tst.pl line 0
        eval {...} called at ./expect_tst.pl line 0
closed.

论坛徽章:
0
发表于 2009-06-06 23:49 |显示全部楼层
不懂。。。
关注一下~:wink:

论坛徽章:
0
发表于 2009-06-07 03:42 |显示全部楼层
纯expect 是没有问题,可以执行到While 里面的语句的,

#!/usr/bin/expect -f

if { $argc != 3 } {
        send_user "usage: get_hwrtne_conf.exp ip user password\n"
        exit
}

set timeout 1
set TERM ANSI

set SERVER [lindex $argv 0]
set USERNAME [lindex $argv 1]
set PASSWD [lindex $argv 2]

spawn telnet
expect "telnet> "
send "open $SERVER\r"
sleep 1
expect "login:"
send "$USERNAME\r"
sleep 1
expect "assword:"
send "$PASSWD\r"
sleep 1
expect "*>"

send "show product-info\r"
while (1) {
        sleep 1
        expect {
                "*MORE*" { send " " }
                "*>" { break }
        }
}
send "quit\r"

interact



Perl 脚本:
use Expect;
use strict;
my $exp = new Expect;
sleep(1);
$exp->raw_pty(1);
#$exp->log_stdout(0);
my $exp = Expect->spawn("telnet 192.168.1.1";

$exp->exp_internal(1);
$exp->debug(3);
$exp->log_file("out","w";
                  sleep(2);
                 $exp->send("root\n";
                 sleep(1);
                 $exp->expect("assword";
                 sleep(1);
                 $exp->send("password\n";

                 $exp->expect("cli>";
                 sleep(1);
                 $exp->send("show product-info\n";
                 while(1){
                    sleep(1);
                    print "<--=\n";
                    $exp->expect( [ qr/--MORE--/i,sub{
                                                  my $self =shift;
                                                  $self->send_slow(" ";
                                                  exp_continue; } ],
                                  [qr'cli>',{ last } ]);
                 }

#$exp->debug();
$exp->soft_close();



问题:
(1)Perl的脚本就无法执行到While(1),我觉得很奇怪,
(2) 下面这个问题不知道是啥错误
at /usr/local/share/perl/5.8.8/Expect.pm line 1264
        Expect::print('Expect=GLOB(0x834e74',



Debug 信息:

Connected to 192.168.1.1.
Escape character is '^]'.
root

spawn id(5): Does `Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is \'^]\'.\r\nroot\r\n'
match:

Sending 'password\n' to spawn id(5)
at /usr/local/share/perl/5.8.8/Expect.pm line 1264
        Expect::print('Expect=GLOB(0x834e74', 'password\x{a}') called at ./expect_tst.pl line 22
Starting EXPECT pattern matching...
at /usr/local/share/perl/5.8.8/Expect.pm line 561
        Expect::expect('Expect=GLOB(0x834e74', 'cli>') called at ./expect_tst.pl line 24
spawn id(5): list of patterns:


spawn id(5): Does `Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is \'^]\'.\r\nroot\r\n'
match:


BusyBox on localhost login: root
Password:
spawn id(5): Does `Trying 192.168.1.1...\r\nConnected to 192.168.1.1.\r\nEscape character is \'^]\'.\r\nroot\r\n\r\nBusyBox on localhost login: root\r\nPassword: '
match:

Sending 'show product-info\n' to spawn id(5)
at /usr/local/share/perl/5.8.8/Expect.pm line 1264
        Expect::print('Expect=GLOB(0x834e74', 'show product-info\x{a}') called at ./expect_tst.pl line 26
<--=
Closing spawn id(5).
at /usr/local/share/perl/5.8.8/Expect.pm line 1354
        Expect::soft_close('Expect=GLOB(0x834e74') called at ./expect_tst.pl line 42

DSL Modem CLI
Copyright (c) 2004 Texas Instruments, Inc.
cli> show product-info
Product Information
   Model Number         : AR7WRD
   HW Revision          : 01000008-02010202
   Serial Number        : none
   USB PID              : N/A
   USB VID              : N/A
   Ethernet MAC         : 00:08:5C:7B:9B:78
   DSL MAC              : 00:08:5C:7B:9B:7A
   USB MAC              : N/A
   USB Host MAC         : N/A
   AP MAC               : 00:08:5C:7B:9B:79

Software Versions
   Gateway              : 3.7.0B
   ATM Driver           : 20.1 (6.00.01.00-370B.060526)

   DSL HAL              : 6.00.01.00

   DSL Datapump         : 6.00.04.00 Annex A
   SAR HAL              : 01.07.2b

   PDSP Firmware        : 0.54

--MORE--Timed out waiting for an EOF from spawn id(5).
spawn id(5) closed.
Pid 12929 of spawn id(5) exited, Status: 0x01
Closing spawn id(5).
at /usr/local/share/perl/5.8.8/Expect.pm line 1431
        Expect::hard_close('Expect=GLOB(0x834e74') called at /usr/local/share/perl/5.8.8/Expect.pm line 1621
        Expect:ESTROY('Expect=GLOB(0x834e74') called at ./expect_tst.pl line 0
        eval {...} called at ./expect_tst.pl line 0

[ 本帖最后由 mantou 于 2009-6-7 04:02 编辑 ]

论坛徽章:
0
发表于 2009-06-07 13:57 |显示全部楼层
顶起来, CU的大侠们多多指点哈

论坛徽章:
0
发表于 2009-06-08 14:18 |显示全部楼层
CU的大侠们, 帮忙多多知道解决呀,

论坛徽章:
0
发表于 2009-06-08 21:02 |显示全部楼层
自己搞定wihle 那部分。
搞定,继续

[ 本帖最后由 mantou 于 2009-6-8 22:38 编辑 ]

论坛徽章:
0
发表于 2009-06-09 00:20 |显示全部楼层

论坛徽章:
0
发表于 2011-06-23 10:46 |显示全部楼层
问题2应该是调试信息而不是报错信息,同求高手解决问题1。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP