I am trying to use perl to login to a piece of network equipment and pull some information. I then want to parse out the information i am interested in. Needless to say i am a perl hack and have absolutely no experience. I have a piece of code i have modified but am stuck on what to do next. Any help in the right direction would be greatly appreciated. I am getting the information i want out of the device fine, but parsing it is where i am having trouble. I am using Net::Telnet to query the device as you will see in the code. I hope this makes sense i would like to get rid of the extra lines that are blank.
use strict; #################### # Required Modules # # # #################### use Net::Telnet; ######## # Main # ######## my $telnet; my $ip = $ARGV[0]; my $uname = $ARGV[1]; my $passwd = $ARGV[2]; my $slot = $ARGV [3]; if (!$ARGV[0]) { print "no hostname/IP supplied\n"; die "usage:\n\n./Show_Optic.pl IP USERNAME PASSWORD slot#\n"; } elsif ((!$ARGV[1]) || (!$ARGV[2])) { print "a telnet username and password must be supplied\n"; die "usage:\n\n./Show_Optic.pl IP USERNAME PASSWORD slot#\n"; } if (!$ARGV[3]) { die "usage:\n./Show_Optic.pl IP USERNAME PASSWORD slot#\n"; } # elsif ($ARGV[3] eq "index") elsif ($ARGV[3]) { &Show_Optic; } ########################## # open a Telnet session: # # # ########################## sub Open_Telnet { $telnet = new Net::Telnet ( Host => $ip, Timeout => 5, Prompt => '/# ?$/i', Input_log=>"input.log", Dump_log=>"dump.log", Output_log=>"output.log"); $telnet->waitfor('/Username: $/i'); $telnet->print($uname); $telnet->waitfor('/Password: $/i'); $telnet->print($passwd); # print "Logged into the system \n"; $telnet->waitfor('/>/i'); $telnet->print('en'); $telnet->waitfor('/Password:$/i'); $telnet->print($passwd); $telnet->waitfor('/#/i'); } ######################## # Close telnet session # # # ######################## sub Close_Telnet { $telnet->close; } ########################### # Login and Do show # # Optic # ########################### #### OUTPUT ROUTER WILL GIVE BACK from show optic Command #### when this is inputted into the telnet@LAB_MLX16#show optic 7 # Port Temperature Tx Power Rx Power Tx Bias Current #+----+-----------+-------------+------------+----------------+ #7/1 N/A N/A N/A N/A #7/2 40.6250 C -002.9277 dBm -040.0000 dBm 5.820 mA # Normal Normal Low-Alarm Normal #7/3 35.5000 C -002.5219 dBm -002.7992 dBm 7.588 mA # Normal Normal Normal Normal #7/4 32.0000 C -036.9897 dBm -003.5085 dBm 0.004 mA # Normal Low-Alarm Normal Low-Alarm sub Show_Optic { &Open_Telnet; my @RawIndexInts = ($telnet->cmd("sh optic $slot")); pop @RawIndexInts; # print "@RawIndexInts\n"; #results in the same unmodified + output of the Router #Port Temperature Tx Power Rx Power Tx Bias Current # +----+-----------+-------------+------------+----------------+ # 7/1 N/A N/A N/A N/A # 7/2 37.9375 C -002.9311 dBm -040.0000 dBm 5.754 mA # Normal Normal Low-Alarm Normal # 7/3 33.5000 C -002.4048 dBm -002.7959 dBm 7.556 mA # Normal Normal Normal Normal # 7/4 30.2500 C -036.9897 dBm -003.4998 dBm 0.004 mA # Normal Low-Alarm Normal Low-Alarm my $token = 0; foreach my $line (@RawIndexInts) { if ($token eq 0) { $line =~ s/^\s+//; $line =~ s/\s+$//; # print "$line\n"; #results in output of Router with +leading and trailing whitespaces of line gone #Port Temperature Tx Power Rx Power Tx Bias Cur +rent #+----+-----------+-------------+------------+------------ +----+ #7/1 N/A N/A N/A N/A #7/2 40.6875 C -002.9208 dBm -214.3647 dBm 5.828 mA #Normal Normal Low-Alarm Normal #7/3 36.0000 C -002.4305 dBm -002.8100 dBm 7.546 mA #Normal Normal Normal Normal #7/4 32.5000 C -036.9897 dBm -003.4920 dBm 0.004 mA #Normal Low-Alarm Normal Low-Alarm + my @Goodlines = $line =~ m/(^\S{1}\/\S{1})\s+\S{7}\s+\S{1} +\s+(\-\S{3}\.\S{4}\sdBm)\s(\-\S{3}\.\S{4}\sdBm)/g; #this string filt +ers the for values i want # print "@Goodlines\n"; #results in interesting lines with + blank lines # # # #7/2 -002.9242 dBm -036.9897 dBm # #7/3 -002.4764 dBm -002.8075 dBm # #7/4 -036.9897 dBm -003.4901 dBm # print "Port: $Goodlines[0] Tx Power: $Goodlines[1] Rx Powe +r: $Goodlines[2]\n"; #Port: Tx Power: Rx Power: #Port: Tx Power: Rx Power: #Port: Tx Power: Rx Power: #Port: 7/2 Tx Power: -002.9208 dBm Rx Power: -036.9897 dBm #Port: Tx Power: Rx Power: #Port: 7/3 Tx Power: -002.3972 dBm Rx Power: -002.7959 dBm #Port: Tx Power: Rx Power: #Port: 7/4 Tx Power: -036.9897 dBm Rx Power: -003.4998 dBm #Port: Tx Power: Rx Power: } } &Close_Telnet; }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |