{"id":2263,"date":"2010-11-20T10:10:39","date_gmt":"2010-11-20T14:10:39","guid":{"rendered":"http:\/\/www.databook.bz\/?page_id=2263"},"modified":"2019-10-22T21:58:09","modified_gmt":"2019-10-23T01:58:09","slug":"2263-2","status":"publish","type":"page","link":"http:\/\/www.linux-databook.info\/?page_id=2263","title":{"rendered":"BASH Code to Determine Valid IP Addresses"},"content":{"rendered":"<p>There are times when it is desirable to determine programmatically whether a an IP Address is valid or not. This little BASH code snippet does exactly that. The return code will be 0 if it is a valid IP address and 1 if not.<\/p>\n<blockquote>\n<pre><span style=\"font-family: courier new,courier;\">\n################################################################################\n# Determine whether an arbitrary set of four octets of numeric data separated  #\n# by periods is a valid IP address.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\n# Note: This code was taken from Linux Journal June 26, 2008\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\n# <a href=\"http:\/\/www.linuxjournal.com\/content\/validating-ip-address-bash-script\">http:\/\/www.linuxjournal.com\/content\/validating-ip-address-bash-script<\/a>        #\n################################################################################ \nvalid_ip() \n{\n   local ip=$1\n   local\n   stat=1\n   # Check the IP address under test to see if it matches the extended REGEX\n   if [[ $ip =~ ^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$ ]]\n   then\n      # Record current default field separator\n      OIFS=$IFS\n      # Set default field separator to .\n      IFS='.'\n      # Create an array of the IP Address being tested\n      ip=($ip)\n      # Reset the field separator to the original saved above IFS=$OIFS\n      # Is each octet between 0 and 255?\n      [[ ${ip[0]} -le 255 &amp;&amp; ${ip[1]} -le 255 &amp;&amp; ${ip[2]} -le 255 &amp;&amp; ${ip[3]} -le 255 ]]\n      # Set the return code.\n      stat=$?\n      fi\n}\n\n<\/span><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>There are times when it is desirable to determine programmatically whether a an IP Address is valid or not. This little BASH code snippet does exactly that. The return code will be 0 if it is a valid IP address&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.linux-databook.info\/?page_id=2263\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":231,"menu_order":3,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2263","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/2263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2263"}],"version-history":[{"count":10,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/2263\/revisions"}],"predecessor-version":[{"id":5646,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/2263\/revisions\/5646"}],"up":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/231"}],"wp:attachment":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}