Today is: 8 January, 2012
Check todays hot topics

DenyHosts reporting script (sshd/httpd)

#!/usr/bin/perl
#
# dhreport.pl: grab sshd/httpd versions from owned hosts
#	       return ownage if possible? :)
#
 
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use IO::Socket::INET;
use LWP::UserAgent; 
 
sub usage 
{
	my $script = basename($0);
	print <<USAGE
 
	$0 [ -f <file> -t <timeout> ]
 
		-f 	hosts.deny file to read from
			default: /etc/hosts.deny
 
		-t	timeout for outbound connections
			default: 4
 
	NOTE: hosts.deny file assumes 'sshd: <host>' format
 
USAGE
;
 
	exit 0;
}
 
 
my $opt = {};
GetOptions( $opt,
	    'file|f=s',
	    'help|h',
	    'timeout|t=i',
	   ) or usage();
 
 
usage if (exists($opt->{'help'}));
my $file = (exists($opt->{'file'})) ? $opt->{'file'} : '/etc/hosts.deny';
my $timeout = (exists($opt->{'timeout'})) ? $opt->{'timeout'} : 4;
 
if ((! -f $file) || (! -r $file))
{
	print STDERR "$file does not exist or unreadable, exiting.\n";
	exit 1;
}
 
open(HOSTSDENY, $file);
my @lines = <HOSTSDENY>;
close(HOSTSDENY);
print "\n";
 
foreach my $line (sort @lines)
{
	next if ($line !~ m/^sshd\:.*$/);
	my (undef, $ip) = split(':\ ', $line);
	chomp($ip);
	print "$ip:\n\n";
 
	my $connect = IO::Socket::INET->new(
		PeerAddr => $ip,
                PeerPort => 22,
                Proto    => 'tcp',
                Timeout  => $timeout,
        );
 
	if ($connect)
	{
		my $CRLF = "\015\012";
		print $connect "$CRLF";
 
		my @vs = $connect->getlines();
		foreach (@vs) { 
			print "\t" . $_ if ($_ =~ m/^SSH/);
		}
 
		close $connect;
	}
	else
	{
		print "\tTimeout exceeded or connection refused\n";
	}
 
	my $ua = LWP::UserAgent->new();
	$ua->timeout($timeout);
	my $resp = $ua->get("http://$ip");
	if ($resp->is_success())
	{
		print "\t" . $resp->{_headers}->{server} . "\n";
	}
	else
	{
		print "\tTimeout exceeded or connection refused\n";
	}
 
	print "\n";
}
 
exit 0;