Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: NET::OpenSSH to execute multiple commands

by hmb104 (Sexton)
on Aug 24, 2012 at 20:16 UTC ( #989633=note: print w/ replies, xml ) Need Help??


in reply to Re: NET::OpenSSH to execute multiple commands
in thread NET::OpenSSH to execute multiple commands

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


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

    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); }
Re^3: NET::OpenSSH to execute multiple commands
by toolic (Chancellor) on Aug 24, 2012 at 20:27 UTC
    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.

Re^3: NET::OpenSSH to execute multiple commands
by aitap (Deacon) on Aug 24, 2012 at 20:29 UTC
    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]

        Try changing $Line2 to $Line in the following:

        if ( $Line2 =~ m/^hostname / ) { @Data2 = split( ' ', $Line2 ); $Rname = $Data2[ 2 ] ; last ; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-23 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls