#!/usr/bin/perl

use DBI;
use Getopt::Std;

sub getrec {
	if ($_) {
		$num=$_;
	}
	if ($port) {
		$sql="select host_ip, host_num, ports from scan where ports regexp '.*$port.*'";
	}
	if ($host) {
		$sql="select host_ip, host_num, ports from scan where host_ip=\"$host\";";
	}
	if ($num) {
		$sql="select host_ip, host_num, ports from scan where host_num=\"$num\";";
	}
	$query=$dbh->prepare($sql);
	$query->execute || die("sql error: $dbh::errstr");
	if (! $iponly) {
		printheader();
		while(($host_ip,$host_num,$ports)=$query->fetchrow_array) {
			write;
		}
	} else {
		while(($host_ip,$host_num,$ports)=$query->fetchrow_array) {
			print "$host_ip\n";
		}
	}
        $query->execute;
        my $ref = $query->fetchall_arrayref;
        $rows = scalar @{$ref};
}

sub grn {
	$sql="select host_ip, host_num, ports from scan where host_num=\"$num\";";
        $query=$dbh->prepare($sql);
        $query->execute || die("sql error: $dbh::errstr");
	($host_ip,$host_num,$ports)=$query->fetchrow_array;
	if (! $iponly) { 
		write;
	} else {
		print "$host_ip\n";
	}
}

sub cntrec {						# random records returned from database.
	$sql="select host_ip,host_num,ports from scan where ports regexp '.*$port.*'";
	$query=$dbh->prepare($sql);
	$query->execute || die("sql error: $dbh::errstr");
	rndcnt();
	if (! $iponly) { 
		printheader(); 
	}
	foreach $num (@rnd) {
		grn($num);
		$rows++;
	}
}

sub initsql () {
        $dbh = DBI->connect('DBI:mysql:basic_scan:203.26.137.9','scanner','h0h0h0');
}

sub printheader {
	$host_num="Host ID";
	$host_ip="Host IP Addr";
	$ports=" Ports Found\n";
	write;
}


sub rndcnt {
	$query->execute || die("sql error: $dbh::errstr");
	$ref=$query->fetchall_arrayref;
	$numrec = scalar @{$ref};
	$i=0;
	@rnd;
	while ($i < $count) {
		$t=0;
		$a = int(rand($numrec)+1);
		foreach $b (@rnd) {
			if ($a == $b) {
				$t=1;
			}
		}
		if ($t==0) {
				push (@rnd,$a);
				$i++;
		}
	}
}


sub finish {
	if (! $iponly) { print "Number of Records Returned: $rows\n"; }
	$query->finish;
	$dbh->disconnect;
}

sub usage {
	print "scanclient v0.8.2\n";
	print "Usage: scanclient -p <port to search for>\n";
	print "       scanclient -h <ip address to search for>\n";
	print "       scanclient -n <record number to search for>\n";
	print " option -c # <Print # Number of Random hosts matching the port criteria>\n";
	print " option -i   <Print IP Addresses only>\n";
	exit;
}

sub main {
	my %options;
	getopts('ip:h:n:c:',\%options) || usage();
	if (defined $options{i}) {
		$iponly=$options{i};
	}
	if (defined $options{c}) {
		$count=$options{c};
	}
	if (defined $options{p}) {
		$port=$options{p};
	}
	elsif (defined $options{h}) {
		$host=$options{h};
	}
	elsif (defined $options{n}) {
		$num=$options{n};
	} else { usage(); }
	initsql();
	if ($count) {
		cntrec();
	} else {
		getrec();
	}
	finish;
}

format STDOUT =
@<<<<<<<<     @<<<<<<<<<<<<<<<<<         @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$host_num, $host_ip, $ports
.

main();
