Last active
July 22, 2020 07:01
-
-
Save anilshrish/8e826bbb4e488c0a39f0aa058198a95e to your computer and use it in GitHub Desktop.
Networking - using python to find ips in CIDR and find CIDR for IP ranges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"## Get CIDR Range from IP Range" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[IPv4Network('10.69.129.84/30'),\n", | |
" IPv4Network('10.69.129.88/30'),\n", | |
" IPv4Network('10.69.129.92/31'),\n", | |
" IPv4Network('10.69.129.94/32')]" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ipaddress\n", | |
"startip = ipaddress.IPv4Address('10.69.129.84')\n", | |
"endip = ipaddress.IPv4Address('10.69.129.94')\n", | |
"\n", | |
"[ipaddr for ipaddr in ipaddress.summarize_address_range(startip, endip)]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Collecting netaddr\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/ff/cd/9cdfea8fc45c56680b798db6a55fa60a22e2d3d3ccf54fc729d083b50ce4/netaddr-0.8.0-py2.py3-none-any.whl (1.9MB)\n", | |
"\u001b[K 100% |████████████████████████████████| 1.9MB 15.0MB/s ta 0:00:01\n", | |
"\u001b[?25hCollecting importlib-resources; python_version < \"3.7\" (from netaddr)\n", | |
" Downloading https://files.pythonhosted.org/packages/ba/03/0f9595c0c2ef12590877f3c47e5f579759ce5caf817f8256d5dcbd8a1177/importlib_resources-3.0.0-py2.py3-none-any.whl\n", | |
"Collecting zipp>=0.4; python_version < \"3.8\" (from importlib-resources; python_version < \"3.7\"->netaddr)\n", | |
" Downloading https://files.pythonhosted.org/packages/b2/34/bfcb43cc0ba81f527bc4f40ef41ba2ff4080e047acb0586b56b3d017ace4/zipp-3.1.0-py3-none-any.whl\n", | |
"Installing collected packages: zipp, importlib-resources, netaddr\n", | |
"Successfully installed importlib-resources-3.0.0 netaddr-0.8.0 zipp-3.1.0\n", | |
"\u001b[33mYou are using pip version 10.0.1, however version 20.2b1 is available.\n", | |
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" | |
] | |
} | |
], | |
"source": [ | |
"import sys\n", | |
"!{sys.executable} -m pip install netaddr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10.241.199.228\n", | |
"10.241.199.229\n" | |
] | |
} | |
], | |
"source": [ | |
"## Get AlL Possible IPs from CIDR\n", | |
"import netaddr\n", | |
"for ip in netaddr.IPNetwork('10.241.199.228/31'):\n", | |
" print(ip)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[IPv4Network('10.241.200.199/32'), IPv4Network('10.241.200.200/32')]" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ipaddress\n", | |
"startip = ipaddress.IPv4Address('10.241.200.199')\n", | |
"endip = ipaddress.IPv4Address('10.241.200.200')\n", | |
"\n", | |
"[ipaddr for ipaddr in ipaddress.summarize_address_range(startip, endip)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[IPv4Network('10.10.200.199/32'), IPv4Network('10.10.200.200/32')]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ipaddress\n", | |
"startip = ipaddress.IPv4Address('10.10.200.199')\n", | |
"endip = ipaddress.IPv4Address('10.10.200.200')\n", | |
"\n", | |
"[ipaddr for ipaddr in ipaddress.summarize_address_range(startip, endip)]" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [conda env:.conda]", | |
"language": "python", | |
"name": "conda-env-.conda-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment