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

Hi Monks,

First off, I'm not a competent Perl coder, so please forgive me if this is obvious....

I'm trying to get the following code to execute two commands on a remote machine using Net::OpenSSH. I can login and execute the first command ok, but the second command simply returns "Connection to x.x.x.x closed by remote host." By introducing a delay before the 2nd command and looking at netstat I can see that the connection is still open, right up until the second command is attempted. In fact netstat looks like:

netstat -an | grep x.x.x.x tcp 0 0 x.x.x.x:55942 x.x.x.x:22 ESTABLISH +ED unix 2 [ ACC ] STREAM LISTENING 7356830 /home/cstron +g/.libnet-openssh-perl/root-10.224.138.142-26229-724671
Is it even possible to execute two commands without opening two connections to the remote machine? Where am I going wrong?!

Thanks Monks!
#!/usr/bin/perl use Net::OpenSSH; $ip="x.x.x.x"; $user="xx"; $pass="xx"; ($ssh)=&connect_ssh($ip, $user, $pass); @arry = $ssh->capture("ps -ef"); while (@arry) { $line=shift @arry; print "MATCH1 $line"; } @arry = $ssh->capture("ps -ef"); while (@arry) { $line=shift @arry; print "MATCH2 $line"; } sub connect_ssh { my $ip = shift; my $user = shift; my $pass = shift; my $ssh = Net::OpenSSH->new( $ip, strict_mode => '0', user => $user, password => $pass, timeout => '60'); return ($ssh); }

By the way, I know running twp "ps -ef" is pointless, but I'm just trying to illustrate the fact that I can't get a second command to run.

I have openssl installed and normal ssh works perfectly fine.

openssl version OpenSSL 0.9.8g 19 Oct 2007

Replies are listed 'Best First'.
Re: Net::OpenSSH multiple commands
by salva (Canon) on Sep 18, 2009 at 14:53 UTC
    I can't see anything wrong in your script!

    Net::OpenSSH has a debug mode that will tell you what's going on. Just add the following line at the beginning of your script:

    $Net::OpenSSH::debug = -1;
    Also, can you tell us the versions of the software you are using, including the OS, perl, Net::OpenSSH, the local SSH client and the remote SSH server?
      Thanks Salva for your response

      I'm running

      Fedora release 9 (Sulphur)

      Perl v5.10.0 built for i386-linux-thread-multi

      openssh-5.1p1

      Local SSH client is OpenSSH_5.1p1, OpenSSL 0.9.8g 19 Oct 2007

      Remote SSH server is OpenSSH_2.5.2p2, SSH protocols 1.5/2.0

      Debug output is as follows:
      # call args: ['ssh','-S','/home/cstrong/.libnet-openssh-perl/root-10.2 +24.138.142-26522-522395','-o','User=root','--','10.224.138.142','ps - +ef'] # open_ex: ['ssh','-S','/home/cstrong/.libnet-openssh-perl/root-10.224 +.138.142-26522-522395','-o','User=root','--','10.224.138.142','ps -ef +'] Connection to 10.224.138.142 closed by remote host. # set_error(5 - child exited with code 255) # DESTROY(Net::OpenSSH=HASH(0x949885c), pid => 26523) # call args: ['ssh','-O','exit','-S','/home/cstrong/.libnet-openssh-pe +rl/root-10.224.138.142-26522-522395','-o','User=root','--','10.224.13 +8.142'] # open_ex: ['ssh','-O','exit','-S','/home/cstrong/.libnet-openssh-perl +/root-10.224.138.142-26522-522395','-o','User=root','--','10.224.138. +142'] # set_error(5 - child exited with code 255)
        Remote SSH server is OpenSSH_2.5.2p2, SSH protocols 1.5/2.0

        That's quite old (~2001) and probably the cause of your problems... the only workaround besides upgrading the server would be to open a new connection for every command.

        Anyway, you (or the corresponding party) should upgrade the server software even if only for security reasons!

        Sorry, forgot to say that the debug ouput just posted is shown after the output of the first "ps -ef" - i.e. the important bit!