#!/usr/bin/perl
#
# read in IP list and remove any entries which appear to be printers
#
#

if ($#ARGV != 2) {
        print "Usage: $0 IP_File New_IP_File NMAP_File\n";
        exit(1);
}

my $ipFile = $ARGV[0];
my $outFile = $ARGV[1];
my $nmapFile = $ARGV[2];
my $nmapPrinters = "/home/m0j0/scripts/nessus/nmap-printers";

open(HAND, "$nmapPrinters") || die("unable to open $nmapPrinters: $!");
@printerList = <HAND>;
close(HAND);

# read in nmap file
my $ipFound;
my $ipAddress;
my %ipScans;
open(HAND, $nmapFile) || die("unable to open $nmapFile: $!");
while(<HAND>) {
   chomp;
   if    (/Interesting ports on/) { ($ipAddress) = /\((.+)\)/; $ipFound = 1; }
   elsif (/^Remote /) { $ipFound = 0; ($ipScans{$ipAddress}{"OS"}) = /: (.+$)/; }
   elsif ($ipFound) { push @{ $ipScans{$ipAddress}{"PORTS"} }, $_; }
}
close(HAND);

# read in IP list
open(HAND, $ipFile) || die("unable to open $ipFile: $!");
open(OUT, ">$outFile") || die("unable to open $outFile: $!");
while(<HAND>) {
   chomp;
	my $ipAddress = $_;
	my $prntFound = 0;

   print "Checking IP: $ipAddress ";
	foreach $printID (@printerList) {
		chomp $printID;
		if ($ipScans{$ipAddress}{'OS'} eq "") { $ipScans{$ipAddress}{'OS'} = "--- NO OS FINGERPRINT ---"; last;}
		elsif ($ipScans{$ipAddress}{'OS'} =~ /\Q$printID/) { print "<-- PRINTER -->"; $prntFound = 1; last;}
	}
	
	if (!$prntFound) {
		my @prntPorts = (280,515,631,9100);
		my $portCount = 0;
		foreach $port (@prntPorts) { if (grep /$port/, @{ $ipScans{$ipAddress}{'PORTS'} }) { $portCount++; } }
		if ($portCount >= 3) { print "<-- ??PRNT?? SAVED -->"; }
		else { print "<-- SAVING IP -->"; }
      print OUT "$ipAddress\n";
   }
   print "\tOS Type: ", $ipScans{$ipAddress}{'OS'}, "\n";
}
close(HAND);
close(OUT);
