#!/usr/bin/perl
use DBI;

my $dbname = "chlor";

# Åben en DatabaseHandle til PostgreSQL
$dbh = DBI->connect("DBI:Pg:dbname=$dbname") or die $dbh->errstr;
# Forbered en SQL-kommando for læsning af een tabel
$sth = $dbh->prepare("SELECT RemoteIP FROM RemoteIP WHERE RevDNS IS NULL ORDER BY RemoteIP");
# Udfør SQL-kommandoen
$sth->execute;
# For alle poster (linjer), udskriv indholdet
while (($remoteip) = $sth->fetchrow) {
	$revdns = `host $remoteip`;
	if ($revdns =~/domain name pointer (.+)\.$/) {
		setrevdns($dbh, $remoteip, $1);
	} else {
		setrevdns($dbh, $remoteip, "1");
	}
}
# Nedlæg StatementHandle
$sth->finish;
# Luk forbindelsen til PostgreSQL
$dbh->disconnect;

# setrevdns( dbh, ipnumber, revdns )
sub setrevdns() {
	my $dbh = shift;
	my $ipnumber = shift;
	my $revdns = shift;
	print "$ipnumber\t$revdns\n";
	my $sql = "UPDATE RemoteIP SET RevDNS='$revdns' WHERE RemoteIP='$ipnumber'";
	#print "$sql\n";
	my $sth = $dbh->prepare($sql);
	$sth->execute;
	$sth->finish;
}

