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

Hi Perl monks
I have a requirement. Need to do telnet to a remote router and execute 2 commands and capture the output. I have written perl code, given below. The issue i am facing is, output is getting captured for first command, but for second command, output is not getting captured. I am providing sample outputs for both the commands (may be it will help is identifying the reason for this issue)

Note - Is this issue because of empty lines in the command 2 output? please suggest

command 1 sample output -

Fri Feb 23 15:10:52.014 IST
R/S/I Modules LED Status
0/RP0/*
host Critical-Alarm Off
host Major-Alarm Off
host Minor-Alarm Off


command 2 sample output -

RP/0/RP0/CPU0:BLR-WFD-MPL-COR-RTR-42-237#show isis neighbors
Fri Feb 23 11:22:50.269 IST

IS-IS IGP neighbors:
System Id Interface SNPA State Holdtime Type IETF-NSF

IS-IS CORE neighbors: System Id Interface SNPA State Holdtime Type IETF-NSF BPL-CPT-MPL-COR-RTR-42-241 BE4 PtoP Up 27 L2 Capable BLR-MPL-LTE-PE-RTR-211 BE8 PtoP Up 26 L2 Capable BLR-MPL-LTE-PE-RTR-211 BE70 PtoP Up 29 L2 Capable BLR-MPL-LTE-PE-RTR-211 BE71 PtoP Up 22 L2 Capable

Perl code -

#!/usr/bin/perl use strict; use warnings; use Switch; use Net::Telnet(); my ($host,$hostname,$username, $passwd, $Eleprompt,$t,@output); $host = q(10.0.0.225); $username = q(user_name); $passwd = q(My_Password); $Eleprompt = q(RP\/0\/RSP0\/CPU0:LCK-MPL-PE-RTR-225\#); eval { if($t = new Net::Telnet (Timeout => 20, Prompt => $Eleprompt)) { print "Got the expected prompt for $hostname\n"; } print "connection status = $t\n"; $t->open($host); my $CS = $t->login($username, $passwd); print "login status = $CS\n"; @commandoutput1 = $t->cmd("admin show environment leds"); print "output is = @commandoutput1 \n"; @commandoutput2 = $t->cmd("show isis neighbors"); print "output is = @commandoutput2 \n"; }; if($@) { print "since we got error w.r.t shell prompt for the IP $host, +we are proceeding with next device\n - $@"; next; }

Replies are listed 'Best First'.
Re: Net::Telnet issue
by poj (Abbot) on Feb 23, 2018 at 12:38 UTC

    Try add some logging for debugging

    if ($t = new Net::Telnet ( Timeout => 20, Prompt => $Eleprompt, Input_Log => 'telnet.log' )){

    and fix these errors

    Possible unintended interpolation of @commandoutput1 in string at telnet0.pl line 23.
    Possible unintended interpolation of @commandoutput2 in string at telnet0.pl line 25.
    Global symbol "@commandoutput1" requires explicit package name at telnet0.pl line 22.
    Global symbol "@commandoutput1" requires explicit package name at telnet0.pl line 23.
    Global symbol "@commandoutput2" requires explicit package name at telnet0.pl line 24.
    Global symbol "@commandoutput2" requires explicit package name at telnet0.pl line 25.
    
    poj

      i have added the log and found the following in log, we can see that it is coming as "--More--" and stopping and by this output is not getting captured. How to overcome this?
      the command output -

      ^MRP/0/RP0/CPU0:BLR-WFD-MPL-COR-RTR-42-236#show isis neighbors
      ^MFri Feb 23 18:28:48.355 IST

      IS-IS IGP neighbors:
      System Id Interface SNPA State Holdtime Type IETF-NSF

      IS-IS CORE neighbors:
      System Id Interface SNPA State Holdtime Type IETF-NSF
      BPL-CPT-MPL-COR-RTR-42-240 BE4 *PtoP* Up 22 L2 Capable
      MYS-MPL-LTE-PE-RTR-227 BE10 *PtoP* Up 24 L2 Capable
      HYD-UPP-MPL-COR-RTR-42-234 BE5 *PtoP* Up 28 L2 Capable
      KOL-GBS-MPL-COR-RTR-42-238 BE6 *PtoP* Up 28 L2 Capable
      CHN-SAN-MPL-COR-RTR-42-216 BE3 *PtoP* Up 25 L2 Capable
      --More--

        Maybe your device vendor has an option to disable the prompt for another keypress?

Re: Net::Telnet issue
by thanos1983 (Parson) on Feb 23, 2018 at 12:39 UTC

    Hello again vasanth.easyrider,

    Do not open new questions when a few minutes ago you have an active question with the same question Telnet Error - Vasanth. Fellow Monks and me are trying to get more information from you by commending on your question and you did not reply to them yet. How can we help you if you do not reply on the comments. Solution is not to open a new question because most likely you will receive the same questions/comments.

    Hope this helps. BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!