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:

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.

my $continue = 1; my %t; # Hash to hold telnet connection objects # start of data collection loop that starts every X minutes while ($continue) { #New ForEach Loop to iterate through Router Hash Table foreach $router (sort (keys (%MSC_Routers))) { my $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"; if (!exists $t{$router}) { my $input_log = $router."_input_log"; my $dump_log = $router."_dump_log"; #Open Telnet Session $t{$router} = new Net::Telnet (Timeout => 10, input_log => "$input_log", dump_log => "$dump_log", prompt => '/#$/', Errmode => $mode ); } my $test_cmd = "ls"; if (!$t{$router}->put($test_cmd)) { # test if connection is op +en $t{$router}->close; $t{$router}->open("$ip"); } # Issue desired commands here } # end foreach $router loop } # end of while loop

In reply to Re: Net::Telnet - open session if not already opened by dasgar
in thread 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.