简体   繁体   中英

How can I get STDERR and STDOUT of a command executed on a remote machine through SSH in Perl?

I have to connect to a remote machine via SSH executed through Expect module. I have to access the STDERR / STDOUT separately if possible and look at the output. I can redirect the output of the script eg

$command = "ssh <script> 2>/tmp/stderr.output"
$exp = Expect->spawn($command) or die "Cannot spawn\n";;
my @command1= $exp->expect(5);

but then I have to remotely connect again to check stderr.output . Is there a way that expect returns the STDERR / STDOUT separately.

You might want to consider using Net::OpenSSH which provides a capture2 method:

($output, $errput) = $ssh->capture2(\%opts, @cmd)

captures the output sent to both stdout and stderr by @cmd on the remote machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM