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


In reply to Net::Telnet - open session if not already opened by isaac737

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.