Perl 期望执行命令的输出

发布于 2024-12-11 18:16:25 字数 545 浏览 0 评论 0原文

我写了一个执行 ls 命令的小脚本。现在我想将该命令的输出存储在一个变量中,以便在进一步的 perl 脚本中使用它。不幸的是我无法获取 ls 命令的输出。

my ($core) = "servername";
    my $tel_con = "ssh ".$core. " -l $user";

    $ssh = Expect->spawn("$tel_con");
    $ssh->log_stdout(1);

    unless ( $ssh->expect( 5, -re => '$' ) ) {
            return "Never connected " . $ssh->exp_error() . "\n";
    }
    sleep 1;
    $ssh->send_slow(0, "ls -l\r");
    $ssh->clear_accum();
    my $str = $ssh->exp_after();
    print "STR = '$str'\n";

也许你可以给我一些帮助?

i wrote a little script which executes the ls command. Now I want to store the output of that command in a variable to work with it in the further perl script. Unfortunately I cannot grab the output from the ls command.

my ($core) = "servername";
    my $tel_con = "ssh ".$core. " -l $user";

    $ssh = Expect->spawn("$tel_con");
    $ssh->log_stdout(1);

    unless ( $ssh->expect( 5, -re => '

Maybe you can give me some help please?

) ) { return "Never connected " . $ssh->exp_error() . "\n"; } sleep 1; $ssh->send_slow(0, "ls -l\r"); $ssh->clear_accum(); my $str = $ssh->exp_after(); print "STR = '$str'\n";

Maybe you can give me some help please?

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

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

发布评论

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

评论(6

伴我老 2024-12-18 18:16:25
use Net::OpenSSH;

my $ssh = Net::OpenSSH->new($core, user => $user);
$ssh->error and die $ssh->error;

my $output = $ssh->capture('ls -l');

print "command output:\n$output\n\n";
use Net::OpenSSH;

my $ssh = Net::OpenSSH->new($core, user => $user);
$ssh->error and die $ssh->error;

my $output = $ssh->capture('ls -l');

print "command output:\n$output\n\n";
欢你一世 2024-12-18 18:16:25

如果您不能或不想使用 Net::OpenSSH 您可以这样做:

my @output = $exp->expect(5);
print 'OUT: '.$output[3].'END';

获取整个输出(包括使用的命令、返回字符串、控制台信息)

In case you can not or don't want to use Net::OpenSSH you may do:

my @output = $exp->expect(5);
print 'OUT: '.$output[3].'END';

To get the whole output (including the used command, return string, console information)

浊酒尽余欢 2024-12-18 18:16:25

您可以在单独的进程中调用 Expect 并通过 qx 获取输出或打开管道

you could call expect in a seperate process and grab the output via qx or open a pipe

梦在深巷 2024-12-18 18:16:25

什么是send_slow?根据此命令发送 ls 命令的方式,可以通过不同的方式接收输出。

最有可能的是,命令的错误输出存储在 $?变量,可能是字节移位的。

What is send_slow? Depending on how this command sends the ls command, the output can be received in different ways.

Most probably, the error output of the command is stored in the $? variable, possibly byte-shifted.

吻安 2024-12-18 18:16:25

看起来 Expect 会将所有内容重定向到 STDOUT 并在内部记录它。由于您使用 $ssh->log_stdout(1) 启用了输出,因此您应该能够直接从 STDOUT 获取 ls -l 的结果> (也许通过将标准输出重定向到变量)。您还可以看到尝试从内部日志记录中获取数据。只需确保在执行 clear_accum() 之前获取输出即可。

来自CPAN$object->send_slow($延迟,@strings);

<块引用>

...在每个字符之后,将检查 $object 以确定它是否准备好任何新数据,如果是,则更新累加器以供将来的 Expect() 调用,并将输出打印到 STDOUT 和 @listen_group 如果 log_stdout 和 log_group适当设置。

It seems that Expect will redirect everything to STDOUT and log it internally. Since you enabled output with $ssh->log_stdout(1), you should be able to get the results of ls -l directly from STDOUT (maybe by redirecting standard out to a variable). You can also see try grab the data from the internal logging. Just make sure to grab the output before doing clear_accum().

From CPAN: $object->send_slow($delay, @strings);

... After each character $object will be checked to determine whether or not it has any new data ready and if so update the accumulator for future expect() calls and print the output to STDOUT and @listen_group if log_stdout and log_group are appropriately set.

乖乖 2024-12-18 18:16:25

外行术语?

我必须强制发布此链接 - 我仍然时不时地去那里,这是我发现的所有正则表达式的最外行的解释:

http://www.anaesthetist.com/mnm/perl/Findex.htm

Layman's terms?

I must obligatorily post this link - I still go there from time-to-time, it is the most layman-y explanation I've ever found of all things regex:

http://www.anaesthetist.com/mnm/perl/Findex.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文