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;
  • Comment on Script to telnet to different cisco devices, but timed out at a particular statement.
  • Download Code

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
    You have not given us sufficient information to be able to assist you.

    The description "cmd_previlege function's first command that is executed" is not precise enough.
    Is it failing at the session prompt request ?
    How do you know ? (i.e. show us the relevant output, and the log file contents).

    Finally, please use the strict and warnings pragmata.

    Use the perl debugger "perl -d" to identify exactly where the failure occurs, and examine ("x") the values sent and returned, prior to the "failure".

            Profanity is the one language all programmers know best.

      Hi,

      Script is executing and once it enters the Function "cmd_previlege", it fails there saying "command timed-out at ***.pl line 281 <-- this is nothing but the line ### @outputforlog = $session->cmd(String => 'show ip ospf neighbor det | in Neighbor | in up', Prompt => '/\#2#/'); ####

      Is it failing at the session prompt request ? -- It is logging into device and executing the command ---> ##### @outputforlog = $session->cmd(String => 'show ip ospf neighbor det | in Neighbor | in up', Prompt => '/\#2#/'); ### from cmd_previlege funtion, then it fails. It doesnt execute further. I came to know of this through log files.

      Snippet of in.log files

      $ cat in.log

      #############################################################################

      #############################################################################

      Warning! Only Network Administrator can log in this router!

      Unauthorized access can be punished by law

      Jun Won, Lee : 02-3415-6757

      Mi Sun, Jeong : 02-3415-6727

      #############################################################################

      username:to27

      password:

      ------------------------------

      6F X03 PWR#1(6K) - UPS 500-1A * 2

      PWR#2(6K) - UPS 500-1B * 2

      ------------------------------ SES_Main#2#show ip ospf neighbor det | in Neighbor | in up

      Neighbor 10.21.157.2, interface address 10.21.157.194

      Neighbor priority is 0, State is FULL, 6 state changes

      Neighbor is up for 7w6d

      Neighbor 10.22.46.2, interface address 10.22.46.194

      Neighbor priority is 0, State is FULL, 6 state changes

      Neighbor is up for 7w6d

      .

      .

      .

      Neighbor 123.11.4.2, interface address 10.20.3.25

      Neighbor priority is 1, State is FULL, 6 state changes

      Neighbor is up for 7w3d

      Neighbor 193.9.20.4, interface address 193.9.14.2

      Neighbor priority is 1, State is FULL, 6 state changes

      Neighbor is up for 11w3d

      Neighbor 193.9.16.4, interface address 193.9.15.6

      Neighbor priority is 1, State is FULL, 6 state changes

      Neighbor is up for 11w3d

      SES_Main#2#

      Snippet of out.log files

      $ cat out.log

      to27

      ********

      show ip ospf neighbor det | in Neighbor | in up

      Snippet of dump.log files

      $ cat dump.log

      < 0x000a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x000b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x000c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x000d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x000e0: 23 23 23 23 23 0d 0a 0d 0a 20 20 20 20 20 20 20 #####....

      < 0x000f0: 57 61 72 6e 69 6e 67 21 20 4f 6e 6c 79 20 4e 65 Warning! Only Ne

      < 0x00100: 74 77 6f 72 6b 20 41 64 6d 69 6e 69 73 74 72 61 twork Administra

      < 0x00110: 74 6f 72 20 63 61 6e 20 6c 6f 67 20 69 6e 20 74 tor can log in t

      < 0x00120: 68 69 73 20 72 6f 75 74 65 72 21 0d 0a 0d 0a 20 his router!....

      < 0x00130: 20 20 20 20 20 20 20 20 20 20 20 55 6e 61 75 74 Unaut

      < 0x00140: 68 6f 72 69 7a 65 64 20 61 63 63 65 73 73 20 63 horized access c

      < 0x00150: 61 6e 20 62 65 20 70 75 6e 69 73 68 65 64 20 62 an be punished b

      < 0x00160: 79 20 6c 61 77 0d 0a 0d 0a 20 20 20 20 20 20 20 y law....

      < 0x00170: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 61 Sa

      < 0x00180: 6d 73 75 6e 67 20 4e 65 74 77 6f 72 6b 73 20 53 msung Networks S

      < 0x00190: 45 20 33 74 68 20 54 65 61 6d 0d 0a 0d 0a 20 20 E 3th Team....

      < 0x001a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

      < 0x001b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

      < 0x001c0: 20 20 20 20 20 20 4a 75 6e 20 57 6f 6e 2c 20 4c Jun Won. L

      < 0x001d0: 65 65 20 20 20 20 20 20 3a 20 30 32 2d 33 34 31 ee : 02-341

      < 0x001e0: 35 2d 36 37 35 37 0d 0a 20 20 20 20 20 20 20 20 5-6757..

      < 0x001f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

      < 0x00200: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

      < 0x00210: 4d 69 20 53 75 6e 2c 20 4a 65 6f 6e 67 20 20 20 Mi Sun. Jeong

      < 0x00220: 20 20 3a 20 30 32 2d 33 34 31 35 2d 36 37 32 37 : 02-3415-6727

      < 0x00230: 0d 0a 0d 0a 23 23 23 23 23 23 23 23 23 23 23 23 ....############

      < 0x00240: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x00250: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x00260: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x00270: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ################

      < 0x00280: 23 0d 0a 75 73 65 72 6e 61 6d 65 3a #..username:

      > 0x00000: ff fd 01 ff fd 03 ff fc 18 ff fc 1f ¢Æ¢Æ.¢Æ¢Æ.¢Æ¢Æ.¢Æ¢Æ.

      > 0x00000: 74 6f 32 37 0d 0a to27..

      < 0x00000: ff fe 18 ff fe 1f 74 6f 32 37 0d 0a 70 61 73 73 ¢Æ¢Æ.¢Æ¢Æ.to27..pass

      < 0x00010: 77 6f 72 64 3a word:

      > 0x00000: 68 6f 6d 65 70 6c 75 73 32 37 0d 0a ********..

      < 0x00000: 0d 0a 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ....------------

      < 0x00010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------

      < 0x00020: 2d 2d 0d 0a 20 20 36 46 20 58 30 33 0d 0a 0a 20 --.. 6F X03...

      < 0x00030: 20 50 57 52 23 31 28 36 4b 29 20 2d 20 55 50 53 PWR#1(6K) - UPS

      < 0x00040: 20 35 30 30 2d 31 41 20 2a 20 32 0d 0a 20 20 50 500-1A * 2.. P

      < 0x00050: 57 52 23 32 28 36 4b 29 20 2d 20 55 50 53 20 35 WR#2(6K) - UPS 5

      < 0x00060: 30 30 2d 31 42 20 2a 20 32 0d 0a 2d 2d 2d 2d 2d 00-1B * 2..-----

      < 0x00070: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------

      < 0x00080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 0d 0a 53 45 53 ---------....SES

      < 0x00090: 5f 4d 61 69 6e 23 32 23 _Main#2#

      > 0x00000: 73 68 6f 77 20 69 70 20 6f 73 70 66 20 6e 65 69 show ip ospf nei

      > 0x00010: 67 68 62 6f 72 20 64 65 74 20 7c 20 69 6e 20 4e ghbor det | in N

      > 0x00020: 65 69 67 68 62 6f 72 20 7c 20 69 6e 20 75 70 0d eighbor | in up.

      > 0x00030: 0a .

      .

      .

      .

      .

      .

      .

      < 0x002f0: 20 20 20 20 4e 65 69 67 68 62 6f 72 20 70 72 69 Neighbor pri

      < 0x00300: 6f 72 69 74 79 20 69 73 20 31 2c 20 53 74 61 74 ority is 1. Stat

      < 0x00310: 65 20 69 73 20 46 55 4c 4c 2c 20 36 20 73 74 61 e is FULL. 6 sta

      < 0x00320: 74 65 20 63 68 61 6e 67 65 73 0d 0a 20 20 20 20 te changes..

      < 0x00330: 4e 65 69 67 68 62 6f 72 20 69 73 20 75 70 20 66 Neighbor is up f

      < 0x00340: 6f 72 20 31 31 77 33 64 20 20 20 0d 0a 53 45 53 or 11w3d ..SES

      < 0x00350: 5f 4d 61 69 6e 23 32 23 _Main#2#

      Could you please suggest me a regex for the prompt with below hostnames with previlege mode access?

      1. SES_Main#1

      2. SES_Main#2

      3. Tesco_main#1

      4. Tesco_Main#2

      I tried regex as "'/(?m:^(?:\w.\/+\:)?\w.-+\s?(?:\(config^\)*\))?\s?\$#>\s?(?:\(enable\))?\s*$)/'". But no luck.

        A few suggestions:
        • Set 'terminal length 0', to avoid paging (See the doc in Net::Telnet::Cisco )
        • Remove the "/" in the prompt regex, and try something simple like "\b\w+#\d\b"
        • Did you set a "Timeout" for the session response ? How long does it take the device to respond to the ospf request ?
        • You do not show what the STDOUT contains - for instance, do you get the "prmot" output before the ospf request is sent?

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

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

    You need more subroutines, subroutines that don't work on global variables , but subroutines that take arguments, return values, die on error

    When you write a bunch of times (like 3)

    open $logfile3... foreach(@output3)... print $logfile3... close $logfile3;

    You need to turn that into a subroutine ... or use Path::Tiny

    use Path::Tiny qw/ path /; path( $logfile1 )->spew( @output1 ); path( $logfile2 )->spew( @output2 ); path( $logfile3 )->spew( @output3 );