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


In reply to Net::OpenSSH multiple commands by cstrong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.