Home 0P3N Blog External IP Address Search Using Python Source Code
Ready to Start Your Career?
Create Free Account
Bmsr256 s profile image
By: Bmsr256
October 18, 2017

External IP Address Search Using Python Source Code

By: Bmsr256
October 18, 2017
Bmsr256 s profile image
By: Bmsr256
October 18, 2017
This time, I'm providing a Python source code tool designed to fetch your external IP address from the Internet. This is used mostly when you're behind a NAT. It picks your IP randomly from a server list to minimize request overhead on a single server.You need to import re and random libraries for this tool to perfectly run. Here is the source code:`#!/usr/bin/env python"""API Usage=========>>> import ipgetter>>> myip = ipgetter.myip()>>> myip'8.8.8.8'>>> ipgetter.IPgetter().test()Number of servers: 47IP's :8.8.8.8 = 47 ocurrencies"""import reimport randomPY3K = version_info >= (3, 0)if PY3K:import urllib.request as urllibelse:import urllib2 as urllib__version__ = "0.4"def myip():return IPgetter().get_externalip()class IPgetter(object):'''This class is designed to fetch your external IP address from the internet.It is used mostly when behind a NAT.It picks your IP randomly from a server list to minimize request overheadon a single server.'''def __init__(self):self.server_list = ['https://ip.dnsexit.com','http://ifconfig.me/ip','http://echoip.com','http://ipecho.net/plain','http://checkip.dyndns.org/plain','http://whatismyipaddress.com/','http://websiteipaddress.com/WhatIsMyIp','http://getmyipaddress.org/','http://www.my-ip-address.net/','http://myexternalip.com/raw','http://www.canyouseeme.org/','http://www.trackip.net/','http://icanhazip.com/','http://www.iplocation.net/','http://www.howtofindmyipaddress.com/','http://www.ipchicken.com/','http://whatsmyip.net/','http://www.ip-adress.com/','http://checkmyip.com/','http://www.tracemyip.org/','http://www.lawrencegoetz.com/programs/ipinfo/','http://www.findmyip.co/','http://ip-lookup.net/','http://www.dslreports.com/whois','http://www.mon-ip.com/en/my-ip/','http://www.myip.ru','http://ipgoat.com/','http://www.myipnumber.com/my-ip-address.asp','http://www.whatsmyipaddress.net/','http://formyip.com/','https://check.torproject.org/','http://www.displaymyip.com/','http://www.bobborst.com/tools/whatsmyip/','http://www.geoiptool.com/','https://www.whatsmydns.net/whats-my-ip-address.html','https://www.privateinternetaccess.com/pages/whats-my-ip/','http://checkip.dyndns.com/','http://myexternalip.com/','http://www.ip-adress.eu/','http://www.infosniper.net/','https://wtfismyip.com/text','http://ipinfo.io/','http://httpbin.org/ip','https://diagnostic.opendns.com/myip','http://checkip.amazonaws.com','https://api.ipify.org','https://v4.ident.me']def get_externalip(self):'''This function gets your IP from a random server'''myip = ''for i in range(7):myip = self.fetch(random.choice(self.server_list))if myip != '':return myipelse:continuereturn ''def fetch(self, server):'''This function gets your IP from a specific server.'''url = Noneopener = urllib.build_opener()opener.addheaders = [('User-agent',"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0")]try:url = opener.open(server, timeout=2)content = url.read()# Didn't want to import chardet. Prefered to stick to stdlibif PY3K:try:content = content.decode('UTF-8')except UnicodeDecodeError:content = content.decode('ISO-8859-1')m = re.search('(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)',content)myip = m.group(0)return myip if len(myip) > 0 else ''except Exception:return ''finally:if url:url.close()def test(self):'''This functions tests the consistency of the serverson the list when retrieving your IP.All results should be the same.'''resultdict = {}for server in self.server_list:resultdict.update(**{server: self.fetch(server)})ips = sorted(resultdict.values())ips_set = set(ips)print('nNumber of servers: {}'.format(len(self.server_list)))print("IP's :")for ip, ocorrencia in zip(ips_set, map(lambda x: ips.count(x), ips_set)):print('{0} = {1} ocurrenc{2}'.format(ip if len(ip) > 0 else 'broken server', ocorrencia, 'y' if ocorrencia == 1 else 'ies'))print('n')print(resultdict)if __name__ == '__main__':print(myip())` I hope this source code tool can be useful to you. If you have any questions you can contact me on the comments section or send me a message.
Schedule Demo

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry