如何检查 Expect->spawn($command) 是否成功?

发布于 2025-01-17 00:33:23 字数 632 浏览 0 评论 0原文

我在 Expect->spawn($command) 中传递 scp 命令,我还基于 $object->expect() 传递密码或密码 + OTP代码>. 您能否告诉我是否有办法确定命令是否已成功运行?

我正在尝试 $object->after() ,但它给出的密码提示是之前的 $object->expect() 还探索了 $object->exitstatus() ,它将为空,因为对象未退出。

my $expect_obj = new Expect;
 $expect_obj->log_stdout(0);
 my $spawn = $expect_obj->spawn(split/\s+/, "scp $src $dest");
 my $pos = $expect_obj->expect(60,
                           [qr/password.*: $/, sub {my $self = shift; $self->send("$pass\n")}],
                        );

如何判断命令是否成功?

I am passing an scp command in Expect->spawn($command), I am also passing the password or password + OTP based on a $object->expect().
Could you let me know if there is a way to determine if the command has been run successfully ?

I'm trying $object->after() , but it's giving the password prompt which is the previous $object->expect()
also explored $object->exitstatus() , it'll be empty since the object is not exited.

my $expect_obj = new Expect;
 $expect_obj->log_stdout(0);
 my $spawn = $expect_obj->spawn(split/\s+/, "scp $src $dest");
 my $pos = $expect_obj->expect(60,
                           [qr/password.*: $/, sub {my $self = shift; $self->send("$pass\n")}],
                        );

How do I determine if the command is successful ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2025-01-24 00:33:23

如何判断命令是否成功?

您可以使用 waitpid ,如 这个答案。它将把子进程的退出代码分配给 $? ($CHILD_ERROR),请参阅 perlvar。这是一个例子:

use feature qw(say);
use strict;
use warnings;
use Expect;

my $pass = 'my_password';
my $src = 'a_file.txt';
my $dest = '[email protected]:';
my $port = 2222;
my @params = ('-P', $port, $src, $dest);
my $expect = Expect->new();
$expect->log_stdout(0);
my $spawn = $expect->spawn("scp", @params) or die "Cannot spawn scp: $!\n";
my $child_pid = $expect->pid();
my ($pos, $error, $match, $before, $after) = $expect->expect(
    60,
    [qr/password.*: $/, sub {my $self = shift; $self->send("$pass\n")}],
);
my $res = waitpid $child_pid, 0;
if ($res == $child_pid) {
    my $exit_code = $? >> 8;
    if ($exit_code == 0) {
        say "scp exited successfully";
    }
    else {
        say "scp failed with exit code $exit_code";
    }
}
elsif ($res == -1) {
    say "Child process $child_pid does not exist";
}
else {
    say "Unexpected: waitpid() return unknown child pid: $res";
}

How do I determine if the command is successful ?

You can use waitpid as shown in this answer. It will assign the exit code of the child to $? ($CHILD_ERROR), see more information in perlvar. Here is an example:

use feature qw(say);
use strict;
use warnings;
use Expect;

my $pass = 'my_password';
my $src = 'a_file.txt';
my $dest = '[email protected]:';
my $port = 2222;
my @params = ('-P', $port, $src, $dest);
my $expect = Expect->new();
$expect->log_stdout(0);
my $spawn = $expect->spawn("scp", @params) or die "Cannot spawn scp: $!\n";
my $child_pid = $expect->pid();
my ($pos, $error, $match, $before, $after) = $expect->expect(
    60,
    [qr/password.*: $/, sub {my $self = shift; $self->send("$pass\n")}],
);
my $res = waitpid $child_pid, 0;
if ($res == $child_pid) {
    my $exit_code = $? >> 8;
    if ($exit_code == 0) {
        say "scp exited successfully";
    }
    else {
        say "scp failed with exit code $exit_code";
    }
}
elsif ($res == -1) {
    say "Child process $child_pid does not exist";
}
else {
    say "Unexpected: waitpid() return unknown child pid: $res";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文