Last active
October 11, 2024 14:22
-
-
Save ChristophShyper/6e5e246f0c0c9f3452d13b773ea4ce28 to your computer and use it in GitHub Desktop.
Google Colab notebook getting external IP plus CIDR and network name from Whois. Great for figuring out CIDR blocks to whitelist for databases access by Colab.
This file contains 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "check_ip_and_cidr.ipynb", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ChristophShyper/6e5e246f0c0c9f3452d13b773ea4ce28/check_ip_and_cidr.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "toT_MSf70AyX", | |
"colab_type": "code", | |
"outputId": "831bebf9-cf26-4acb-8b7b-fa1f794269e4", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 136 | |
} | |
}, | |
"source": [ | |
"!pip install ipwhois\n", | |
"\n", | |
"from ipwhois import IPWhois\n", | |
"from requests import get\n", | |
"\n", | |
"ip = get('https://api.ipify.org').text\n", | |
"whois = IPWhois(ip).lookup_rdap(depth=1)\n", | |
"cidr = whois['network']['cidr']\n", | |
"name = whois['network']['name']\n", | |
"\n", | |
"print('\\n')\n", | |
"print('Provider: ', name)\n", | |
"print('Public IP: ', ip)\n", | |
"print('CIDRs: ', cidr)" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: ipwhois in /usr/local/lib/python3.6/dist-packages (1.1.0)\n", | |
"Requirement already satisfied: dnspython in /usr/local/lib/python3.6/dist-packages (from ipwhois) (1.16.0)\n", | |
"\n", | |
"\n", | |
"Provider: GOOGLE-CLOUD\n", | |
"Public IP: 104.155.213.181\n", | |
"CIDRs: 104.154.0.0/15\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "blOgaGx1qkpT", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment