Finding network (external) IP addresses using Python - python

I want to know my internet provider (external) IP address (broadband or something else) with Python.
There are multiple machines are connected to that network. I tried in different way's but I got only the local and public IP my machine. How do I find my external IP address through Python?
Thanks in advance.

Use this script :
import urllib, json
data = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())
print data["ip"]
Without json :
import urllib, re
data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
print data
Other way it was to parse ifconfig (= linux) or ipconfig (= windows) command but take care with translated Windows System
(ipconfig was translated).
Example of lib for linux :
ifparser.

Secure option (with https support)
from requests import get
get('https://ipapi.co/ip/').text
Complete JSON response
P.S. requests module is convenient for https support. You can try httplib though.

You'll have to use an external source you trust. Python2.x:
from urllib import urlopen
import json
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urlopen(url).read())
print(info['ip'])
If you want more info, you can print more values from info.
Non-python oneliner:
wget -q -O- icanhazip.com

You can check out this answer:
https://stackoverflow.com/a/22157882/5687894
TL;DR:
import ipgetter
ip=ipgetter.myip()

In my opinion the simplest solution is
f = requests.request('GET', 'http://myip.dnsomatic.com')
ip = f.text
Thats all.

my website http;//botliam.com/1.php outputs your ip so you only need these 3 lines to get your ip.
import requests
page = requests.get('http://botliam.com/1.php')
ip = page.text
what its doing is:
opens my webpage and calls it "page"
sets "ip" to the contents of the "page"
if you want your own server to return your external ip instead of relying on my site the code for it is below:
<?php
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
echo "$ip";
?>

Related

Python construct a URL with username and password credentials

I'm trying to port some basic code from JavaScript to Python. In JavaScript the URL class makes this trivial:
const url = new URL('https://python.org');
url.username = 'foo';
url.password = 'bar';
console.log(url.href)
https://foo:bar#python.org/
How do I do this natively in Python? In other words, how can this be done using core libraries (without any third party libraries e.g. the requests module)?
Thanks in advance!
Try using f-strings like this:
url = f"https://{username}:{password}#python.org"
print(url)
For instance, I use this technique to configure my applications to be able to access Internet when I am behing a corporate proxy.

How do I check public IP using Python or DDNS with Cloudflare

It's any way to check my public IP addres using Python? I have had an account on Cloudflare and VPS in a home (but dynamic IP). I need to update VPN IP'S before had an account on OVH and DDNS works after migration dose not work.
requests:
import requests
response = requests.get('http://ifconfig.me')
print(response.text)
python 2:
import urllib2
response = urllib2.urlopen('http://ifconfig.me')
print response.read()
python 3:
from urllib import request
response = request.urlopen('http://ifconfig.me')
print(response.read())
There's a bunch of websites online that offer simple APIs to check the IP address you're connecting from.
As an example:
➜ ~ curl 'https://api.ipify.org?format=json'
{"ip":"1.2.3.4"}
You can use a python library (like Requests) to call the API in python code.
# download requests with `pip install requests`
import requests
res = requests.get("https://api.ipify.org?format=json")
your_ip = res.json() # {"ip" : "1.2.3.4"}
Recently I've created a simple docker image that allows you to keep you local ip in sync with Cloudflare AKA Cloudflare Dynamic DNS (DDNS)
check it out here:
https://github.com/marcelowa/cloudflare-dynamic-dns

How To Get Result Of A HTML Page With Python

edit : SORRY THIS IS COMPLETELY WRONG! thanks for answers <3
hello i want to get result of my html page
my url is here : http://Xantia.rzb.IR/PG/IP
result of this page is client ip but in source i have :
<script>function getIP(json) {document.write(json.ip);}</script>
<script src="http://api.ipify.org?format=jsonp&callback=getIP"></script>
<script>function getIP(json) {document.write(json.ip);}</script>
<script src="http://api.ipify.org?format=jsonp&callback=getIP"></script>
and when i use requests.get('http://xantia/pg/ip').text i just get above source
Is there a way to get the result of this URL?
I have a weblog and I want to make an API for getting pubic IP
if can I use PHP or Django pls help because I just have python
I'm sorry because my English is very bad
From the ipify.org own sample code on the homepage, there is a block of code showing how to get the IP value in python:
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
ip = get('https://api.ipify.org').text
print('My public IP address is: {}'.format(ip))
Your question is very vague.
What exactly do you want to achieve?
The request delivers exactly what you request - the source code of http://xantia/pg/ip.
JavaScript is executed on client (in browser, after the response is received) and not on server (before the response is generated).
What you want to achieve exactly? Receive the public ip address of the server or the ip address of visitor of your page?
For the visitors address you can use in your server side php:
<?php
$ip = $_SERVER['REMOTE_ADDRESS'];
echo $ip;
If you want to receive your servers public address, you can do something like in your server side php:
<?php
$response = file_get_contents('http://api.ipify.org/?format=json');
$data = json_decode($response, true);
$ip = $data['ip'];
echo $ip;
Please note that this is just a simple example (which requires that allow_url_fopen is enabled in your php.ini).
Better is to use a http client library (https://packagist.org/?query=http%20client).
You should also handle exceptions - what happens if api.ipfy.org is not reachable?
If this request is made everytime your page is requested, it will slow down the response time of your server. In this case you should cache the IP somewhere - i guess the public server address will not change very often (except you are using a dynamic dns service).

pysimplesoap web service return connection refused

I've created some web services using pysimplesoap like on this documentation:
https://code.google.com/p/pysimplesoap/wiki/SoapServer
When I tested it, I called it like this:
from SOAPpy import SOAPProxy
from SOAPpy import Types
namespace = "http://localhost:8008"
url = "http://localhost:8008"
proxy = SOAPProxy(url, namespace)
response = proxy.dummy(times=5, name="test")
print response
And it worked for all of my web services, but when I try to call it by using an library which is needed to specify the WSDL, it returns "Could not connect to host".
To solve my problem, I used the object ".wsdl()" to generate the correct WSDL and saved it into a file, the WSDL generated by default wasn't correct, was missing variable types and the correct server address...
The server name localhost is only meaningful on your computer. Once outside, other computers won't be able to see it.
1) find out your external IP, with http://www.whatismyip.com/ or another service. Note that IPs change over time.
2) plug the IP in to http://www.soapclient.com/soaptest.html
If your local service is answering IP requests as well as from localhost, you're done!

Getting a machine's external IP address with Python

Looking for a better way to get a machines current external IP #... Below works, but would rather not rely on an outside site to gather the information ... I am restricted to using standard Python 2.5.1 libraries bundled with Mac OS X 10.5.x
import os
import urllib2
def check_in():
fqn = os.uname()[1]
ext_ip = urllib2.urlopen('http://whatismyip.org').read()
print ("Asset: %s " % fqn, "Checking in from IP#: %s " % ext_ip)
I liked the http://ipify.org. They even provide Python code for using their API.
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
ip = get('https://api.ipify.org').content.decode('utf8')
print('My public IP address is: {}'.format(ip))
Python3, using nothing else but the standard library
As mentioned before, one can use an external service like ident.me in order to discover the external IP address of your router.
Here is how it is done with python3, using nothing else but the standard library:
import urllib.request
external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8')
print(external_ip)
Both IPv4 and IPv6 addresses can be returned, based on availability and client preference; use https://v4.ident.me/ for IPv4 only, or https://v6.ident.me/ for IPv6 only.
If you are behind a router which obtains the external IP, I'm afraid you have no other option but to use external service like you do. If the router itself has some query interface, you can use it, but the solution will be very environment-specific and unreliable.
You should use the UPnP protocol to query your router for this information. Most importantly, this does not rely on an external service, which all the other answers to this question seem to suggest.
There's a Python library called miniupnp which can do this, see e.g. miniupnpc/testupnpigd.py.
pip install miniupnpc
Based on their example you should be able to do something like this:
import miniupnpc
u = miniupnpc.UPnP()
u.discoverdelay = 200
u.discover()
u.selectigd()
print('external ip address: {}'.format(u.externalipaddress()))
I prefer this Amazon AWS endpoint:
import requests
ip = requests.get('https://checkip.amazonaws.com').text.strip()
In my opinion the simplest solution is
import requests
f = requests.request('GET', 'http://myip.dnsomatic.com')
ip = f.text
Thats all.
Use requests module:
import requests
myip = requests.get('https://www.wikipedia.org').headers['X-Client-IP']
print("\n[+] Public IP: "+myip)
As simple as running this in Python3:
import os
externalIP = os.popen('curl -s ifconfig.me').readline()
print(externalIP)
Try:
import requests
ip = requests.get('http://ipinfo.io/json').json()['ip']
Hope this is helpful
If you don't want to use external services (IP websites, etc.) You can use the UPnP Protocol.
Do to that we use a simple UPnP client library (https://github.com/flyte/upnpclient)
Install:
pip install upnpclient
Simple Code:
import upnpclient
devices = upnpclient.discover()
if(len(devices) > 0):
externalIP = devices[0].WANIPConn1.GetExternalIPAddress()
print(externalIP)
else:
print('No Connected network interface detected')
Full Code (to get more information as mentioned in the github readme)
In [1]: import upnpclient
In [2]: devices = upnpclient.discover()
In [3]: devices
Out[3]:
[<Device 'OpenWRT router'>,
<Device 'Harmony Hub'>,
<Device 'walternate: root'>]
In [4]: d = devices[0]
In [5]: d.WANIPConn1.GetStatusInfo()
Out[5]:
{'NewConnectionStatus': 'Connected',
'NewLastConnectionError': 'ERROR_NONE',
'NewUptime': 14851479}
In [6]: d.WANIPConn1.GetNATRSIPStatus()
Out[6]: {'NewNATEnabled': True, 'NewRSIPAvailable': False}
In [7]: d.WANIPConn1.GetExternalIPAddress()
Out[7]: {'NewExternalIPAddress': '123.123.123.123'}
If you think and external source is too unreliable, you could pool a few different services. For most ip lookup pages they require you to scrape html, but a few of them that have created lean pages for scripts like yours - also so they can reduce the hits on their sites:
automation.whatismyip.com/n09230945.asp (Update: whatismyip has taken this service down)
whatismyip.org
I use IPGrab because it's easy to remember:
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
ip = get('http://ipgrab.io').text
print('My public IP address is: {}'.format(ip))
There are a few other ways that do not rely on Python checking an external web site, however the OS can. Your primary issue here, is that even if you were not using Python, if you were using the command line, there are no "built-in" commands that can just simply tell you the external (WAN) IP. Commands such as "ip addr show" and "ifconfig -a" show you the server's IP address's within the network. Only the router actually holds the external IP. However, there are ways to find the external IP address (WAN IP) from the command line.
These examples are:
http://ipecho.net/plain ; echo
curl ipinfo.io/ip
dig +short myip.opendns.com #resolver1.opendns.com
dig TXT +short o-o.myaddr.l.google.com #ns1.google.com
Therefore, the python code would be:
import os
ip = os.popen('wget -qO- http://ipecho.net/plain ; echo').readlines(-1)[0].strip()
print ip
OR
import os
iN, out, err = os.popen3('curl ipinfo.io/ip')
iN.close() ; err.close()
ip = out.read().strip()
print ip
OR
import os
ip = os.popen('dig +short myip.opendns.com #resolver1.opendns.com').readlines(-1)[0].strip()
print ip
Or, plug any other of the examples above, into a command like os.popen, os.popen2, os.popen3, or os.system.
P.S. you can use "pip3 install pytis" and use/take a look at, the "getip" program, written in Python.
You can also find it's code here: https://github.com/PyTis/PyTis/blob/development/src/pytis/getip.py
I tried most of the other answers on this question here and came to find that most of the services used were defunct except one.
Here is a script that should do the trick and download only a minimal amount of information:
#!/usr/bin/env python
import urllib
import re
def get_external_ip():
site = urllib.urlopen("http://checkip.dyndns.org/").read()
grab = re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', site)
address = grab[0]
return address
if __name__ == '__main__':
print( get_external_ip() )
import requests
import re
def getMyExtIp():
try:
res = requests.get("http://whatismyip.org")
myIp = re.compile('(\d{1,3}\.){3}\d{1,3}').search(res.text).group()
if myIp != "":
return myIp
except:
pass
return "n/a"
Unfortunately, there is no way to get your external IP address without consulting a computer on the internet. At best, you can get the local network IP address of your network card (which is likely a 192.16.. address).
You can use the whatismyip module to get the external IP addres. It has no dependencies outside the Python 3 standard library. It connects to public STUN servers and what-is-my-ip websites to find the IPv4 or IPv6 address. Run pip install whatismyip
Example:
>>> import whatismyip
>>> whatismyip.amionline()
True
>>> whatismyip.whatismyip() # Prefers IPv4 addresses, but can return either IPv4 or IPv6.
'69.89.31.226'
>>> whatismyip.whatismyipv4()
'69.89.31.226'
>>> whatismyip.whatismyipv6()
'2345:0425:2CA1:0000:0000:0567:5673:23b5'
If the machine is being a firewall then your solution is a very sensible one: the alternative being able to query the firewall which ends-up being very dependent on the type of firewall (if at all possible).
The most simple (non python) working solution I can think of is
wget -q -O- icanhazip.com
I'd like to add a very short Python3 solution which makes use of the JSON API of http://hostip.info.
from urllib.request import urlopen
import json
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urlopen(url).read().decode('utf-8'))
print(info['ip'])
You can of course add some error checking, a timeout condition and some convenience:
#!/usr/bin/env python3
from urllib.request import urlopen
from urllib.error import URLError
import json
try:
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urlopen(url, timeout = 15).read().decode('utf-8'))
print(info['ip'])
except URLError as e:
print(e.reason, end=' ') # e.g. 'timed out'
print('(are you connected to the internet?)')
except KeyboardInterrupt:
pass
In [1]: import stun
stun.get_ip_info()
('Restric NAT', 'xx.xx.xx.xx', 55320)
Working with Python 2.7.6 and 2.7.13
import urllib2
req = urllib2.Request('http://icanhazip.com', data=None)
response = urllib2.urlopen(req, timeout=5)
print(response.read())
Linux only solution.
On Linux Systems, you can use Python to execute a command on the shell. I think it might help someone.
Something like this, (assuming 'dig/drill' is working on the os)
import os
command = "dig TXT +short o-o.myaddr.l.google.com #ns1.google.com | awk -F\'\"\' '{print $2}' "
ip = os.system(command)
For Arch users, please replace 'dig' with 'drill'.
I liked Sergiy Ostrovsky's answer, but I think there is an even tidier way to do this now.
Install ipify library.
pip install ipify
Import and use the library in your Python program.
import ipify
ip = ipify.get_ip()
ipWebCode = urllib.request.urlopen("http://ip.nefsc.noaa.gov").read().decode("utf8")
ipWebCode=ipWebCode.split("color=red> ")
ipWebCode = ipWebCode[1]
ipWebCode = ipWebCode.split("</font>")
externalIp = ipWebCode[0]
this is a short snippet I had written for another program. The trick was finding a simple enough website so that dissecting the html wasn't a pain.
Here's another alternative script.
def track_ip():
"""
Returns Dict with the following keys:
- ip
- latlong
- country
- city
- user-agent
"""
conn = httplib.HTTPConnection("www.trackip.net")
conn.request("GET", "/ip?json")
resp = conn.getresponse()
print resp.status, resp.reason
if resp.status == 200:
ip = json.loads(resp.read())
else:
print 'Connection Error: %s' % resp.reason
conn.close()
return ip
EDIT: Don't forget to import httplib and json
If you're just writing for yourself and not for a generalized application, you might be able to find the address on the setup page for your router and then scrape it from that page's html. This worked fine for me with my SMC router. One read and one simple RE search and I've found it.
My particular interest in doing this was to let me know my home IP address when I was away from home, so I could get back in via VNC. A few more lines of Python stores the address in Dropbox for outside access, and even emails me if it sees a change. I've scheduled it to happen on boot and once an hour thereafter.
Use this script :
import urllib, json
data = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())
print data["ip"]
Without json :
import urllib, re
data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
print data
import os
public_ip = os.system("inxi -i |grep 'WAN IP'")
print(public_ip)
If you are not interested in hitting any url to get public ip, I think following code can help you to get public ip using python of your machine
import os
externalIP = os.popen("ifconfig | grep 'inet' | cut -d: -f2 | awk '{print $2}' | sed -n 3p").readline()
print externalIP
sed -n 3p line varies as per the network you are using for connecting device.
I was facing same issue, I was needed public ip of iot device which is hitting my server. but public ip is totally different in ifconfig command and ip i am getting in server from request object. after this I am adding extra param into my request to send ip of device to my server.
hope this is helpful
import os
externalIp = os.popen("ipconfig").read().split(":")[15][1:14]
some numbers may need to be changed but this works for me

Categories