Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

NET::OpenSSH to execute multiple commands

by hmb104 (Sexton)
on Aug 24, 2012 at 19:25 UTC ( [id://989626]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I'm writing a very basic script using Net::OpenSSH to connect to some switches and extract some data. I have tried to use Net::SSH and it is really slow and it crashes my system. I have also tried to Expect before, but I really like to use the Net::OpenSSH module.

Is it possible to execute more than 1 command in 1 connection using this module?. Any ideas?

Also in the foreach loop, can I read from 2 arrays? Like

foreach (@arra1 || @arra2)?

Thanks for the help.

Replies are listed 'Best First'.
Re: NET::OpenSSH to execute multiple commands
by toolic (Bishop) on Aug 24, 2012 at 19:40 UTC
    Also in the foreach loop, can I read from 2 arrays? Like foreach (@arra1 || @arra2)?
    The following will loop through the elements of the 1st array then loop through those of the 2nd array, as if the 2 arrays were one big happy list:
    foreach (@arra1, @arra2)

    See also List::MoreUtils::zip if you want to alternate elements.

      Thanks for the reply. I'm still having problems with it. I will paste my code snippet to get some feedback if possible

      $user="myname"; $pass="mypass"; @ip = split( /\./, $IPNumber ); if ( $ip[0] == 110 ) { $rtrIP = "10.10.10.1"; } else if ( $ip[0] == 120 ) { $rtrIP = "10.20.20.1"; } else { die( "not in my range" ); } ($Core) = &SSHconnect( $rtrIP, $user, $pass ); @Output = $Core->capture( "show ip route $IPNumber" ) or die( " command failed: " . ($! >> 8) ); ($Core2) = &SSHconnect( $rtrIP, $user, $pass ); @Output2 = $Core2->capture( "show run | include hostname" ) or die( " command failed: " . ($! >> 8) ); foreach $Line ( @Output || @Output2 ) { if ( $Line =~ m/^Routing entry for / ) { @Data = split( ' ', $Line ); $Subnet = $Data[ 3 ] ; last ; } if ( $Line =~ m/^hostname / ) { @Data2 = split( ' ', $Line ); $Rname = $Data[ 2 ] ; last ; } } @network = split( /\./, $Subnet ); $rIP = join( '.', $network[0], $network[1], $network[2], '1' );
        foreach $Line ( @Output || @Output2 ) {
        The foreach loop will probably get executed only once, and $Line will take on a value equal to the number of elements in the @Output array. You probably want a comma, like I showed.
        foreach $Line ( @Output || @Output2 )
        You didn't replace your code with the right version specified above. Please fix it. Also, please show the error message you get (and do it every time you ask for help, see How do I post a question effectively?).
        Sorry if my advice was wrong.

        This is the ssh sub:

        sub SSHconnect { my $ip = shift; my $user = shift; my $pass = shift; my %options = ( user => $user, passwd => $pass, master_stderr_discard => 1, master_opts => [ -o => "UserKnownHostsFile=/dev/null", ], ssh_cmd => '/usr/local/bin/ssh' ); my $ssh = Net::OpenSSH->new( $ip, %SSHOptions ); $ssh->error and die( $SSH->error ); return ($SSH); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://989626]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-30 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found