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

Hi Perl monks I have an issue. I have a command to be executed on remote server. I am using Net::Telnet module to connect to remote server. But the command output is not getting captured, not sure why. Can you please help me -
command to be executed - "show isis neighbors"
The code is -
#!/usr/bin/perl use strict; use warnings; use DBI; use Switch; use Net::Telnet(); my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; #reading the hostname, IP address and credentials in a csv file gi +ven as an argument my $sum = 0; my ($host,$hostname,$username, $passwd, $Eleprompt,$t,$string,$match,$ +output,@output); open(my $data, '<', $file) or die "Could not open '$file' $!\n"; my $i=1; while(my $line = <$data>) { chomp $line; my @fields = split "," , $line; $host = $fields[0]; $hostname = $fields[1]; $username = $fields[2]; $passwd = $fields[3]; $Eleprompt = $fields[4]; print "IP of device $i = $host\n"; print "Hostname of device $i = $hostname\n"; print "Username of device $i = $username\n"; print "password of device $i = $passwd\n"; print "login prompt of device $i = $Eleprompt\n"; eval { if($t = new Net::Telnet (Timeout => 20, Prompt => $Ele +prompt)) { 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"; $string = "show isis neighbors"; $match = $Eleprompt; @output = $t->cmd("admin show environment leds"); + #String => $string,Prompt => $match); print "output is = @output\n"; @output = $t->cmd("show running-config lpts punt exces +sive-flow-trap"); #String => $string,Prompt => $ +match); print "output is = @output\n"; @output = $t->cmd(String => $string,Prompt => $matc +h,Errmode => die); #String => $string,Prompt => +$match); print "output is = @output\n"; };

Sample output of the command -
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 BLR-MPL-LTE-PE-RTR-211 BE12 *PtoP* Up 23 L2 Capable BLR-WFD-MPL-COR-RTR-42-236 BE1 *PtoP* Up 23 L2 Capable NDL-OKH-MPL-COR-RTR-42-206 BE2 *PtoP* Up 21 L2 Capable BLL-MPL-LTE-PE-RTR-42-55 BE9 *PtoP* Up 22 L2 Capable

Replies are listed 'Best First'.
Re: Net::Telnet not capturing output
by hippo (Archbishop) on Feb 23, 2018 at 08:52 UTC

    There's probably something wrong with your code, vasanth.easyrider. However, since you have not shown your code that's about all one can say. If you do decide to share the non-capturing code be sure to enclose it in <code> tags and it pays to do the same for any relevant data.

    Update: You have edited your post to include the code now. See How do I change/delete my post? for how to mark it so. The code provided doesn't compile, which isn't a great start. It also has use Switch; without further reference to this module. I would remove that if you aren't using it - see Short, Self-Contained, Correct Example for how to present a good piece of sample code.

    What is the actual error condition? ie. what do you actually see as output compared to what you expect? Here is an SSCCE to get you started:

    #!/usr/bin/env perl use strict; use warnings; use Net::Telnet; my $nt = Net::Telnet->new (Host => 'www.perlmonks.org', Port => 80, Ti +meout => 20); $nt->prompt ('/<\/html>/'); my @output = $nt->cmd ("GET / HTTP/1.1\nHost: www.perlmonks.org\n\n"); print "Here are the HTTP headers:\n\n"; for my $line (@output) { print $line; last if length ($line) < 3; } print "End of headers.\n"; $nt->close; exit;

    See how the output is captured and manipulated? How does that differ from your own approach?

      i have pasted the code. Can you check and suggest me

Re: Telnet Error - Vasanth
by thanos1983 (Parson) on Feb 23, 2018 at 09:42 UTC

    Hello vasanth.easyrider,

    Unfortunately I can not test your code on my local OS due to some configurations that I have applied. So at this point I can only ask questions and try to understand why your code is not working. On your sample of code you have multiple commands, any of them are working or all are failing? Also why Net::Telnet and not Net::OpenSSH.

    SSH is much more secure connection. A few years ago I was experimenting with module and I wrote this question Best module to execute administrator commands on external operating systems (Linux) which contains sample of code for all the modules.

    Looking forward to your reply.

    BR / Thanos

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

      yes i am executing many commands. For few commands it is working fine and for few it is not working fine. I observed that, if the command output has empty lines, it is failing. Can you help me on how to overcome this?

        if the command output has empty lines, it is failing

        What is the value of $fields[4] and/or $Eleprompt and/or $match?

      If i use Net::OpenSSH, how to tackle "SSH connection: the authenticity of the target" errors that we get when we use Net::OpenSSH. Can we manage authenticity in script itself ??

        Hello again vasanth.easyrider,

        What do you mean by:

        how to tackle "SSH connection: the authenticity of the target" errors that we get when we use Net::OpenSSH

        Net::OpenSSH handles authentication, I assume you mean password_prompt. If this is the case yes the module can support that read more here Net::OpenSSH methods.

        Hope this helps. BR / Thanos

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