打印期望输出而不调用变量

问题描述 投票:0回答:1

我想了解Expect Perl模块。

我想从$var获取值和机器,但即使我使用正则表达式,我也无法使我的代码工作。

我想从$var机器和命令提示符的值(如机器1-7),但我不知道为什么它在终端打印$var因为我没有调用$var变量

$Expect::Exp_Internal =0;
my $admin_user = "root";
my $timeout = 40;
my $prompt = '~]#';


my @devices =qw(
machine1
machine2
machine3);


foreach my $device (@devices){
my $exp = Expect->spawn("ssh", "-o", "UserKnownHostsFile /dev/null", "root\@$device");

$exp->log_user(0);


my $seen_user = 0;


$exp->expect(
    $timeout,
    [
        qr/connecting \(yes\/no\)\? / => sub {
            my $self = shift;
            $self->send("yes\r");
            exp_continue;
        }
    ],
    [
        qr/User: ?/ => sub {
            my $self = shift;
            if ( $seen_user == 1 ) {
                $var .= "Bad password on $device \n<br>";
                $exp->soft_close();
            }
            #  $self->send("$admin_user\n<br>");
            $seen_user = 1;
            exp_continue;
        }
    ],
    [
        qr/$prompt/ => sub {
            my $self = shift;
            #    print 'first_prompt';
        }
    ],
    [
        'timeout' => sub {
            $var .= "timed out during login on $device\n<br>";
        }
    ]
);

$exp->log_user(1);

# This following line is printed, without being called:

my $var = $exp->send("ps -aef | grep -i app | wc -l\n");

#$var =~ s/\D+//g;
#print 'numbers'.$var;

print $var;
undef $var;

my $logout_sent = 0;

$exp->expect(
    $timeout,
    [
        qr/$prompt/ => sub {
            my $self = shift;
            $exp->log_user(0);
            # $self->send("ps -aef | grep -i sql | wc -l\r");
            $self->send("logout\r");
            $logout_sent = 1;
            exp_continue;
        }
    ],

    }],

    [
        qr/\(y\/N\)/ => sub {
            my $self = shift;
            $self->send("y");
            print "ok\n<br>";
            #    exit 0;
        }
    ],
    [
        'timeout' => sub {
            print "timed out waiting for prompt\n<br>";
            # exit 1;
        }
    ],
    [
        'eof' => sub {
            if ($logout_sent) {
                $var .= "succesfully logged out from the $device\n<br>";
                #exit 0;
            }
            else {
                $var .="unexpected eof waiting for prompt on $device\n<br>";
            }
        }
    ]
);

}

The Output

<br>ps -aef | grep -i app | wc -l
7
[root@machine1 ~]# succesfully logged out from the machine1
<br>ps -aef | grep -i app | wc -l
9
[root@machine2 ~]# succesfully logged out from the machine2
<br>ps -aef | grep -i app | wc -l
7
[root@machine3 ~]# succesfully logged out from the machine3

perl automation expect.pm
1个回答
0
投票

您可以像这样获得机器的价值:

# $exp->clear_accum(); # clear the accumulator if you need to
$exp->expect( $timeout, '-re', 'root@machine\d+');
my ($machine_number) = $exp->match() =~ /root@machine(\d+)/;

然后你可以获得这样的进程数:

$exp->clear_accum(); # clear the accumulator if you need to
$exp->send("ps -aef | grep -i app | wc -l\n");
$exp->expect( $timeout, '-re', '^\d+');
my ($num_processes) = $exp->match() =~ /(\d+)/;
© www.soinside.com 2019 - 2024. All rights reserved.