vasanth.easyrider has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks


I have written a perl script to connect to cisco routers and execute command "show interface interface-name". Issue here is that the entire command output is not getting captured. Perl script is -


#!/usr/bin/perl #we will use the minimum required modules or features for our perl scr +ipt use strict; use warnings; use Net::OpenSSH; #Declaring the required variables/arrays my ($DeviceIP, $username, $passwd,$CommandE, $ssh,$expect,$command,$ti +meout,$t); my (@output,@lines); $username = q(newnetcool); $passwd = q(Hello@123); #We will read the input arguments $DeviceIP = q(202.123.37.37); $CommandE = "show interface ae16"; print "Received details are = $DeviceIP and $CommandE\n"; print "The command to be executed is = $CommandE\n"; eval { $ssh = Net::OpenSSH->new($DeviceIP, user => $username, password => + $passwd, timeout => 10, strict_mode => 0); @output = $ssh->capture($CommandE) or die "remote command failed: +" . $ssh->error; print "output start point\n"; print "Device IP = $DeviceIP and Command = $CommandE\n"; print "@output\n"; print "output end point\n"; undef $ssh; }; if($@) { print "Error. Please check - $@"; }

Actual command output is =

---------------------------------


Physical interface: ae16 (MC-AE-16, active), Enabled, Physical link is + Up Interface index: 145, SNMP ifIndex: 5398 Description: #ANG(CEN)-NNI-CEN for BOTH TAG-(ANG-WAO NPE-2(L1:-192.1 +68.53.34),PORT-Te0/0/0/7)-Secondary#Primary##CONNECTED-TO-$BLR-ISP-AC +C-RTR-059$-$xe-2/3/1$##CMR:512754# Link-level type: Flexible-Ethernet, MTU: 9192, Speed: 10Gbps, BPDU E +rror: None, MAC-REWRITE Error: None, Loopback: Disabled, Source filte +ring: Disabled, Flow control: Disabled Pad to minimum frame size: Disabled Minimum links needed: 1, Minimum bandwidth needed: 1bps Device flags : Present Running Interface flags: SNMP-Traps Internal: 0x4000 Current address: 54:e0:32:74:1f:05, Hardware address: 02:23:9c:f4:00 +:ca Last flapped : 2018-06-25 01:24:34 IST (1w1d 13:41 ago) Input rate : 2778310800 bps (669429 pps) Output rate : 7111971424 bps (1013741 pps) Logical interface ae16.101 (Index 7407) (SNMP ifIndex 6805) Description: D-VOIS-ISP-LINK Flags: Up SNMP-Traps 0x4000 VLAN-Tag [ 0x8100.101 ] Encapsulation +: ENET2 Bandwidth: 2000mbps Statistics Packets pps Bytes bps Bundle: Input : 143083 0 9917359 0 Output: 156698 0 10872523 0 Adaptive Statistics: Adaptive Adjusts: 0 Adaptive Scans : 0 Adaptive Updates: 0 Protocol inet, MTU: 9170 Flags: Sendbcast-pkt-to-re Addresses, Flags: Is-Preferred Is-Primary Destination: 125.16.224.192/30, Local: 125.16.224.193, Broadca +st: 125.16.224.195 Protocol multiservice, MTU: Unlimited


But when i capture output through the script the output i am getting is -

----------------------------------------------------------------------------


Physical interface: ae16 (MC-AE-16, active), Enabled, Physical link is + Up Interface index: 145, SNMP ifIndex: 5398 Description: #ANG(CEN)-NNI-CEN for BOTH TAG-(ANG-WAO NPE-2(L1:-192.1 +68.53.34),PORT-Te0/0/0/7)-Secondary#Primary##CONNECTED-TO-$


it is getting captured till $ only. I think the special character is causing the issue. Can you help me how to capture entire output

2018-07-04 Athanasius added code tags

Replies are listed 'Best First'.
Re: Special Characters_CommandOutput
by Lotus1 (Vicar) on Jul 03, 2018 at 13:36 UTC

    In the documentation for Net::OpenSSH I noticed they don't use the capture or die construct but instead check for the error on a seperate line. This made sense once I found the description of what happens during a capture if there is an error. It goes ahead and returns what it has captured so far so the or die... won't be triggered.

    When an error happens while capturing (for instance, the operation times out), the partial captured output will be returned. Error conditions have to be explicitly checked using the "error" method. For instance:
    my $output = $ssh->capture({ timeout => 10 }, "echo hello; sleep 20; echo bye"); $ssh->error and warn "operation didn't complete successfully: ". $ssh->error; print $output;

    Try replacing this line

    @output = $ssh->capture($CommandE) or die "remote command failed: +" . $ssh-> +error;
    with this and see what happens. I've never used this module but the timeout options might be worth investigating.
    @output = $ssh->capture($CommandE); $ssh->error and die "remote command failed or didn't complete successfully: " +. $ssh->error;

    There is also the capture2 function to capture both STDERR and STDOUT. I couldn't tell from your command prompt version of the output if there was anything going to STDOUT.

    Edit: PS If you put your command output text into code tags<code> ...[text]...</code> then it will just be text and brackets won't be interpreted as links. It will be much easier for us to understand what is going on.

    Edit: Using capture with Net::OpenSSH on Cisco routers came up in this node. It looks like salva had some good suggestions.

Re: Special Characters_CommandOutput
by salva (Canon) on Jul 03, 2018 at 16:56 UTC
    Read the FAQ in the Net::OpenSSH documentation about connecting to networking devices.

    Try also using the stdin_keep_open hack:

    @output = $ssh->capture({stdin_data => '', stdin_keep_open => 1}, $Com +mandE);