Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Net::Telnet - open session if not already opened

by isaac737 (Initiate)
on Sep 08, 2011 at 22:24 UTC ( [id://924963]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Monks,

I have written a script that telnets into a series of routers every minute to collect some data and then it properly closes the session out. Well, it seems as though the routers' RPM doesn't like being telnetted to every minute and after a period of time the router is no longer able to allocate memory. We think it is because of the script and the fact that the code on the router is rather sucky.

So, my question is how can I tell the Perl Net::Telnet module to *only* open a session if one is not open already. We would like to keep a session open all the time and just issue the command once a minute. The script would only open a new session if the existing one gets torn down for whatever reason.

Here is what I have so far:

#New ForEach Loop to iterate through Router Hash Table foreach $router (sort (keys (%MSC_Routers))) { $ip = $MSC_Routers{$router}; print ("$router has IP: of $ip\n\n"); #Print out which one I am connecting to print ("\t\t I am telnetting to: $ip \n\n"); sleep 3; $mode = "return"; #Open Telnet Session $t = new Net::Telnet (Timeout => 10, input_log => "input_log", dump_log => "dump_log", prompt => '/#$/', Errmode => $mode ); $t->open("$ip");

That works... what I guess I am after is some sort of if statement that says: if (Telnet Session is not open) { open session } else { proceed to rest of code }

Any thoughts would be much appreciated. Thanks! -Jg

Replies are listed 'Best First'.
Re: Net::Telnet - open session if not already opened
by dasgar (Priest) on Sep 09, 2011 at 05:25 UTC

    First, I should point out that it looks like you might not be using use strict; in your code. If that is the case, I'd recommend adding that as well as use warnings; to your code.

    Here's how I'd approach the modification:

    • Declare the $t variable as a hash outside of the foreach loop
    • For each key in your main hash, you'll be creating that key in the %t hash
    • Inside the foreach loop, do the following:
      • Check to see if the key (connection object) exists. If not, create it with unique logs.
      • Issue a command to the connection object. If there's an error, close the connection and reopen it.
      • Issue the commands that you need to do

    Below is my untested modification of your code to do the above suggestions. You'll need to modify the test command to be something that makes sense for your situation.

      Dasgar! Thanks so much for this solution. I have beta tested it and it seems to be just what we were after.

      The key line is seeing whether or not the object is instantiated or not and I wasn't quite sure how to do that. if (!exists $t{$router}) { seems to do the trick. Oh and I do run with perl -w but I don't use strict because I can't see the purpose of it and it makes writing perl a pain. It has something to do with scope right?

      Anyhow, cheers! - Jg

      Hello, can any one told me that how can i change this script with my ip address and port number... what is this line "foreach $router (sort (keys (%MSC_Routers))) {"
Re: Net::Telnet - open session if not already opened
by isaac737 (Initiate) on Sep 08, 2011 at 22:31 UTC

    Hi, Just a quick follow up here... If I tell the Perl script to print $t after opening the Telnet session I get this printed out:

    Net::Telnet=GLOB(0x10092110)

    The Hex code I guess changes each time I run the script: Net::Telnet=GLOB(0x1cf82110)

    Any insight on how best to use that in an if statement to see if there is a pre-existing Telnet session open or not? Cheers! - Jg

      The Hex code I guess changes each time I run the script: Net::Telnet=GLOB(0x1cf82110)

      Sure it does. Net::Telnet is an object, and its methods are documented in Net::Telnet. You might be interested in the eof and timed_out methods.

      The documentation also states:

      If you have the IO:: libraries installed (they come standard with perl5.004 and later) then IO::Socket::INET is used as a base class, otherwise FileHandle is used.

      So, it is very likely that Net::Telnet inherits some more methods from IO::Socket::INET, which inherits from IO::Socket, which inherits from IO::Handle.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found