I am working on a Perl script that is to help with the automation of scanning of machines on our network. I am not a programmer by trade, but none-the-less this project has been assigned to me, and I am quite stumped. Before I explain the nature of what is stumping me, let me explain the outline of what I am doing.
Basically, this script will be run every n hours. When run, it will check a file that holds a log of active IPs and check them against a DHCP log to single out only those that are static. These then are put into a hash (a new one if flagged to initialize, loaded using Storable otherwise), with the key being the IP and within an array their MAC [0] and a "last scanned" date [1] initially set to 19700101. The next part of the script compares the date between today's date and the "last scanned" date - and if it under a certain threshold, it sends a query to our scanner.
The issue that has me so lost is that when the date is being checked, it seems to me that the date value (the updated "last scanned") is being set before entering the conditional. While this doesn't seem likely to me, it is the only possibility I can think of. Here is the relevant chunks of code:
The code that adds IP/MACs to the hash
if(init == 1){
%SCAN = ();
@data = ();
foreach $key (keys %IPS){
$unsavedDB = 1;
$data[0] = $IPS{$key};
$data[1] = 19700101;
print $data[1];
$SCAN{$key} = \@data;
}
}else{
#repeat of the above code, but with a if(exists...) to prevent duplicates from being added to the hash that is loaded via storables.
}
The code that checks the date (which is set previously, and would be 20120726 for today). Between the above code and the following there is nothing but comments
$scanned = 0;
foreach $key (keys %SCAN){
$lastScanned = $SCAN{$key}[1];
if(($date - $lastScanned) > $threshold){
$unsavedDB = 1;
$toScan = ${$key}[0];
#omitted data for security reasons, just basically forms a string to send to a scanner
$SCAN{$key}[1] = $date;
$scanned++;
}
}
print "finished. $scanned hosts queued\n";
Now, the reason that I believe that the value is being changed before entering the loop is when I add a 'print $lastScanned' statement right before the 'if(($date...){' the date printed the whatever is assigned to $date earlier - but if I to comment out the '$SCAN{$key}[1] = $date;' statement, the print statements will print the '19700101' date and everything functions as it should. What is happening? $SCAN{$key}[1] is never being touched except in the two places shown above.
Sorry if this is very badly phrased, or doesn't make sense. I tried my best to explain something that has been stumping me for hours.
Thank you!