Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Net::Telnet::Cisco Using a Global Session

by spickles (Scribe)
on Jul 28, 2010 at 19:46 UTC ( [id://851776]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that is broken up into various subroutines. One version is very slow and stable in which I open and close the telnet session in each sub when I need to. I am now trying to avoid that and speed things up by declaring the session early on (before subs) so as to be global. I currently have a global connection to my MySQL database and I can use it inside and outside subs with no problem. When I run my script using the global telnet session, however, it crashes on the first sub that uses the session. I have tried passing the session into the sub and returning it back out as well with no success. Can anyone think of a reason as to why the global db connection is working fine but not a global telnet session?

<snip> my $host = "172.16.0.2"; 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"); <snip> #Inside subroutine my @output; my $input_log = $directory . "/" . $host . "_get_ap_summary_in.log"; my $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");
Regards,
Scott

Replies are listed 'Best First'.
Re: Net::Telnet::Cisco Using a Global Session
by Corion (Patriarch) on Jul 28, 2010 at 20:09 UTC

    Where does it crash? How does it crash? What is the input/output? Does the presence of the database have any bearing? What is the minimal script that still exhibits the behaviour but is shorter than 20 lines?

    By answering these questions, you can help us to help you better. We have little to no knowledge of your existing program and if you provide information in a way that's easy to digest it makes it much likelier that we can look at your problem successfully.

      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 ######################################################## +################################## ###################################################################### +##################################

        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.

      Yeah, I know I'm not being a team player without a lot of code, but it's proprietary and I can't release it. I'd have to try and create some generic code that does the same thing and then post that. Probably later tonight.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found