{"id":175,"date":"2016-03-28T19:31:24","date_gmt":"2016-03-28T17:31:24","guid":{"rendered":"http:\/\/www.geohei.lu\/geoheiWP\/?p=175"},"modified":"2016-04-05T19:25:15","modified_gmt":"2016-04-05T17:25:15","slug":"dns-stock-update-script","status":"publish","type":"post","link":"https:\/\/www.geohei.lu\/geoheiWP\/dns-stock-update-script\/","title":{"rendered":"DNS Stock update script"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-173\" src=\"https:\/\/i0.wp.com\/www.geohei.lu\/geoheiWP\/wp-content\/uploads\/2016\/04\/dnsstock_update_screenshot.png?resize=300%2C200\" alt=\"dnsstock_update_screenshot\" width=\"300\" height=\"200\" \/>This script updates the DNS records of <a href=\"http:\/\/www.dns-stock.com\/\" target=\"_blank\">DNS Stock<\/a>.<\/p>\n<p>It generates an URL <span class=\"highlight\" style=\"background-color: #303030;\"><code>http:\/\/dyndns.dns-stock.com\/?hostname=&lt;domain&gt;&amp;username=&lt;username&gt;&amp;password=&lt;pass&gt;&amp;myip=&lt;ipaddr&gt;<\/code><\/span>, which is eventually passed to the DNS Stock server. The code is basically about parsing, validating, checking and formatting the returned WAN IP <span class=\"highlight\" style=\"background-color: #303030;\"><code>&lt;ipaddr&gt;<\/code><\/span>. Two URLs are configured to check WAN IP. If the first one returns an error, the second one is queried. If that one fails as well, the script abandons. The logfile <span class=\"highlight\" style=\"background-color: #303030;\"><code>dnsstock_update.log<\/code><\/span> reveals relevant details about what happened.<\/p>\n<p>Configured passwords can either be plain text (depreciated) or MD5 crypted (preferred), as visible on DNS Stock site. The DNS Stock server accepts both formats.<\/p>\n<p>Code and logfile entries should be self-explanatory.<\/p>\n<p>Download: <a href=\"https:\/\/www.geohei.lu\/downloads\/geoheiWP\/dnsstock_update.sh\">dnsstock_update.sh<\/a><\/p>\n<p>Comments are desired.<\/p>\n<p>Thanks,<br \/>\n<!--more--><\/p>\n<hr \/>\n<pre>#!\/bin\/sh\r\n\r\n# dnsstock_update.sh\r\n#\r\n# dns stock DDNS update script\r\n# forIPv4 only\r\n# by geohei <georges@geohei.lu>\r\n# created : 28.03.2016\r\n# revised : 02.04.2016 - added 'dyndns.dns-stock.com\/my-ip.php' as primary check ip url\r\n#\r\n# dns stock update url syntax\r\n# http:\/\/dyndns.dns-stock.com\/?hostname=<domain>&username=<username>&password=<pass>&myip=<ipaddr>\r\n#\r\n# use cron to trigger (suggested every minute) e.g. '* * * * * ~\/dnsstock_update.sh'\r\n#\r\n# $tmpdir\r\n# is used for 'dnsstock_update.cache' and 'dnsstock_update.log' files\r\n# omit trailing '\/'\r\n\r\ndomain=''\r\nusername=''\r\npassword=''\r\ncheckipurl_1='dyndns.dns-stock.com\/my-ip.php'\r\ncheckipurl_2='ipv4.whatismyv6.com'\r\ntmpdir='\/tmp'\r\n\r\n# parse IPv4 for $checkipurl_1\r\nipaddr=$(wget -q -O - $checkipurl_1)\r\n# check if wget returned != 0\r\nif [ $? != 0 ]; then\r\n  # parse IPv4 for 'ipv4.whatismyv6.com'\r\n  ipaddr=$(wget -q -O - $checkipurl_2 | grep -iohP '(?<=\\x3e)([0-9]+\\.){3}[0-9]+(?=\\x3c)')\r\n  # check if wget returned != 0\r\n  if [ $? != 0 ]; then\r\n    exit 1\r\n  else\r\n    checkipurl_use='(2)'\r\n  fi\r\nelse\r\n  checkipurl_use='(1)'\r\nfi\r\n\r\n# exit 0 if $ipaddr is cached (= didn't change)\r\nif [ -e $tmpdir\/dnsstock_update.cache ] &#038;&#038; [ $(cat $tmpdir\/dnsstock_update.cache) = $ipaddr ]; then\r\n  exit 0\r\nfi\r\n\r\n# check if IP valid (>255 check and IP syntax check)\r\nif expr \"$ipaddr\" : '[0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*$' >\/dev\/null; then\r\n  for i in 1 2 3 4; do\r\n    # >255 check\r\n    if [ $(echo \"$ipaddr\" | cut -d. -f$i) -gt 255 ]; then\r\n      # write log entry\r\n      echo \"$(date '+%Y-%m-%d %H:%M:%S')   FAIL$checkipurl_use: IPv4 invalid: $ipaddr (>255 check failed).\" >> $tmpdir\/dnsstock_update.log\r\n      exit 1\r\n    fi\r\n  done\r\n  # update dns\r\n  success=$(wget -q -O - \"http:\/\/dyndns.dns-stock.com\/?hostname=$domain&username=$username&password=$password&myip=$ipaddr\")\r\n  if [ ! -z $update ]; then\r\n    # write log entry\r\n    echo \"$(date '+%Y-%m-%d %H:%M:%S')   FAIL$checkipurl_use: dns stock server returned error.\" >> $tmpdir\/dnsstock_update.log\r\n    exit 1\r\n  fi\r\n  # update dnsstock_update.cache\r\n  echo $ipaddr > $tmpdir\/dnsstock_update.cache\r\n  # write log entry\r\n  echo \"$(date '+%Y-%m-%d %H:%M:%S')   SUCCESS$checkipurl_use: IPv4 valid and updated: '$ipaddr'.\" >> $tmpdir\/dnsstock_update.log\r\n  exit 0\r\nelse\r\n  # write log entry\r\n  echo \"$(date '+%Y-%m-%d %H:%M:%S')   FAIL$checkipurl_use: IPv4 invalid: '$ipaddr' (IP syntax check failed).\" >> $tmpdir\/dnsstock_update.log\r\n  exit 1\r\nfi\r\n\r\nexit 0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This script updates the DNS records of DNS Stock. It generates an URL http:\/\/dyndns.dns-stock.com\/?hostname=&lt;domain&gt;&amp;username=&lt;username&gt;&amp;password=&lt;pass&gt;&amp;myip=&lt;ipaddr&gt;, which is eventually passed to the DNS Stock server. The code is basically about parsing, validating, checking and formatting the returned WAN IP &lt;ipaddr&gt;. Two URLs are configured to check WAN IP. If the first one returns an error, the second&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/www.geohei.lu\/geoheiWP\/dns-stock-update-script\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[23,3],"tags":[27,24,25,26,22],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-coding","category-ubuntu","tag-coding","tag-dns","tag-dns-stock","tag-script","tag-ubuntu"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5mjlH-2P","_links":{"self":[{"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":12,"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":201,"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/posts\/175\/revisions\/201"}],"wp:attachment":[{"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.geohei.lu\/geoheiWP\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}