使用 SSH 实时记录到文件?

Real time logging to file with SSH?

my $file = 'log.log';
my $cmd = 'sysstat';
my $ssh = Net::SSH::Perl->new($host);
$ssh->login('admin', 'password');

open my $in, "-|", ($ssh->cmd($cmd))[0];
open my $out_fh, ">", $file;
#print+($ssh->cmd($cmd))[0]."\n";
while (my $line = <$in>) {
    print { $out_fh } $line;
}

关于如何实时将 ssh 输出记录到文件的任何建议? $cmd 将 运行 永远,我希望它吐出的每一行都实时写入文件。

你不能用 Net::SSH::Perl 做到这一点......好吧,至少不容易!

改用Net::OpenSSH

$ssh = Net::OpenSSH->new($host, user => $user, password => $password);
$ssh->system({stdout_file => $file}, $cmd);