Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Net::Telnet issue getting command output

by gballanti (Initiate)
on Feb 12, 2016 at 08:20 UTC ( #1155051=perlquestion: print w/replies, xml ) Need Help??

gballanti has asked for the wisdom of the Perl Monks concerning the following question:

Hello expert, I'm facing with a strange behaviour from Net::Telnet trying to get the command output from a Cisco device.
The result from "show run" command is placed into a scalar but printing the contents I get a number.
The dump log contests the right output.
Below the simple code that I'm using.

#/opt/ActivePerl-5.18/bin/perl use Net::Telnet; eval { $t = new Net::Telnet ( Timeout=>10, Errmode=> 'die', Prompt=> '/[# +>] $/' ); $t->open("x.x.x.x"); }; if($@) { print "error: " . $@ . "\n"; } else { print "success: " . $@ . "\n"; } $t->dump_log("file_dump"); $t->output_log("file_out"); #$t->max_buffer_length(50*1024*1024); $t->waitfor('/Username: ?$/i'); $t->print("*****"); $t->waitfor('/Password: ?$/i'); $t->print("*****"); $t->waitfor(-match=> '/> ?$/', -errmode=> "return") or die "login fail +ed: ", $t->lastline; $t->print("en"); $t->waitfor('/Password: ?$/i'); $t->print("*****"); $t->waitfor(-match=> '/# ?$/', -errmode=> "return") or die "login enab +le failed: ", $t->lastline; #$t->cmd(-string=> 'terminal pager 0', -timeout=> 30, -prompt=> '/# $/ +'); #$t->waitfor(-match=> '/# $/', -errmode=> "return") or die "prompt not + returned: ", $t->lastline; #@lines=$t->cmd(-string=> 'show run', -timeout=> 30, -prompt=> '/# $/' +); $t->print('terminal pager 0'); $t->waitfor('/# $/'); @showrun=$t->cmd('show run'); print @showrun . "\n"; print "moving to next statement\n";

running the script I get:

success:
2280
moving to next statement

Anyone can help me ?
Thanks

**************************************************************

I resolved, don't understand why but the command
print @showrun . "\n";
prints the numbers of element into the array.
The following code show the array contents:
print("@showrun\n");

Replies are listed 'Best First'.
Re: Net::Telnet issue getting command output
by RichardK (Parson) on Feb 12, 2016 at 08:49 UTC

    The string concatination operator . has forced the array into scalar context, and so the list returns it's length not its content.

    Try runing this to see some different ways of doing it

    my @tmp = (1,2,3); print @tmp; print "\n"; print @tmp , "\n"; print @tmp . "\n"; print "@tmp" , "\n"; print join("\n" , @tmp);

      Hello Richard, thank you for the answer
      you have reason using the concatenation operator "." how I did
      Is not a good way to have array printed.
      greetings

Re: Net::Telnet issue getting command output
by VinsWorldcom (Prior) on Feb 12, 2016 at 14:02 UTC

    On a side note, you could use Net::Telnet::Cisco to greatly simplify your script with things like login() and a Cisco-like-prompt string so don't need any of the waitfor() commands.

    #!perl use strict; use warnings; use Net::Telnet::Cisco; my $t = Net::Telnet::Cisco->new( Host => "x.x.x.x", dump_log => "file_dump", output_log => "file_out" ) or die "no connection\n"; $t->login( Name => "username_goes_here", Password => "password_goes_here" ) or die "no login\n"; $t->enable("enable_pass_goes_here") or die "no enable\n"; $t->cmd('terminal length 0'); my @showrun = $t->cmd('show run'); print $_ for ( @showrun ); print "\nmoving to next statement\n";
Re: Net::Telnet issue getting command output
by Anonymous Monk on Nov 22, 2017 at 12:41 UTC
    I am upping this post which made my day today. Please allow keywords /Net::Telnet/ /Nagios::Monitoring/ /as400/ be used for other novices future reference.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2020-07-10 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?