{"id":70,"date":"2013-01-15T12:52:27","date_gmt":"2013-01-15T11:52:27","guid":{"rendered":"http:\/\/rubinstein.me\/?p=70"},"modified":"2014-10-27T13:28:18","modified_gmt":"2014-10-27T13:28:18","slug":"validate-ip-addresses-and-slash-notation-network-addresses-in-python-using-regular-expressions","status":"publish","type":"post","link":"http:\/\/iict-space.heig-vd.ch\/ars\/2013\/01\/15\/validate-ip-addresses-and-slash-notation-network-addresses-in-python-using-regular-expressions\/","title":{"rendered":"Validate IP Addresses and Slash Notation"},"content":{"rendered":"<p>I&#8217;m currently working on a project that requires the user to provide a target IPv4 address within a LAN for a network analysis. The address may indicate a single host (i.e. 10.0.2.25) or it may use &#8220;slash notation&#8221; to designate a group of IP addresses according to the\u00a0<a href=\"http:\/\/www.ripe.net\/internet-coordination\/press-centre\/understanding-ip-addressing\" target=\"_blank\">Classless Inter-Domain Routing (CIDR) Chart<\/a> (i.e. 10.0.2.0\/24):<\/p>\n<p><a href=\"http:\/\/rubinstein.me\/wp-content\/uploads\/2013\/01\/cidr_chart-2.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-72\" src=\"http:\/\/rubinstein.me\/wp-content\/uploads\/2013\/01\/cidr_chart-2.jpg\" alt=\"cidr_chart-2\" width=\"426\" height=\"601\" \/><\/a><\/p>\n<p>Google found several regular expressions that were supposed to filter out valid IP addresses but none of them included the slash notation, and many of them didn&#8217;t even work correctly.<\/p>\n<p>I finally decided to write my own from scratch and put it all in a function to determine if an IP address (or network) is valid. I believe it works as expected:<\/p>\n<pre class=\"expand:true lang:python decode:true\">#!\/usr\/bin\/env python\r\n\r\n\"\"\"\r\nValidate IP and network addresses.\r\n\r\nCreated by Abraham Rubinstein on 2013-01-15\r\nCopyright (c) 2013 HEIG-VD. All rights reserved.\r\n\"\"\"\r\n\r\nimport re\r\n\r\ndef isValidIP(ip):\r\n    regex = (\r\n            \"^(([0-9]|[1-9][0-9]|1[0-9]{2}|[1-2][0-4][0-9]|25[0-5]).)\" #Match i.e 5. or 27. or 255.\r\n            \"{3}\"                                                       #Apply 3 times the last rule... i.e. 10.0.3.\r\n            \"([0-9]|[1-9][0-9]|1[0-9]{2}|[1-2][0-4][0-9]|25[0-5])\"      #Last number doesn't end with a period\r\n            \"((\/[0-9]|\/[1-2][0-9]|\/[1-3][0-2])?)$\"                   #Optionally match slash notation\r\n            )\r\n    return re.match(regex,ip)<\/pre>\n<p>You may freely use this code if you wish, but I&#8217;m not officially releasing it under any open source license because I need my other code that integrates this to remain closed source.<\/p>\n<p>Let me know if this helps you!<\/p>\n<p><strong>Update<\/strong>: Found a little bug and corrected it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently working on a project that requires the user to provide a target IPv4 address within a LAN for a network analysis. The address may indicate a single host (i.e. 10.0.2.25) or it may use &#8220;slash notation&#8221; to designate <a class=\"more-link\" href=\"http:\/\/iict-space.heig-vd.ch\/ars\/2013\/01\/15\/validate-ip-addresses-and-slash-notation-network-addresses-in-python-using-regular-expressions\/\">Continue reading <span class=\"screen-reader-text\">  Validate IP Addresses and Slash Notation<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,4,15,3],"tags":[9,10,12,11],"_links":{"self":[{"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":1,"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":72,"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/posts\/70\/revisions\/72"}],"wp:attachment":[{"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iict-space.heig-vd.ch\/ars\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}