Hello!!! I am new to perl, enjoying it but having a few problems.
I am trying to create a program which telnets to a bunch of routers / switches, runs some commands and then outputs to a text file. I currently have the program reading the logon info from an excel doc. Should be simple?...
My problem is that when a telnet session fails the program stops. What I want it to do is Output to the user that it has failed and then carry on to the next router/switch in the excel doc. Is there something that the telnet outputs that I can use to recognise it has failed? Or maybe if it hasn't done anything for 10 seconds go to next item in the for loop.
Thanks for you help :)

#!/usr/bin/perl require Net::Cisco; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; my $Router_Ip; my $Router_User; my $Router_Logon; my $Router_Enable; my $Row; my $Col; my $NumDevices = &promptUser("Amount of devices to Telnet to?"); print ("Telneting to $NumDevices devices \n"); $Win32::OLE::Warn = 3; # die on errors... # get already active Excel application or open new my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # open Excel file my $Book = $Excel->Workbooks->Open("c:/SCRIPTS/data.xlsx"); # select worksheet number 1 my $Sheet = $Book->Worksheets(1); #Read the excel spreadsheet and telnet to devices for ($i=1; $i<=$NumDevices; $i++ ){ $Row = $i; &readExcel(); &telnetRouter(); } # close excel doc $Book->Close; #------------------------------------------------# # SUB ROUTINES # #------------------------------------------------# # Read Excel doc and set Router Vars sub readExcel() { foreach my $Col (1..4){ use Switch; switch ($Col) { case 1 {$Router_Ip =$Sheet->Cells($Row,$Col)->{'Value'}} case 2 {$Router_User =$Sheet->Cells($Row,$Col)->{'Value'}} case 3 {$Router_Logon =$Sheet->Cells($Row,$Col)->{'Value'}} case 4 {$Router_Enable = $Sheet->Cells($Row,$Col)->{'Valu +e'}} } # skip empty cells next unless defined $Sheet->Cells($Row,$Col)->{'Value'}; $Sheet->Cells($Row,$Col)->{'Value'}, $Sheet->Cells($Row,$Col)->{'Formula'}; } } sub telnetRouter() { print ("\nAbout to telnet to Router:\nIP: $Router_Ip using:\nUN: + $Router_User \nPW: $Router_Logon \nEN: $Router_Enable \n"); my $session = Net::Telnet::Cisco->new(Host => $Router_Ip , Inp +ut_log =>"c:/SCRIPTS/$Router_Ip.log"); $session->login($Router_User, $Router_Logon); if ($session->enable($Router_Enable)){ @output = $session->cmd('show run | include username'); print ("Finished $Router_Ip Router Command \n"); } else { warn `Can’t enable:`. $session -> errmsg; } $session -> close; } sub promptUser { local($promptString,$defaultValue) = @_; if ($defaultValue) { print $promptString, "[", $defaultValue, "]: "; } else { print $promptString, ": "; } $| = 1; # force a flush after our print $_ = <STDIN>; # get the input from STDIN (presumably the ke +yboard) chomp; if ($defaultValue) { return $_ ? $_ : $defaultValue; # return $_ if it has a value } else { return $_; } }

In reply to Telnet Question: by kiwi_chris

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.