Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

DHCP and DNS Compare

by Hyler (Sexton)
on Mar 30, 2000 at 20:14 UTC ( #6538=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Martin Hylerstedt
Description: For Windows NT administrators. Compares DHCP server and DNS server info to see if they match reasonably. Requires NT Resource Kit installed.
# A little something I cooked up to check if the DHCP 
#and the DNS servers matched reasonably, regarding name 
# and number.
# Done in ActivePerl on NT 4.0
# Needs NT Resource Kit installed, for using the 
# command 'dhcpcmd' to query the DHCP server.
# Macs using DHCP sometimes give (null) as computer name.

use Socket;
use Net::hostent;
use Time::localtime;

$DHCPserver = "111.222.333.444"; 
# your DHCP server's IP here
$scope = "111.222.333.0"; # the DHCP scope

sub makedhcphash(){
    concat(DHCPCMD, "dhcpcmd $DHCPserver enumclients $scope|");
    while ($line = <DHCPCMD>) {
    if ($line =~ /^\d/) {
        @fields = split(/\s+/,$line);
        $dhcphash{$fields[1]} = $fields[2];
    } # if ends

    } # while ends
    close DHCPCMD;
} # makedhcphash ends

sub makednshash(){
# With the wild assumption that the computer you 
# run the script on knows which DNS server, if any, 
# it should talk to.
    while (($ipnumber, $computername) = each(%dhcphash)) {
    unless ($h = gethost($ipnumber)) {
        $dnshash{$ipnumber} = "not";
    $dnshash{$ipnumber} = gethost("$ipnumber")->name;
    } # while ends
} # makednshash ends

sub twodigit {
# return string (mday, mon, hour and so on) as
# two digits. January is thus 01, not 1
    if ($_[0] =~ /\d/) {
    return "0" . $_[0];
    else {
    return $_[0];

sub comparehashes(){
# . operator concatenates string values
    $timestamp = twodigit(localtime->mday) . localtime->mon . localtim
+e->hour . localtime->min;
    while(($ip, $name) = each(%dhcphash)) {
    $_ = $dnshash{$ip};
    print $1;
    unless ($dhcphash{$ip} =~ /$1/i) {
        print OUT ($ip, " (", $dhcphash{$ip}, ") is ", $dnshash{$ip}, 
+" in DNS\n");
    } # unless ends
    } # while ends
} # comparehashes ends

Replies are listed 'Best First'.
RE: DHCP and DNS Compare
by chromatic (Archbishop) on Mar 30, 2000 at 22:25 UTC
    Always check the status of your open calls! You'll spend less time debugging mysterious errors, I promise. open (DHCPCMD, "dhcpcmd $DHCPserver enumclients $scope|") || die "Can't get DHCP information: $!"; Use sprintf instead of twodigit(): my $day = sprintf("%02d", localtime->mday); I'm not sure what comparehashes() is doing, especially as you refer to $1 but don't appear to grab it from anywhere. Instead, I'd lc() machine names when assigning them to %dhcphash and %dnshash and just use the eq operator to compare them.

    Finally, I like to check the return values of my close() calls. No pun intended. If something goes wrong here, I want to know about it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://6538]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2017-07-21 01:02 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (316 votes). Check out past polls.