HELLO~! I am reasonably new to Perl and need some help to write a TELNET script to logon to Cisco router and then output the session to a text file.
At the moment my script reads an excel file for the logon details and it then begin to telnet to the MULTIPLE cisco routers. I want the script to telnet to the router, run commands, and then output the telnet session screen to a separate text file.
My script is screwed somehow and I am pulling my hair out! HELP Please.
1)After the first telnet session router it doesn't output any further sessions.
2)ALSO if a telnet session fails it doesn't skip that one and move on. It just end the script.

#!/usr/bin/perl require Net::Cisco; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; my $routerip; # router ip address my $routerun; #logon username my $routerlgnpwd; #logon password my $routerenpwd; #enable password $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 (you can also select a worksheet by name) my $Sheet = $Book->Worksheets(1); #Read the excel spread sheet for logon details foreach my $row (1..2){ #ROWS This is for a spread sheet with 2 ro +ws and 4 columns foreach my $col (1..4){ #read row column by column use Switch; switch ($col) { #set values for telnet logon for excel row case 1 {$routerip =$Sheet->Cells($row,$col)->{'Value'}} case 2 {$routerun =$Sheet->Cells($row,$col)->{'Value'}} case 3 {$routerlgnpwd =$Sheet->Cells($row,$col)->{'Value'}} case 4 {$routerenpwd = $Sheet->Cells($row,$col)->{'Value' +}} }#//switch # skip empty cells next unless defined $Sheet->Cells($row,$col)->{'Value'}; # print out the contents of a cell # printf "%s ", $Sheet->Cells($row,$col)->{'Value'}, $Sheet->Cells($row,$col)->{'Formula'}; }#// For Column print ("\nAbout to telnet to Router:\nIP: $routerip using:\nUN: +$routerun \nPW: $routerenpwd \nEN: $routerlgnpwd \n"); # Log on to Router and perform commands output label the rout +er ip. my $session = Net::Telnet::Cisco->new(Host => $routerip , Input_lo +g =>"c:/SCRIPTS/$routerip.log"); $session->login($routerun, $routerpwd); if ($session->enable($en)){ #'cpx270zby9' @output = $session->cmd('show run | include username'); print ("Finished $routerip Router Command \n"); } else { warn `Can’t enable:`. $session -> errmsg; } $session -> close; }#//For Row $Book->Close; # close excel doc

In reply to Output to File from Telnet 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.