bks has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I'm trying to telnet to different cisco devices and executing some commands and prepare a excel report
Script is getting timed out in cmd_previlege function's first command that is executed, not sure if this is because of Prompt?. Please guide me.
Please find below the device hostname and logging into previlege mode (#) as prompt but may be hostname is causing an issue and unable to login into devices
1. SES_Main#1
2. SES_Main#2
3. Tesco_main#1
4. Tesco_Main#2
Code is as below
sub cmd_previlege { my $matchprompt = $session->last_prompt; print "prmot $matchprompt\n"; @outputforlog = $session->cmd(String => 'show ip ospf neighbor + det | in Neighbor | in up', Prompt => '/\#2#/'); print "Out for log @outputforlog\n"; #$session->waitfor('/\#2#/'); @output1 = $session->cmd(String => 'show ip ospf neighbor det +| in Neighbor is up for', Prompt => '/\#2#/'); print "output1 is @output1\n"; @output3 = $session->cmd(String => 'show version | inc uptime' +, Prompt => '/\#2#/'); print "OUtput 3 is @output2\n"; @output4 = $session->cmd(String => 'show process cpu | inc %/' +, Prompt => '/\#2#/'); print "out 4 is @output4\n"; @output5 = $session->cmd(String => 'show env', Prompt => '/\#2 +#/'); print "out 5 is @output5\n"; @output6 = $session->cmd(String => 'show memory free | incl Pr +ocessor | exclude Allocator', Prompt => '/\#2#/'); print "out 6 is @output6\n"; ### Creating log file of complete output for reference in futu +re open $logfile1, ">log_for_$ip.log" or die "Could not open file + log_for_$ip.log"; foreach(@outputforlog) { print $logfile1 $_; } print $logfile1 "\n------------------------------------------- +-------------------------\n"; close $logfile1; open $logfile3, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output3) { print $logfile3 $_; } print $logfile3 "--------------------------------------------- +-----------------------\n"; close $logfile3; open $logfile4, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output4) { print $logfile4 $_; } print $logfile4 "--------------------------------------------- +-----------------------\n"; close $logfile4; open $logfile5, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output5) { print $logfile5 $_; } print $logfile5 "--------------------------------------------- +-----------------------\n"; close $logfile5; open $logfile6, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output6) { print $logfile6 $_; } close $logfile6; ospf(); device_uptime(); env_alarm(); process_cpu(); memory_cal(); excel(); } sub cmd_normal { @outputforlog = $session->cmd('show ip ospf neighbor det | in +Neighbor | in up'); #$session->waitfor('/\#/'); @output1 = $session->cmd('show ip ospf neighbor det | in Neigh +bor is up for'); @output2 = $session->cmd('show ip bgp summary | begin Neighbor +'); @output3 = $session->cmd('show version | inc uptime'); @output4 = $session->cmd('show process cpu | inc %/'); @output5 = $session->cmd('show env'); @output6 = $session->cmd('show memory free | incl Processor | +exclude Allocator'); ### Creating log file of complete output for reference in futu +re open $logfile1, ">log_for_$ip.log" or die "Could not open file + log_for_$ip.log"; foreach(@outputforlog) { print $logfile1 $_; } print $logfile1 "\n------------------------------------------- +-------------------------\n"; close $logfile1; open $logfile2, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output2) { print $logfile2 $_; } print $logfile2 "--------------------------------------------- +-----------------------\n"; close $logfile2; open $logfile3, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output3) { print $logfile3 $_; } print $logfile3 "--------------------------------------------- +-----------------------\n"; close $logfile3; open $logfile4, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output4) { print $logfile4 $_; } print $logfile4 "--------------------------------------------- +-----------------------\n"; close $logfile4; open $logfile5, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output5) { print $logfile5 $_; } print $logfile5 "--------------------------------------------- +-----------------------\n"; close $logfile5; open $logfile6, ">>log_for_$ip.log" or die "Could not open fil +e log_for_$ip.log"; foreach(@output6) { print $logfile6 $_; } close $logfile6; ospf(); bgp(); device_uptime(); env_alarm(); process_cpu(); memory_cal(); excel(); } sub excel { $count = $count + 1; my $sheet = "sheet".$count; my $outfile = "Checklist_$ip.xls"; my $workbook1 = Spreadsheet::WriteExcel->new($outfile); $sheet = $workbook1->add_worksheet($ip); $sheet->activate(); # Set the column width for columns 1 to 7 $sheet->set_column(0, 7, 20); # Increased the cell size of the merged cells to highlight the + formatting. $sheet->set_column('B:G', 30); $sheet->set_row(2, 30); # Create a format for the column headings my $header1 = $workbook1->add_format(); $header1->set_bold(); $header1->set_size(12); $header1->set_color('black'); $header1->set_pattern(); # This is optional when using a solid + fill $header1->set_bg_color('grey'); $sheet->write(0, 0, 'DATE', $header1); $sheet->write(0, 1, 'OSPF Neighbor state',$header1); $sheet->write(0, 2, 'Memory State', $header1); $sheet->write(0, 3, 'CPU', $header1); $sheet->write(0, 4, 'Environment status', $header1); $sheet->write(0, 5, 'Device Uptime', $header1); $sheet->write(0, 6, 'BGP Status', $header1); my $dateformat = $workbook1->add_format(); $dateformat->set_size(11); $dateformat->set_color('black'); if ($ospfokay eq "Okay") { print "OSPF valure is okay hence would print in green\ +n"; $okaystatus1 = $workbook1->add_format(); $okaystatus1->set_size(11); $okaystatus1->set_color('green'); } else { print "OSPF values is not okay hence print in red\n"; $okaystatus1 = $workbook1->add_format(); $okaystatus1->set_size(11); $okaystatus1->set_color('red'); } if ($envokay eq "Okay") { print "env status is okay hence print in green\n"; $okaystatus2 = $workbook1->add_format(); $okaystatus2->set_size(11); $okaystatus2->set_color('green'); } else { print "env status is not okay hence print in red\n"; $okaystatus2 = $workbook1->add_format(); $okaystatus2->set_size(11); $okaystatus2->set_color('red'); } if ($mem_okay eq "Okay") { print "Memory status is okay hence print in green\n"; $okaystatus3 = $workbook1->add_format(); $okaystatus3->set_size(11); $okaystatus3->set_color('green'); } else { print "Memory status is not okay hence print in red\n" +; $okaystatus3 = $workbook1->add_format(); $okaystatus3->set_size(11); $okaystatus3->set_color('red'); } if ($cpu_stat eq "Okay") { print "CPU status is okay hence print in green\n"; $okaystatus4 = $workbook1->add_format(); $okaystatus4->set_size(11); $okaystatus4->set_color('green'); } else { print "CPU status is not okay hence print in red\n"; $okaystatus4 = $workbook1->add_format(); $okaystatus4->set_size(11); $okaystatus4->set_color('red'); } if ($uptime_state eq "Okay") { print "Device uptime status is okay hence print in gre +en\n"; $okaystatus5 = $workbook1->add_format(); $okaystatus5->set_size(11); $okaystatus5->set_color('green'); } else { print "Device uptime status is not okay hence print in + red\n"; $okaystatus5 = $workbook1->add_format(); $okaystatus5->set_size(11); $okaystatus5->set_color('red'); } if ($bgp_state eq "Okay") { print "Device uptime status is okay hence print in gre +en\n"; $okaystatus6 = $workbook1->add_format(); $okaystatus6->set_size(11); $okaystatus6->set_color('green'); } else { print "Device uptime status is not okay hence print in + red\n"; $okaystatus6 = $workbook1->add_format(); $okaystatus6->set_size(11); $okaystatus6->set_color('red'); } $sheet->write(1, 0, $date_ex, $dateformat); $sheet->write(1, 1, $ospfokay, $okaystatus1); $sheet->write(1, 2, $mem_okay, $okaystatus3); $sheet->write(1, 3, $cpu_stat, $okaystatus4); $sheet->write(1, 4, $envokay, $okaystatus2); $sheet->write(1, 5, $uptime_state, $okaystatus5); $sheet->write(1, 6, $bgp_state, $okaystatus6); } sub telnet { $count = 0; my $ip_1 = '*.*.*.*'; my $ip_2 = '*.*.*.*'; my $ip_3 = '*.*.*.*'; ### Connecting to device using telnet if ( $ip =~ m/$ip_1/ || $ip =~ m/$ip_2/ || $ip =~ m/$ip_3/ ) { #ReadMode 2; print "\nEnter enable password:"; $enpassword = <STDIN>; chomp $enpassword; #ReadMode 0; print "user name is $username and password is $passwor +d\n"; $session = Net::Telnet::Cisco->new(Host => $ip, Timeou +t => 10, Input_log => 'in.log', Output_log => 'out.log', Dump_log => +'dump.log'); $session->login(Name => $username, Password => $passwo +rd, Passcode => $enpassword, Prompt => '/\#2#/'); cmd_previlege(); } else { $session = Net::Telnet::Cisco->new(Host => $ip, Timeou +t => 10, Prompt => '/\>/'); $session->login($username,$password); cmd_normal(); } $session->close; } # Description : Main Function + + # my $filename = 'Device_IP_list.txt'; #Reading Device Ip from this file + and logging into each device one at a time open(my $deviceipfile, '<:encoding(UTF-8)', $filename) or die "could n +ot open the file '$filename' $!"; print "Enter Username:"; $username = <STDIN>; chomp $username; # To read password without printing onto console #ReadMode 2; print "\nEnter Password:"; $password = <STDIN>; chomp $password; #ReadMode 0; $resultospf = "ospf.txt"; $resultenv = "env_alarm.txt"; open(my $fh, "<$filename") or die "Could not open file $!"; while ($ip= <$fh>) { chomp $ip; print "\nConnecting to Device $ip.....\n"; telnet(); } close $fh;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Script to telnet to different cisco devices, but timed out at a particular statement.
by NetWallah (Canon) on Aug 12, 2014 at 14:08 UTC | |
by bks (Novice) on Aug 13, 2014 at 11:10 UTC | |
by NetWallah (Canon) on Aug 13, 2014 at 14:31 UTC | |
|
Re: Script to telnet to different cisco devices, but timed out at a particular statement.
by neilwatson (Priest) on Aug 12, 2014 at 13:32 UTC | |
|
Re: Script to telnet to different cisco devices, but timed out at a particular statement.
by Anonymous Monk on Aug 13, 2014 at 03:12 UTC |