open(INPUT, "< $file ");
my $counter = 0;
while(<INPUT>) {
$counter++;
@stuff = split(";;");
# Sanity checks:
next if (length($stuff[2]) <= 11);
next if (length($stuff[6]) < 1);
next unless defined($stuff[7]);
# It isn't right if a MAC is associated with a Vlan interface, or these:
next if ($stuff[7] =~ /^(V[lL]|Nu|Lo|Eo|Po)/);
# Assigning useful variables names from the input line:
$date = localtime();
my $vlan = 1;
# I just want the short hostname, not the FQDN. Personal preference...
my $switch = "$stuff[6]";
$switch =~ s/^(.*?)\.(.*)/$1/;
$switch = lc($switch);
my $switchport = "$stuff[7]";
my $macaddr = "$stuff[2]";
my $source = "nedi";
my $sourcedate = $date;
if (defined($stuff[4])) {
$firstseen = localtime($stuff[4]);
}
else { $firstseen = "NULL"; }
if (defined($stuff[5])) {
$lastseen = localtime($stuff[5]);
}
else { $lastseen = "NULL"; }
if (defined($stuff[10])) {
if (length($stuff[10]) > 1) {
$vlan = "$stuff[10]";
}
else {$vlan = 1; }
}
chomp($vlan);
chomp($switch);
# We should now have these, and we can use them without fear of uninitialized value
# errors:
# print (Test: $switch, $switchport, $macaddr, $source, $firstseen, $lastseen\n);
# Check to see if it's already in the DB:
my $bquery = "SELECT * FROM bridge WHERE (mac_addr='$macaddr' and switchport='$switchport' and switch='$switch')" ;
my $hth = $dbh->prepare($bquery);
if( !$hth ) {
die "Cannot prepare statement: $DBI::errstr\n";
}
if(!($hth->execute)) {
print "db select failed: $DBI::errstr\n";
$hth->finish;
print "db insert failed: $DBI::errstr\n";
}
else {
if ( @row = $hth->fetchrow_array ) {
if (@row) {
$row[2] =~ s/://g;
# Verify the match, and if there is a match, just update lastseen:
if ($row[0] =~ /$switch/i && $row[1] =~ /$switchport/i && $row[2] =~ /$macaddr/i) {
$newquery = "UPDATE bridge SET lastseen='$lastseen', vlan='$vlan' WHERE (mac_addr='$macaddr' and switchport='$switchport' and switch='$switch')";
}
#if anything is different (something moved):
elsif ($row[2] =~ /$macaddr/i) {
print "Something moved: $stuff[0] $macaddr on: $switch $switchport net $vlan\n";
$newquery = "INSERT INTO \"bridge\" (switch, switchport, \"mac_addr\", source, sourcedate, lastseen, firstseen, vlan) VALUES ('$switch', '$switchport', '$macaddr', '$source', '$sourcedate', '$lastseen', '$firstseen', '$vlan')";
}
my $hth2 = $dbh->prepare($newquery);
if (!($hth2->execute)) {
print "db update failed: $DBI::errstr\n";
$hth2->finish;
}
}
}
# The SELECT failed, this is new.
else {
print "Inserting new stuff: $stuff[0] $macaddr on: $switch $switchport net $vlan\n";
print "\t\tPort desc: $stuff[9]\n";
print "\tFirst seen $firstseen, last seen $lastseen\n\n";
my $newquery = "INSERT INTO \"bridge\" (switch, switchport, \"mac_addr\", source, sourcedate, lastseen, firstseen, vlan) VALUES ('$switch', '$switchport', '$macaddr', '$source', '$sourcedate', '$lastseen', '$firstseen', '$vlan')";
my $hth2 = $dbh->prepare($newquery);
if (!($hth2->execute)) {
print "db instert failed: $DBI::errstr\n";
$hth2->finish;
}
}
$hth->finish;
}
} #end of loop
$dbh->disconnect;