#!/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)->{'Value'}} } # 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 , Input_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 $_ = ; # get the input from STDIN (presumably the keyboard) chomp; if ($defaultValue) { return $_ ? $_ : $defaultValue; # return $_ if it has a value } else { return $_; } }