kiwi_chris has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Output to File from Telnet
by Anonymous Monk on Feb 24, 2011 at 11:24 UTC | |
by kiwi_chris (Initiate) on Feb 25, 2011 at 02:53 UTC | |
|
Re: Output to File from Telnet
by roboticus (Chancellor) on Feb 24, 2011 at 23:59 UTC | |
|
Re: Output to File from Telnet
by roboticus (Chancellor) on Feb 25, 2011 at 00:04 UTC | |
by kiwi_chris (Initiate) on Feb 25, 2011 at 02:54 UTC |