Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: NET::OpenSSH to execute multiple commands

by toolic (Chancellor)
on Aug 24, 2012 at 19:40 UTC ( #989627=note: print w/ replies, xml ) Need Help??


in reply to NET::OpenSSH to execute multiple commands

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.


Comment on Re: NET::OpenSSH to execute multiple commands
Select or Download Code
Re^2: NET::OpenSSH to execute multiple commands
by hmb104 (Sexton) on Aug 24, 2012 at 20:16 UTC

    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' );

      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); }
      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.
        ($Core) = &connect( $Core, $user, $pass ); @Output = $Core->capture( "show ip route $IPNumber" ) or die( ($! >> 8) ); $DEBUG && Debug_Output( @Output ); ($Core2) = &connect( $Core2, $user, $pass ); @Output2 = $Core2->capture( "show run | include hostname" ) or die( ($! >> 8) ); $DEBUG && Debug_Output( @Output2 ); foreach $Line ( @Output, @Output2) { if ( $Line =~ m/^Routing entry for / ) { @Data = split( ' ', $Line ); $Subnet = $Data[ 3 ] ; } if ( $Line2 =~ m/^hostname / ) { @Data2 = split( ' ', $Line2 ); $Rname = $Data2[ 2 ] ; last ; } } @Subnet = split( /\./, $Subnet ); $RouterIP = join( '.', $Subnet[0], $Subnet[1], $Subnet[2], '1' ); print( " Router: [$Rname]\t Router IP: [$RouterIP]\n" );

        The loop doesn't featch my Output2 array elements. This is what I get in the command line:

        bash-3.00$ ./test 10.10.10.16 Router: [] Router IP: [10.10.10.1]

        The router name is empty.

      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.
        ($Core) = &connect( $Core, $user, $pass ); @Output = $Core->capture( "show ip route $IPNumber" ) or die( ($! >> 8) ); $DEBUG && Debug_Output( @Output ); ($Core2) = &connect( $Core2, $user, $pass ); @Output2 = $Core2->capture( "show run | include hostname" ) or die( ($! >> 8) ); $DEBUG && Debug_Output( @Output2 ); foreach $Line ( @Output, @Output2) { if ( $Line =~ m/^Routing entry for / ) { @Data = split( ' ', $Line ); $Subnet = $Data[ 3 ] ; } if ( $Line2 =~ m/^hostname / ) { @Data2 = split( ' ', $Line2 ); $Rname = $Data2[ 2 ] ; last ; } } @Subnet = split( /\./, $Subnet ); $RouterIP = join( '.', $Subnet[0], $Subnet[1], $Subnet[2], '1' ); print( " Router: [$Rname]\t Router IP: [$RouterIP]\n" );

        The loop doesn't featch my Output2 array elements. This is what I get in the command line:

        bash-3.00$./test.pl 10.10.10.16 Router: [] Router IP: [10.10.10.1]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989627]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-08-23 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls