【问题标题】:How do you read every line of the output using Perl's OpenSSH如何使用 Perl 的 OpenSSH 读取输出的每一行
【发布时间】:2016-06-24 01:23:01
【问题描述】:

使用 Perl 的 Net::SSH::Except,有一个名为 read_line 的方法可以让我读取输出的每一行。

我想使用Net::OpenSSH,但找不到类似的东西。

如何使用OpenSSH 编写这个SSH::Except 方法?

$ssh->send("ls\r\n");

while ( defined( $line = $ssh->read_line() ) {
    # do something
}

【问题讨论】:

  • 我想你的意思可能是Net::SSH::Expect?您必须始终将代码和数据复制到问题中
  • 公平点。与其说是解决方案,不如说是一个指针
  • @ChrisDoyle:如果您认为您有解决方案,请发布。评论不是摘录或背诵的地方。另外,你说的是哪个模块?据我所知,POD for Net::OpenSSH 中没有类似的东西,Net::SSH::Except 不存在
  • @ChrisDoyle:对不起,克里斯。我搞砸了我的降价,不得不重写,所以你的答案现在在我的问题之前。但是那个“cpan 页面”摘录的的来源是什么?
  • 我明白你的意思,在 Net::OpenSSH 中有一个捕获方法。

标签: perl ssh openssh


【解决方案1】:

Net::OpenSSH 模块提供了一个捕获方法来捕获所有的输出。如果在列表上下文中调用将逐行返回输出

use strict;
use warnings;
use Net::OpenSSH;

my $host = "localhost";
my %opts = (
        user => 'cdoyle',
        password => 'thisisnottherealpassword',
);
my $ssh = Net::OpenSSH->new($host, %opts);
my @output = $ssh->capture('ls -lrta');

my $count=0;
foreach my $line (@output){
        $count++;
        print "line $count $line";
}

给出输出

line 1 total 28
line 2 -rw-------  1 cdoyle users   73 Jun 11  2015 .Xauthority
line 3 -rw-------  1 cdoyle users  602 Jun 11  2015 .viminfo
line 4 -rw-r--r--  1 cdoyle users   32 Jun 11  2015 .profile
line 5 drwxr-xr-x  2 cdoyle users 4096 Jun 11  2015 .
line 6 -rw-------  1 cdoyle users  682 Aug 20  2015 .sh_history
line 7 drwxr-xr-x 32 root   root  4096 Feb 21 02:33 ..

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多