If the lease files are lost what would be the solution?

Frank Bulk frnkblk at iname.com
Fri May 4 13:43:43 UTC 2007


I'm no full-time programmer, but this got the job done.  I did compare the
contents of the existing file dhcpd.leases file to the arp cache so that the
leases file would have priority.

You would have the change the dates, of course.  The ARP output was from a
BSR 64K using 'show arp'.  You would have to modify the script based on how
your ARP output is formatted.

Frank

=====================================================
#!/usr/bin/perl

use Socket;
my $i = 0;
my $j = 0;
my $k = 0;
my $match = 0;
my $line;
my $line2;
my $ip = '';
my $mac = '';
my $line;
my @array;

open (DHCP_IN_FILE, "dhcpd.leases");
my @dhcp_in_file = <DHCP_IN_FILE>;
my $dhcp_in_file_count = @dhcp_in_file;
print "DEBUG: [$dhcp_in_file_count]\n";
close DHCP_IN_FILE;

open (CM_INFO_FILE, "arp2.txt");
my @cm_info_file = <CM_INFO_FILE>;
my $cm_info_file_count = @cm_info_file;
print "DEBUG: [$cm_info_file_count]\n";
close CM_INFO_FILE;

open (DHCP_OUT_FILE, ">dhcpd.edit");

while ($i < $cm_info_file_count) {
	$line = $cm_info_file[$i];
	chomp $line;
	($ip, undef, $mac) = split (/\s+/, $line);
	$mac = &convert_address('semi', $mac);
	for ($j = 0; $j <= $dhcp_in_file_count; $j++) {
		$line2 = $dhcp_in_file[$j];
		chomp $line2;
# print "DEBUG: [$line2]\n";
		if ($line2 =~ /$ip {/) {
			$match = 1;
# print "DEBUG: IP matched! [$ip] [$dhcp_in_file[$j]\n";
#			while ($line2 !~ /hardware ethernet/) {
#				$j++;
#				$line2 = $dhcp_in_file[$j];
#				chomp $line2;
#			}
#			if ($line2 =~ $mac) {
# print "DEBUG: Match!\n";
#				$match = 1;
#			}
		}
	}	
	if (!$match) {
		print "No match: $ip\n";
		$dhcp_out_file[$k++] = 'lease ' . $ip . ' {\r';
		$dhcp_out_file[$k++] = '  starts 5 2006/11/10 22:45:00;\r';
		$dhcp_out_file[$k++] = '  ends 1 2006/11/13 22:45:00;\r';
		$dhcp_out_file[$k++] = '  tstp 1 2006/11/13 22:45:00;\r';
		$dhcp_out_file[$k++] = '  binding state active;\r';
		$dhcp_out_file[$k++] = '  next binding state free;\r';
		$dhcp_out_file[$k++] = '  hardware ethernet ' . $mac .';\r';
		$dhcp_out_file[$k++] = '  client-hostname "UNKNOWN";\r';
		$dhcp_out_file[$k++] = '}\r';
	}
	$match = 0;
	$i++;
}

print DHCP_OUT_FILE (@dhcp_out_file);

exit;

sub convert_address {
	my $switch_type = shift;
	my $in_address = shift;
	my $return_str = '';

    if ($in_address eq '') {
        return $return_str;
    }

	# lower case any letters
	$in_address = lc $in_address;

	# Strip out any existing dots and dashes
	$in_address =~ s/\.//g;
	$in_address =~ s/-//g;
	$in_address =~ s/://g;

	# Put the dots and dashes back in for the corresponding switch type
	if ($switch_type eq 'semi') {
		$return_str = substr $in_address, 0, 2;
		$return_str .= ':' .  substr $in_address, 2, 2;
		$return_str .= ':' .  substr $in_address, 4, 2;
		$return_str .= ':' .  substr $in_address, 6, 2;
		$return_str .= ':' .  substr $in_address, 8, 2;
		$return_str .= ':' .  substr $in_address, 10, 2;
	} elsif ($switch_type eq 'dash') {
		$return_str = substr $in_address, 0, 2;
		$return_str .= '-' .  substr $in_address, 2, 2;
		$return_str .= '-' .  substr $in_address, 4, 2;
		$return_str .= '-' .  substr $in_address, 6, 2;
		$return_str .= '-' .  substr $in_address, 8, 2;
		$return_str .= '-' .  substr $in_address, 10, 2;
	} elsif ($switch_type eq 'dot') {
		$return_str = substr $in_address, 0, 4;
		$return_str .= '.' . substr $in_address, 4, 4;
		$return_str .= '.' . substr $in_address, 8, 4;
	} else {
		$return_str = $in_address;
	}

	return $return_str;
}
===================================================== 

-----Original Message-----
From: dhcp-users-bounce at isc.org [mailto:dhcp-users-bounce at isc.org] On Behalf
Of kalyanasundaram
Sent: Friday, May 04, 2007 7:55 AM
To: dhcp-users at isc.org
Subject: RE: If the lease files are lost what would be the solution?

Hi Frank,
    As others mentioned the only solution would be  cleaning up the
abandon  lease  and restart the server for some time. But i would like
to see how you have done it through router's table. Is that possible for
me to see the perl script. 
Thanks,
  -"kalyan"


On Wed, 2007-04-25 at 07:10 -0500, Frank Bulk wrote:
> I once accidentally deleted mine and had to re-create it (I used a Perl
> script to do so).  I sourced the data from the router's ARP table and
turned
> on ping-ahead.  I obviously had some pain until my 50% of lease time
arrive
> (36 hours).
> 
> Frank 
> 
> -----Original Message-----
> From: dhcp-users-bounce at isc.org [mailto:dhcp-users-bounce at isc.org] On
Behalf
> Of S Kalyanasundaram
> Sent: Wednesday, April 25, 2007 2:56 AM
> To: dhcp-users at isc.org
> Subject: If the lease files are lost what would be the solution?
> 
> Hi all,
> The leases are stored in dhcpd.leases and backup is at dhcpd.leases~. For
> some reason these files are lost and dont have any kind of backup of them
> and again server got stopped. Say some hardware fault have come. We will
> have to restart the dhcpd server without the lease database. 
> 
> Does anyone faced this kind of situation. What would be the good
suggestion
> for recovering this problem. 
> 
> The problem is, the existing client go for renewal and may be get a new IP
> address which breaks his (connection, download or whatever). The other
case
> would be new clients have come up and ask for an IP and the server check
for
> a free IP but which is already available and mark as abandon and give some
> new IP to the new client. When the original client goes for renewal of the
> IP but that is already marked as abandon. So after that i guess it will
get
> a new IP address. 
> I guess there would be some more scenarios. 
> 
> Anybody faced this kind of similar problem? Any suggestions are
appreciated.
> 
> 
> Thanks in advance,
>  -"kalyan"
> 
> 
> 
> 
> 
> 





More information about the dhcp-users mailing list