Add host to DNS via script

I recently created a script to add my Linux hosts to DNS. Since we use static IPs, the DNS entries to not get dynamically added. It's a rather simple script, and could have been dome completely in bash. But, I wrote it in perl. I figured I would share this in case anyone was looking for a script to do the same thing. Depending on your needs, you may have to do more work, say for multi-homed servers, etc.

#!/usr/bin/perl

$domain = ".example.com";
$hostname = `hostname`;
chomp($hostname);

$ipaddr = `hostname -i`;
chomp($ipaddr);
$ipaddr_rev = `echo -n \"$ipaddr.\" | tac -s.`;
chomp($ipaddr_rev);

open(OUT,">/tmp/${0}.update");
print OUT "update add ${hostname}${domain} 86400 a ${ipaddr}\nsend\n";
print OUT "update add ${ipaddr_rev}in-addr.arpa 86400 ptr ${hostname}${domain}\nsend\n";
close(OUT);

`nsupdate -v /tmp/${0}.update`;
unlink("/tmp/${0}.update");

Comments