check for IP and MAC addresses

This commit is contained in:
rasbt 2014-07-06 23:29:57 -04:00
parent 59851864c4
commit c24f1c6532
4 changed files with 193 additions and 3 deletions

BIN
Images/Ipv4_address.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
Images/Ipv6_address.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
Images/MACaddressV3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:295b519fef2aba4116d805cb3c00fc582bdc1a0b7da29b1ae36415f606592010"
"signature": "sha256:68319934ddacbbabceac87b1876ae59e0d4fc3509192906a9483958d08c8d839"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -41,7 +41,7 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"Last updated: 06/07/2014 12:24:58 EDT\n",
"Last updated: 06/07/2014 22:50:23 EDT\n",
"\n",
"CPython 3.4.1\n",
"IPython 2.1.0\n"
@ -104,7 +104,8 @@
"- [Checking for numbers](#Checking-for-numbers)\n",
"- [Validating dates](#Validating-dates)\n",
"- [Time](#Time)\n",
"- [Checking for HTML tags](#Checking-for-HTML-tags)"
"- [Checking for HTML tags](#Checking-for-HTML-tags)\n",
"- [Checking for IP addresses](#Checking-for-IP-addresses)"
]
},
{
@ -871,6 +872,195 @@
"source": [
"<font size=\"1px\">source: [http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/](http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/)</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Checking for IP addresses"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#Sections)]"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"IPv4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](../Images/Ipv4_address.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"1px\">Image source: http://en.wikipedia.org/wiki/File:Ipv4_address.svg</font>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pattern = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'\n",
"\n",
"str_true = ('172.16.254.1', '1.2.3.4', '01.102.103.104', )\n",
" \n",
"str_false = ('17216.254.1', '1.2.3.4.5', '01 .102.103.104', )\n",
"\n",
"for t in str_true:\n",
" assert(bool(re.match(pattern, t)) == True), '%s is not True' %t\n",
"\n",
"for f in str_false:\n",
" assert(bool(re.match(pattern, f)) == False), '%s is not False' %f"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"1px\">source: [http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/](http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/)</font>"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Ipv6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](../Images/Ipv6_address.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"1px\">Image source: http://upload.wikimedia.org/wikipedia/commons/1/15/Ipv6_address.svg</font>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pattern = r'^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$'\n",
"\n",
"str_true = ('2001:470:9b36:1::2',\n",
" '2001:cdba:0000:0000:0000:0000:3257:9652', \n",
" '2001:cdba:0:0:0:0:3257:9652', \n",
" '2001:cdba::3257:9652', )\n",
" \n",
"str_false = ('1200::AB00:1234::2552:7777:1313', # uses `::` twice\n",
" '1200:0000:AB00:1234:O000:2552:7777:1313', ) # contains an O instead of 0\n",
"\n",
"for t in str_true:\n",
" assert(bool(re.match(pattern, t)) == True), '%s is not True' %t\n",
"\n",
"for f in str_false:\n",
" assert(bool(re.match(pattern, f)) == False), '%s is not False' %f"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"1px\">source: [http://snipplr.com/view/43003/regex--match-ipv6-address/](http://snipplr.com/view/43003/regex--match-ipv6-address/)</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Checking for MAC addresses"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](../Images/MACaddressV3.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"1px\">Image source: http://upload.wikimedia.org/wikipedia/en/3/37/MACaddressV3.png </font>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pattern = r'^(?i)([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$'\n",
"\n",
"str_true = ('94-AE-70-A0-66-83', \n",
" '58-f8-1a-00-44-c8',\n",
" '00:A0:C9:14:C8:29'\n",
" , )\n",
" \n",
"str_false = ('0:00:00:00:00:00', \n",
" '94-AE-70-A0 -66-83', ) \n",
"\n",
"for t in str_true:\n",
" assert(bool(re.match(pattern, t)) == True), '%s is not True' %t\n",
"\n",
"for f in str_false:\n",
" assert(bool(re.match(pattern, f)) == False), '%s is not False' %f"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 29
}
],
"metadata": {}