http://www.perlmonks.org?node_id=1038427

walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

Basically, what I am trying to accomplish here is the ability to see Net::OpenSSH log into a remote system, run commands (viewing STDOUT/STDERR from these commands), and then exit. I can easily connect to the system, capture STDERR/STDOUT, and then print them. But I'd really like to see what is happening as it is happening as if I was sitting there at my terminal typing the commands myself.

Let me try to be a bit more specific. What I'd really like is for Net::OpenSSH to spawn a shell (/bin/bash), ssh to remote server and run commands. I'd be looking first at a localhost prompt, then the ssh connection ('localhost # ssh remote_server'), possible a password prompt and the interaction that happens there (if password => 'pass' is used when creating object), and then the commands as they are passed on the remote shell and the remote prompt as well.

I haven't included any code here because I have no idea if it's even possible, thus no idea really where to start. I did create a Net::OpenSSH object and used Expect to create an interactive terminal, but I still only ended up with output... As well, I'd really rather not use Expect for this if I don't need to (i.e., the commands themselves are not interactive).

#!/usr/bin/env perl + use strict; + use warnings; + use Net::OpenSSH; + use Expect; + + my $timeout = 20; + my $ssh = Net::OpenSSH->new('walkingthecow@remote_host', password => ' +PkDDaDoES$551890'); # not really my password ;) + my ($pty, $pid) = $ssh->open2pty("ls /etc") + or die "open2pty failed: " . $ssh->error . "\n"; + + my $expect = Expect->init($pty); + $expect->raw_pty(1); + $expect->log_stdout(1); + while(<$pty>) { + print "$_" + }