Bulk import DHCPD data into pfSense

Similar to my previous post, if you are trying to bulk import your current DHCPD data into pfSense, the built-in pfSense shell comes in handy.

Here we’ll start to use the current ISC-DHCPD configuration file, /etc/dhcp/dhcpd.conf, which will have entries like this:

host pi31 { hardware ethernet AA:BB:CC:DD:EE:FF; fixed-address pi31; }
host pi32 { hardware ethernet DD:EE:FF:AA:BB:CC; fixed-address pi32; }
host MobilePhone { hardware ethernet CC:DD:AA:BB:EE:FF; fixed-address mobilephone; }

Then run the following script – modify it to your needs – which will print out the commands for the pfSense shell. Since my DHCPD configuration is relying upon existing DNS entries and I am having hostnames as “fixed-address” entries, I need to resolve these entries with a dig command. If your file is always using IP addresses, just parse them out:

echo "global \$config;"
echo "parse_config(true);"

index=0
grep '^host' /etc/dhcp/dhcpd.conf | while read line
do
  hostname=$(echo $line | cut -d ' ' -f 2)
  mac=$(echo $line | cut -d '{' -f 2 | cut -d ' ' -f 4 | cut -d';' -f 1)
  ip=$(dig +short $hostname.mydomain.com)

if test -n "$ip"
then
  echo "\$config['dhcpd']['lan']['staticmap']['$index']['mac']=\"$mac\";"
  echo "\$config['dhcpd']['lan']['staticmap']['$index']['cid']=\"$hostname\";"
  echo "\$config['dhcpd']['lan']['staticmap']['$index']['ipaddr']=\"$ip\";"
  echo "\$config['dhcpd']['lan']['staticmap']['$index']['hostname']=\"$hostname\";"
  echo "\$config['dhcpd']['lan']['staticmap']['$index']['descr']=\"Automatically migrated\";"
else
  # echo "NO IP KNOWN FOR $hostname"
  echo -n ""
fi

let index=$index+1
done
echo "write_config();"
echo "exec"

This will generate the following output, ready to paste into the pfSense shell:

global $config;
parse_config(true);
$config['dhcpd']['lan']['staticmap']['0']['mac']="AA:BB:CC:DD:EE:FF";
$config['dhcpd']['lan']['staticmap']['0']['cid']="pi31";
$config['dhcpd']['lan']['staticmap']['0']['ipaddr']="192.168.1.1";
$config['dhcpd']['lan']['staticmap']['0']['hostname']="pi31";
$config['dhcpd']['lan']['staticmap']['0']['descr']="Automatically migrated";
$config['dhcpd']['lan']['staticmap']['1']['mac']="DD:EE:FF:AA:BB:CC";
$config['dhcpd']['lan']['staticmap']['1']['cid']="pi32";
$config['dhcpd']['lan']['staticmap']['1']['ipaddr']="192.168.1.2";
$config['dhcpd']['lan']['staticmap']['1']['hostname']="pi32";
$config['dhcpd']['lan']['staticmap']['1']['descr']="Automatically migrated";
$config['dhcpd']['lan']['staticmap']['2']['mac']="CC:DD:AA:BB:EE:FF";
$config['dhcpd']['lan']['staticmap']['2']['cid']="MobilePhone";
$config['dhcpd']['lan']['staticmap']['2']['ipaddr']="192.168.1.3";
$config['dhcpd']['lan']['staticmap']['2']['hostname']="MobilePhone";
$config['dhcpd']['lan']['staticmap']['2']['descr']="Automatically migrated";
write_config();
exec

Please keep in mind the index starts at 0, valid for an empty list of host names in your pfSense DHCPD configuration. For each already existing entry you have to add 1 to the starting index of 0.