#!/usr/bin/perl
# $Header: /home/smilton/bin/dhcp/RCS/dhcp_migration.pl,v 1.2 2007/05/08 12:10:23 smilton Exp smilton $
# Desc: Migration script Sun DHCP -> ISC Dhcp
# Dumps Sun DHCP IPs and Mac and converts to ISC dhcp host lines.
#
# Shane Milton
#
use warnings;
use Socket;

#usage dhcp_migration.pl 10.102.14.0

sub cid2mac($) {
   my $ether = shift;
   $ether =~ tr/A-Z/a-z/;
   $ether =~ s/(01)(..)(..)(..)(..)(..)(..)/$2:$3:$4:$5:$6:$7/;
   return $ether;
}

sub printISCconf($$$) {
   my $hostname = shift;
   my $ip       = shift;
   my $mac      = shift;

   print "host $hostname\t\{ hardware ethernet $mac;\tfixed-address $ip;\toption host-name \"$hostname\";\}\n"
}


#
# MAIN
#
foreach (`pntadm -P $ARGV[0]`) {
   if ( $_ =~ m/(^01\w{12})/ ) {
      chomp;
      my ($cid) = $_ =~ m/(^01\w{12})/;
      my ($ip) = $_ =~ m/(10\.102\.\d+\.\d+)/;
      my $ipaddr = inet_aton(m/(10\.102\.\d+\.\d+)/);
      my $hostname = sprintf "%s", gethostbyaddr($ipaddr, AF_INET);
      $hostname =~ s/\.autotrader\.com//; #strip off the FQDN
      my $mac = cid2mac($cid);
      #print "cid=$cid\tip=$ip\thostname=$hostname\tmac=$mac\n";

      if ($hostname =~ m/\w/) { #added filter to strip out unnamed records
         printISCconf($hostname, $ip, $mac);
      }
   }
}