Hi
I've been using PERL for minor forms and such on my web server for a while.. I'm still very much a begginner with the language and now I have a problem.
I have two separate txt files that I need to do a search on. The files are MAC tables from a router and a switch. The "MASTER" file contains all of the MAC addresses + IP addresses from the core router. The "SWITCH" file contains the MAC address and what port on the switch that MAC address is forwarded to. The files are formatted like this:
I need to match MAC address xx:xx:xx:xx:xx:xx from the SWITCH file to the MASTER file and then output all of this info to a third file. The output's format should look something like:
So this is the program I tried writing:
The problem is that my output comes out very wrong. The MAC address matches up with the port (but I can just get that from the SWITCH text file) but the IP address shows up as the last IP in the MASTER text file.
The MASTER file is roughly 330kb and the SWITCH file is 5kb. I know my mistake lies somewhere in one of the loops since it outputs the very last IP in the MASTER file for every MAC match it makes in the SWITCH file but I can't seem to fix it.
ANY help would be greatly appreciated.
I've been using PERL for minor forms and such on my web server for a while.. I'm still very much a begginner with the language and now I have a problem.
I have two separate txt files that I need to do a search on. The files are MAC tables from a router and a switch. The "MASTER" file contains all of the MAC addresses + IP addresses from the core router. The "SWITCH" file contains the MAC address and what port on the switch that MAC address is forwarded to. The files are formatted like this:
Code:
MASTER:
4/3 in Location_A xx:xx:xx:xx:xx:xx 10.1.1.1 dynamic
9/1 in Location_B yy:yy:yy:yy:yy:yy 10.2.2.2 dynamic
Code:
SWITCH:
learned xx:xx:xx:xx:xx:xx 1/39
learned yy:yy:yy:yy:yy:yy 1/13
I need to match MAC address xx:xx:xx:xx:xx:xx from the SWITCH file to the MASTER file and then output all of this info to a third file. The output's format should look something like:
Code:
OUTPUT:
xx:xx:xx:xx:xx:xx 10.1.1.1 1/39
So this is the program I tried writing:
Code:
#!/usr/bin/perl
# Open the switch text for reading
open SWITCH, "switch.txt" or die "Could not open switch.txt!";
# Open the master text for searching
open MASTER, "master.txt" or die "Could not open master.txt!";
# Open an output file
open OUTPUT, ">output.txt" or die "Could not open output.txt!";
# Loop through the switch text
while(<SWITCH>) {
# Read a line and then cut off the end of line character
$TheRec = $_; chomp($TheRec);
# The three variables in the exported switch files are delimited by tabs
# Cut them up to get the needed address variable
($status, $smac, $port) = split(/\t/, $TheRec, 3);
# While still in that loop check the master.txt file for a match
while(<MASTER>) {
# Read another line and cut off the end of line character
$TheRec2 = $_; chomp($TheRec2);
# Once again the variables needed are tab delimited
# Cut them up into appropriate variables
($interface, $mmac, $mip, $type) = split(/\t/, $TheRec2, 4);
# End the MASTER search loop
}
# Get rid of any spaces in the variables
($status, $smac, $port, $interface, $mmac, $mip, $type) =~ s/^\s+|\s+$//g;
# Compare the switch mac with the master mac
# If they match then export all of the relevant data
# to the output file delimited by tabs
if($smac == $mmac) {
print OUTPUT "$smac\t$mip\t$port\n";
}
# End the SWITCH search loop
}
# Close all of the used files
close SWITCH;
close MASTER;
close OUTPUT;
The problem is that my output comes out very wrong. The MAC address matches up with the port (but I can just get that from the SWITCH text file) but the IP address shows up as the last IP in the MASTER text file.
The MASTER file is roughly 330kb and the SWITCH file is 5kb. I know my mistake lies somewhere in one of the loops since it outputs the very last IP in the MASTER file for every MAC match it makes in the SWITCH file but I can't seem to fix it.
ANY help would be greatly appreciated.