I have a script which uses the text::diff module and I cant figure out
how to use the output
I can print the result, but I need to parse the results into a array
of strings and format it.
My code is:
use Net::Telnet::Cisco;
use File::Copy;
use Text::Diff;
my $device = '1.2.3.4';
# Connect to device
my $session = Net::Telnet::Cisco->new(Host => $device );
$session->login('login','password');
# Enable mode
if ($session->enable("password"))
{
$session->cmd('term len 0');
@output = $session->cmd('show run');
$session->cmd('exit');
}
else
{
warn "Can't go into enable mode: " . $session->errmsg;
}
my $NEWCFGFILE = "D:/cfg-data/$device.new.cfg";
my $OLDCFGFILE = "D:/cfg-data/$device.old.cfg";
# Delete old config file
unlink "$OLDCFGFILE";
# Rename existing new file to old file
copy ( $NEWCFGFILE, $OLDCFGFILE );
unlink "$NEWCFGFILE";
# Save new config to file
open CURRENTFILE, ">$NEWCFGFILE";
print CURRENTFILE @output;
close CURRENTFILE;
# Test if any lines are different
my $diff_txt = diff( $NEWCFGFILE, $OLDCFGFILE, { STYLE =>
"Context" } );
print "$diff_txt";
Thanks
Mark Clements - 04 Sep 2008 06:13 GMT
> I have a script which uses the text::diff module and I cant figure out
> how to use the output
[quoted text clipped - 8 lines]
>
> my $device = '1.2.3.4';
Hi,
Your script is missing
use strict;
use warnings;
This will help you catch potential errors before runtime. Also, you need
to *always* check the return value of system calls:
unless(unlink "$OLDCFGFILE"){
# no files deleted!!
}
unless(open CURRENTFILE, ">$NEWCFGFILE"){
# could not open file!!
die "$!";
}
It's generally recommended to use the three-argument form of open and a
lexically-scoped filehandle:
unless(open my $currentfh, ">",$NEWCFGFILE){
...
}
print $currentfh @output;
although I'd argue it's confusing writing to a new file with a
filehandle called "current".
You may like to look at Text::Patch for ways of using the output of
Text::Diff.
Mark