#!/usr/bin/perl # # 1.1 10/Jun/2003 Finally got around to placing on website (and slight tidy) # 1.0 03/Dec/2002 Completed # # DNS Resolving NameServer checker # Written by Kul - http://akakul.co.uk/ # use strict; use Socket qw(AF_INET PF_INET SOCK_STREAM); use Fcntl qw(:DEFAULT :flock); my $timeout = 3; my $port = 53; if (! @ARGV) { my $resolver = q(/etc/resolv.conf); if (-e $resolver) { sysopen (RESOLVER, $resolver, O_RDONLY) or die qq(Could not SYSOPEN file $resolver for READ: $!); flock (RESOLVER, LOCK_SH) or die qq(Could not SH-LOCK $resolver file: $!); while () { chomp; if (/^\s*nameserver\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*$/i) { print netutil_test_dns_server($1) . "\n"; } } flock (RESOLVER, LOCK_UN) or die qq(Could not UN-LOCK $resolver file: $!); close (RESOLVER) or die qq(Could NOT CLOSE $resolver: $!); } } else { while (my $this = shift @ARGV) { if ($this =~ /^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*$/i) { print netutil_test_dns_server($this) . "\n"; } else { print "Not an IP: $this\n"; } } } sub netutil_test_dns_server { my ($ip) = @_; local (*SSOCK); my $packedip = pack('S n a4 x8', AF_INET(), $port, pack ('C4', split (/\./o, $ip) ) ); $SIG{'ALRM'} = sub { close SSOCK; return ("Failed $ip"); } ; alarm ($timeout); unless ( socket (SSOCK, PF_INET(), SOCK_STREAM(), getprotobyname('tcp') ) ) { close (SSOCK); return ("Failed: $ip"); } unless ( connect (SSOCK, $packedip) ) { close (SSOCK); return ("Failed: $ip"); } alarm (0); $SIG{'ALRM'} = 'DEFAULT'; close (SSOCK); return ("OK: $ip"); } __END__