{"id":482,"date":"2013-08-25T14:21:25","date_gmt":"2013-08-25T12:21:25","guid":{"rendered":"http:\/\/bergs.biz\/blog\/?p=482"},"modified":"2013-08-25T14:21:25","modified_gmt":"2013-08-25T12:21:25","slug":"how-to-roll-your-own-dyndns","status":"publish","type":"post","link":"https:\/\/bergs.biz\/blog\/2013\/08\/25\/how-to-roll-your-own-dyndns\/","title":{"rendered":"How to roll your own DynDNS&#8230;"},"content":{"rendered":"<p>I didn&#8217;t want to rely upon services like <code>DynDNS.org<\/code> (which obviously was a smart decision since they now pretty much closed their free service) so I rolled my own&#8230;<\/p>\n<p>What you need is the following:<\/p>\n<ol>\n<li>Host your domain yourself using the popular nameserver &#8220;Bind.&#8221;<\/li>\n<li>Host a small CGI script that will tell you your external IP (or use one of the many free services available that do the same).<\/li>\n<li>Run a machine within your LAN 24&#215;7 which can detect changes of your external IP and update your hostname accordingly.<\/li>\n<\/ol>\n<h2>Step 1: Setup Bind for Dynamic DNS Update<\/h2>\n<p>to do<\/p>\n<h2>Step 2: CGI Script<\/h2>\n<p>The CGI script that needs to be deployed somewhere in the Internet to tell you your external IP is very simple and tiny and looks like this:<\/p>\n<pre>#!\/bin\/bash\r\necho \"Content-type: text\/plain\"\r\necho \"\"\r\necho \"$REMOTE_ADDR\"<\/pre>\n<h2>Step 3: External IP Probe<\/h2>\n<p>Here&#8217;s the script that needs to run periodically on a machine (I use Ubuntu server) within your LAN (or on your Internet gateway, although if you have the means to run stuff on your gateway you could employ a more elegant, &#8220;proper&#8221; solution):<\/p>\n<pre>#!\/bin\/bash\r\nlockfile=\"\/run\/extip\"\r\nlockfile-check $lockfile\r\nif [ $? -eq 0 ]; then\r\n\u00a0\u00a0 \u00a0echo \"Locked, bailing out...\"\r\n\u00a0\u00a0 \u00a0exit 1\r\nfi\r\n\r\nlockfile-create $lockfile\r\nfilename=\"\/var\/lib\/extip.txt\"\r\nlogfile=\"\/var\/log\/extip.log\"\r\nkeyfile=\"\/root\/var\/lib\/dyndns\/Kmyhost.dyn.example.org.+163+56719.key\"\r\ncur_ip=`curl -s http:\/\/example.org\/cgi-bin\/myip.sh`\r\nprev_ip=`cat $filename`\r\nif [ $cur_ip != $prev_ip ]; then\r\n\u00a0\u00a0 \u00a0echo \"`date --rfc-3339=seconds` IP changed, old IP: $prev_ip, new IP: $cur_ip\" &gt;&gt;$logfile\r\n\u00a0\u00a0 \u00a0echo \"$cur_ip\" &gt;$filename\r\n\u00a0\u00a0 \u00a0# Wait 5 sec to complete, force kill if nsupdate not done after 10 sec\r\n\u00a0\u00a0 \u00a0timeout -k 10s 5s nsupdate -k $keyfile -v&lt;&lt;EOF\r\nserver example.org\r\nzone dyn.example.org.\r\nupdate delete myhost.dyn.example.org. A\r\nupdate add myhost.dyn.example.org. 60 A $cur_ip\r\nsend\r\nEOF\r\nfi\r\nlockfile-remove $lockfile<\/pre>\n<p>The above script &#8212; even though it&#8217;s pretty small &#8212; is not a quick&#8217;n&#8217;dirty hack, but even employs some sanity checks:<\/p>\n<ul>\n<li>It makes sure that only <em>one<\/em> instance is running at any time, and<\/li>\n<li>it uses the <code>timeout<\/code> command from the Linux <code>coreutils<\/code> package to enforce that the <code>nsupdate<\/code> command will be terminated if it takes longer than 10 s (e. g. due to network issues).<\/li>\n<\/ul>\n<p>I run the above script once a minute as follows:<\/p>\n<pre># cat \/etc\/cron.d\/extip \r\n* * * * * root \/usr\/local\/bin\/pubextip.sh<\/pre>\n<p>That&#8217;s it!<\/p>\n<p>Let me know what you think. Suggestions how to improve things are, as always, very welcome!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t want to rely upon services like DynDNS.org (which obviously was a smart decision since they now pretty much closed their free service) so I rolled my own&#8230; What you need is the following: Host your domain yourself using the popular nameserver &#8220;Bind.&#8221; Host a small CGI script that will tell you your external [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[81,93],"class_list":["post-482","post","type-post","status-publish","format-standard","hentry","category-linux-computers","tag-dynamic-dns","tag-dyndns"],"_links":{"self":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":4,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts\/482\/revisions\/486"}],"wp:attachment":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}