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
    You're using wordpad aren't you?

    In perl, backticks qx aka `` are used to execute a program and capture the output, observe

    $ perl -e " warn `Can’t enable:` " 'Can’t' is not recognized as an internal or external command, operable program or batch file. Warning: something's wrong at -e line 1.
    Also, you need to get yourself a good code [editor/IDE, gvim_portable, http://padre.perlide.org/,

    Also, self documenting variable names are better than comments

    my $RouterIP; my $RouterUsername; my $RouterPassword; my $RouterPasswordEnabled;

      I have installed Strawberry Padre and it is great thanks!

Re: Output to File from Telnet
by roboticus (Chancellor) on Feb 24, 2011 at 23:59 UTC

    kiwi_chris:

    I think you're making things harder than they need to be. For example, you've got:

    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

    and I think you'd really be better off with:

    foreach my $row (1..2) { $routerip = $Sheet->Cells($row,1)->{Value}; $routerun = $Sheet->Cells($row,2)->{Value}; $routerlgnpwd = $Sheet->Cells($row,3)->{Value}; $routerenpwd = $Sheet->Cells($row,4)->{Value}; }

    You're also making the code harder to read with your comments, rather than easier to read. It would probably be a lot easier to fix if you cleaned up the indentation and remove most of the comments.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Output to File from Telnet
by roboticus (Chancellor) on Feb 25, 2011 at 00:04 UTC

    kiwi_chris:

    Oh, yeah, one more thing. Your code that processes the sessions is outside of your for loop, so it's only processing the last item in your spreadsheet. You'd see it more easily if you clean up the indentation.

    ...roboticus

    When your only tool is Switch, all problems look like they went through a source filter.

      Roboticus your suggestions have been EXTREMELY helpful Thank you for your time!