Generating dhcpd.conf from list of IP address, MAC address, and hostname

According to my last post, you might want to customize some part of information, e.g., order and name. For sorting, you can easily insert sort between the pipe chain.

[root@mail ip]# arp -n | sort -t "." -k 4 -g | ./arptodhcpd.sh 
        host host1 {
                hardware ethernet 00:13:F7:08:20:D2;
                fixed-address 192.168.10.1;
        }
        host host10 {
                hardware ethernet 00:11:2F:44:F9:44;
                fixed-address 192.168.10.10;
        }
        host server2000 {
                hardware ethernet 00:11:85:19:F0:D4;
                fixed-address 192.168.10.42;
        }

However, name and IP address cannot be modified at all. You could do it by redirecting result of arp -n to a file and then modified it first.

[root@mail ip]# arp -n > iplist.txt
[root@mail ip]# cat iplist.txt | ./arptodhcpd.sh

For shorter and more readability, I wrote another script to collect IP address, MAC address and its netbios name in tab separated value (TSV) format. It "s named as collectip.sh.

#!/bin/sh
 
arp -n | awk "
/..:..:..:..:..:../ {
  name="";
  tmp="/tmp/ip2nb.tmp";
  system(sprintf("./ip2nb.sh %s > %s",$1,tmp));
  getline name < tmp
  close(tmp);
  if (name == "") {
    split($1,ip,".");
    name=sprintf("host%s",ip[4]);
  }
  printf("%s\t%s\t%s\n",$1,$3,name);
}
" | sort -k 4 -t "." -g
rm -f /tmp/ip2nb.tmp

As a result, we will get something like below.

[root@mail ip]# ./collectip.sh > iplist.txt
[root@mail ip]# cat iplist.txt
192.168.10.1    00:13:F7:08:20:D2       host1
192.168.10.42   00:11:85:19:F0:D4       server2000

Now we can customize order, IP address, MAC addres, and hostname in iplist.txt and then convert it to dhcpd.conf later using listtodhcpd.sh as follow.

#!/bin/sh
awk "
/..:..:..:..:..:../ {
  printf("\thost %s {\n",$3);
  printf("\t\thardware ethernet %s;\n",$2);
  printf("\t\tfixed-address %s;\n\t}\n",$1);
}"

Below is the result generated by this script.

[root@mail ip]# cat iplist.txt | ./listtodhcpd.sh 
        host host1 {
                hardware ethernet 00:13:F7:08:20:D2;
                fixed-address 192.168.10.1;
        }
        host server2000 {
                hardware ethernet 00:11:85:19:F0:D4;
                fixed-address 192.168.10.42;
        }

Post new comment