【问题标题】:Perl Grep in ssh2->exec not finding anythingssh2->exec 中的 Perl Grep 没有找到任何东西
【发布时间】:2014-09-16 05:47:59
【问题描述】:

我设计了一个程序,它会检查我所有应用程序中不同 Solaris (Unix) 盒子中的每个日志文件,并查找某个错误。所以,我设计了这样的程序:

my $ssh2 = Net::SSH2->new();
$ssh2->connect($host) or die "Unable to connect Host $@ \n";
$ssh2->auth_keyboard($user,$pass) or die "Unable to login $@ \n";

$chan1 = $ssh2->channel();
$chan1->exec("find /apps/appName/loc/Logs/server/ -type f | xargs grep \"CVS Call\" ");
while(<$chan1>) {
    print $_ . "\n";
}

如果我要 grep 一个单词(只是“CVS”),它就可以了。但是,第二次我添加一个空格或一个点它没有找到任何东西。我尝试用 [[:space:]] 或 [:space:] 替换空格,但它仍然没有显示任何结果。我知道日志文件包含此错误。

我也试过了:

$chan1->exec("find /apps/appName/loc/Logs/server/ -type f -exec grep 'CVS Call' '{}' \\; -print");

我用这个命令得到了相同的结果,只用一个词查找,只要我添加第二个词,它就不会显示任何结果。

有没有人知道为什么当我在 grep 搜索中添加一个空格时它没有找到任何东西。

【问题讨论】:

    标签: regex linux perl ssh grep


    【解决方案1】:

    这个问题可能与所需的几个引用级别有关。用右手做是相当困难的。

    如果你使用的是 Linux/Unix 机器,你可以使用 Net::OpenSSH 来为你做引用:

     my $ssh = Net::OpenSSH->new($host, user => $user, password => $password);
     my @lines = $ssh->capture('find', $logs_dir,
                               '-type', 'f',
                               '-exec', 'grep', '-q', $string, '{}', ';',
                               '-print');
    
     for (@lines) {
         ...
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      相关资源
      最近更新 更多