Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Net::Telnet::Cisco Using a Global Session

by spickles (Scribe)
on Jul 28, 2010 at 21:17 UTC ( [id://851807]=note: print w/replies, xml ) Need Help??


in reply to Re: Net::Telnet::Cisco Using a Global Session
in thread Net::Telnet::Cisco Using a Global Session

Here is some example code. Unless you have a Cisco controller, you can't actually run this and get meaningful output. The script completes, but the output to the various logs is all wrong. I think I need to find a way to wait for the prompt again so that it slows things down. I think the script is happening too quickly, the log inputs/outputs are getting created before the first subroutine has ended and the output for sub get_ap_summary shows up in the output log for show_inventory. The prompt is '(Cisco Controller) >', and I thought I set that in the properties of the session object, but perhaps the regex doesn't match? I thought if I changed the prompt in the session object that it would always wait (IOW, I thought the default setting is 'always wait for prompt').

#!c:/perl/bin/perl use strict; use warnings; use Net::Telnet::Cisco; use DBI; use DBD::mysql; use Env; use File::Path; use File::Basename; use IO::Handle; use Config::IniFiles; use Term::ReadKey; ###################################################################### +################################## # Global Variables ################################################### +################################## ###################################################################### +################################## my $username = "admin"; my $password = "admin"; my $host = "172.16.0.2"; my @output; my $input_log; my $output_log; my $directory = "c:/perl/bin/test/"; #Check for the presence of the log file directory. If not there, crea +te it eval { mkpath($directory); if ($@) { print "Couldn't create path: $@"; } else { mkpath($directory); } }; ###################################################################### +################################## # Global Variables ################################################### +################################## ###################################################################### +################################## ###################################################################### +################################## # Controller Connection ############################################## +################################## ###################################################################### +################################## system "cls"; print "Connecting to controller ...\n"; my $session = Net::Telnet::Cisco->new( Host => $host, Prompt => '/\(Cisco Controller\)/', Errmode => 'return', Timeout => 5); $session->open($host); # Wait for the username prompt and enter username $session->waitfor('/^User:.*$/'); $session->print($username); # Wait for the password prompt and enter password $session->waitfor('/^Password:.*$/'); $session->print($password); $session->cmd("config paging disable"); ###################################################################### +################################## # Controller Connection ############################################## +################################## ###################################################################### +################################## get_ap_summary(); show_run(); show_inventory(); cleanup(); exit(); ###################################################################### +################################## # Subroutines ######################################################## +################################## ###################################################################### +################################## sub get_ap_summary { print "\nGetting AP Summary ...\n"; #my @output; $input_log = $directory . $host . "_get_ap_summary_in.log"; $output_log = $directory . $host . "_get_ap_summary_out.log"; $session->input_log($output_log); $session->output_log($input_log); @output = $session->cmd("show ap summary"); return; } sub show_run { print "\nGetting Running Config ...\n"; #my @output; $input_log = $directory . $host . "_get_run_config_in.log"; $output_log = $directory . $host . "_get_run_config_out.log"; $session->input_log($output_log); $session->output_log($input_log); @output = $session->cmd("show run-config"); return; } sub show_inventory { print "\nGetting inventory ...\n"; #my @output; $input_log = $directory . $host . "_get_inventory_in.log"; $output_log = $directory . $host . "_get_inventory_out.log"; $session->input_log($output_log); $session->output_log($input_log); @output = $session->cmd("show inventory"); return; } sub cleanup { print "\nClosing telnet connection ...\n"; $session->cmd("config paging enable"); $session->close; print "FINISHED\n"; } ###################################################################### +################################## # Subroutines ######################################################## +################################## ###################################################################### +##################################

Replies are listed 'Best First'.
Re^3: Net::Telnet::Cisco Using a Global Session
by spickles (Scribe) on Jul 29, 2010 at 01:20 UTC

    I think the issue might be that the log files are not actually written to disk until the session is closed. I'll have to test it, but if I'm right, it's going to be a pain to write my own logging function. Furthermore, I don't know of any other way to capture the output from the session. I've looked extensively at redirecting the session output, particularly to STDOUT, with no success.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-24 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found