perl problem two: parsing cisco command prompt
I'm working on this ISDN scanning project, and I can issue the command, and get the results, back, what I'm missing is a handy way to deal with the results. I want to parse the result line, and store the router name and pass / fail in a report. Shoving the results into an array seems like the best place to start.
I have been working this week, on stuffing it into an @array, and splitting on white space.
This is where i'm coming up empty, I can issue print ((split "\n", "@cmd_output")[1]); and get the second line of output,
for instance, in anycase the lines I want won't start until line 9 though... what i can't figure out is how to do this... into an array.
Have a look at the code, if you give me a few pointers, I'll make it worth your while.
#
#
# Monitor ISDN Lines For Call Completion
# Log Results To Database
#
#
use Net::Telnet::Cisco;
my $cs = Net::Telnet::Cisco->new( Host => '192.168.1.1' );
$cs->login( 'enable', 'staging123' );
# Turn off paging !!!
my @cmd_output = $cs->cmd( 'terminal length 0' );
# need to send the test dialer string first to populate the call records...
# add this later...
# Check the call history!
# Execute a command
@cmd_output = $cs->cmd( 'show isdn history' );
#show what we got back all at once!
#should be something like this:
#--------------------------------------------------------------------------------
# ISDN CALL HISTORY
#--------------------------------------------------------------------------------
#Call History contains all active calls, and a maximum of 100 inactive calls.
#Inactive call data will be retained for a maximum of 15 minutes.
#--------------------------------------------------------------------------------
#Call Calling Called Remote Seconds Seconds Seconds Charges
#Type Number Number Name Used Left Idle Units/Currency
#--------------------------------------------------------------------------------
#Out 5551212 R2 240 60 240 0
#Out 5551212 R2 300 0 300 0
#--------------------------------------------------------------------------------
#
print @cmd_output;
# here i'm demonstrating returning a single line...
print "calling out the second line returned!\n";
print "\n";
print ((split "\n", "@cmd_output")[1]); #counting from zero, interested in 9+
print "\n that was the second line returned! \n";
# Execute a command
@cmd_output = $cs->cmd( 'show isdn history' );
#http://www.pageresource.com/cgirec/ptut14_1.htm
print "-" x 30, "\n";
print "Last prompt: <", $cs->last_prompt, ">\n";
print "Last command: <", $cs->last_cmd, ">\n";
print "Last error: <", $cs->errmsg, ">\n";
#print "Cmd output: <", @cmd_output, ">\n";
print "-" x 30, "\n";
$cs->close;
0 TrackBacks
Listed below are links to blogs that reference this entry: perl problem two: parsing cisco command prompt.
TrackBack URL for this entry: http://kennethhunt.com/mt/mt-tb.cgi/1638
ok... now the split on the array is giving problems:
#
#
# Monitor ISDN Lines For Call Completion
# Log Results To Database
#
#
use Net::Telnet::Cisco;
$inputfile='isdn-call-history.txt';
open (FILE, "$inputfile") || die "Can't open file!\n";
@isdnlog = <FILE>;
close (FILE);
@category = qw(calltype callednumber remotename secondsused secondsleft secondsidle charges);
@subcategory = qw(name name name time time time time);
@prename = splice(@isdnlog, 9, 1);
print @prename;
@name = qw($prename);
print @name;
@indices = sort {$category [$a] cmp $subcategory[$b]
or $category[$a] cmp $subcategory[$b]} (0 .. 6);
foreach $index (@indices) {
print "$category[$index]/$subcategory[$index]: $name[$index]\n";
}
print "\n \n \n all done";
END
I get an error "Use of uninitialized value in concatenation (.) or string"
any suggestions?
OK this suggestion worked well, I took out the extra code and am focused on debugging this with demo data from a text file...here's my current code...
#
#
# Monitor ISDN Lines For Call Completion
# Log Results To Database
#
#
use Net::Telnet::Cisco;
$inputfile='isdn-call-history.txt';
open (FILE, "$inputfile") || die "Can't open file!\n";
@isdnlog = <FILE>;
close (FILE);
@result = splice(@isdnlog, 9, 1);
print @result;
print "\n \n \n all done";
END
I want to grab the first result and parse it in to the whitespace delimited data that it is.
I'll try this out tomorrow when I'm working on it again. I need:
log on to the router
issue command
parse command
save results
right now I'm at step three...
It's not very clear what exactly your problem is. Can you try to rephrase it?
@cmd_output contains all lines of command output. If you want to get only those after line 9 into an array, then you can do the following:
@result = splice(@cmd_output, 9);
splice is used here to return a part of an array. @cmd_output is the original array, 9 is the offset from which to start. Note though that @cmd_output will be modified by this command. If you don't want that, than just copy @cmd_output into another array.
Email me, if you still have a problem. ;)