[TriLUG] YAPQ (yet another perl question)..
Greg Brown
gwbrown1 at gmail.com
Thu Jun 11 14:20:52 EDT 2009
Ok, I've got a really simple script that is confounding me. I have a source
list of a bunch of routers, all the same make and model. I ssh into the
routers and, at this point, just running a simple command: show flash. The
command 'show flash' returns output much like this:
routername#show flash
Directory of flash:/
2 -rwx 2072 Jun 2 2009 17:29:17 +00:00 multiple-fs
3 -rwx 7713393 Mar 1 1993 01:11:46 +00:00
c3560-advipservicesk9-mz.122-25.SED.bin
4 -rwx 616 Mar 1 1993 00:00:55 +00:00 vlan.dat
5 drwx 192 Mar 1 1993 00:08:10 +00:00
c3560-ipservices-mz.122-25.SEB2
358 -rwx 3916 Jun 2 2009 17:29:17 +00:00 private-config.text
359 -rwx 8248 Jun 2 2009 17:29:17 +00:00 config.text
15998976 bytes total (358912 bytes free)
Nothing terrible exciting. I'm capturing this using Perl::Expect and I'm
doing so using this small block of code:
# now we should be at the "#" prompt
$ssh->expect(30,'#');
# now see how many free bytes we have
print $ssh "show flash\r";
my( $which, $why, $match, $before, $after ) = $ssh->expect(30,'#');
The variable "$before" captures the output of 'show flash'
The only line I'm interested in at this point is the last line that shows
the total size of flash and the bytes free. I've tried a thousand ways to
get *just that line* out of the output in many and I finally just tried to
split it using:
@before=split(/\n/,$before);
foreach $beforeLine (<@before>) {
chomp($beforeLine);
print "beforeLine $beforeLine\n";
}
Well guess what.. the output looks like...
beforeLine 00:00:55
beforeLine +00:00
beforeLine vlan.dat
beforeLine 5
beforeLine drwx
beforeLine 192
beforeLine Mar
beforeLine 1
beforeLine 1993
beforeLine 00:08:10
beforeLine +00:00
This is NOT what I'm trying to do.. I'm trying to split at the newline but
it's not working. I opened a debug to print out $before to get an idea of
what it looks like.. and here it is:
which 1
why
match #
before show flash^M
^M
Directory of flash:/^M
^M
2 -rwx 9860621 Nov 10 2008 20:25:19 +00:00
c3560-advipservicesk9-mz.122-40.SE.bin^M
3 -rwx 616 Nov 10 2008 20:26:47 +00:00 vlan.dat^M
4 -rwx 2072 Nov 17 2008 18:57:21 +00:00 multiple-fs^M
6 -rwx 6234 Nov 17 2008 18:57:21 +00:00 config.text^M
7 -rwx 1934 Nov 17 2008 18:57:21 +00:00 private-config.text^M
^M
15998976 bytes total (6124544 bytes free)^M
routername_3560 after
I'm not sure what the "^M" at the end of the lines are but I can't seem to
split on them either.. or I get the same results as splitting with "\n".
Any ideas? I'm missing something really simple here but, well, I'm missing
it. Any ideas?
Greg
More information about the TriLUG
mailing list