Getting external IP address

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

I have a PC behind a firewall, and I'm trying to programmatically
determine the IP address visible from outside the firewall.

If I do this:
('localhost.localdomain', ['localhost'], ['127.0.0.1'])

I get my internal IP address, which is not what I want.

Other tricks, like parsing the output of os.system('/sbin/ifconfig eth0')
also give me my internal IP address.

I found this post on comp.lang.python a few years ago:

http://mail.python.org/pipermail/python-list/2003-March/192495.html

which seems like it _should_ do what I want, but it doesn't: I get an
exception.
from httplib import HTTPConnection
from xml.dom.ext.reader.Sax import FromXmlStream
conn = HTTPConnection('xml.showmyip.com')
conn.request('GET', '/')
doc = FromXmlStream(conn.getresponse())
print doc.getElementsByTagName('ip')[0].firstChild.data
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/UserList.py", line 28, in __getitem__
def __getitem__(self, i): return self.data
IndexError: list index out of range
I don't know how to fix it so that it works.

Can anyone help me fix that code snippet, or suggest another (better) way
to get the externally visible IP address?
 
D

Duncan Booth

Steven D'Aprano said:
from httplib import HTTPConnection
from xml.dom.ext.reader.Sax import FromXmlStream
conn = HTTPConnection('xml.showmyip.com')
conn.request('GET', '/')
doc = FromXmlStream(conn.getresponse())
print doc.getElementsByTagName('ip')[0].firstChild.data
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/UserList.py", line 28, in __getitem__
def __getitem__(self, i): return self.data
IndexError: list index out of range
I don't know how to fix it so that it works.

Can anyone help me fix that code snippet, or suggest another (better) way
to get the externally visible IP address?


Try running it interactively and looking at the data you receive:
<html><head><title>Object moved</title></head><body>

<h2>Object moved to <a href="http://www.showmyip.com/xml/">here</a>.
</h2>

</body></html>


If you try connecting to 'www.showmyip.com' and requesting '/xml/' it
should work.
 
P

Paul Rubin

Duncan Booth said:
If you try connecting to 'www.showmyip.com' and requesting '/xml/' it
should work.

If the firewall is really obnoxious, it can bounce consecutive queries
around between multiple originating IP addresses. That is uncommon
but it's been done from time to time.
 
D

Duncan Booth

Paul Rubin said:
If the firewall is really obnoxious, it can bounce consecutive queries
around between multiple originating IP addresses. That is uncommon
but it's been done from time to time.

Yes, each connection through the firewall needs to have a unique
originating ip and port number. If there are too many machines inside the
firewall then you may need to allocate multiple ip addresses on the
outside. I would hope that in general a single internal IP should map to
one external IP at a time (otherwise you would have problems with ip based
session persistence connecting to load balanced systems), but you might
expect to bounce round different IPs after periods of inactivity.

Also you could have multiple levels of NAT in which case the question
becomes whether Steven wants the IP as seen from outside the immediate
firewall or outside the final one. Maybe he should be using IPv6 to avoid
all this?
 
S

Sion Arrowsmith

Steven D'Aprano said:
I have a PC behind a firewall, and I'm trying to programmatically
determine the IP address visible from outside the firewall.
[ ... ]
Can anyone help me fix that code snippet, or suggest another (better) way
to get the externally visible IP address?

Depending on your circumstances, it may be possible to just ask the
firewall. You'll probably need some kind of administrator login, and
may well have to parse HTML if it's only got a web interface, but it
does mean that you don't have to connect to anything in the outside
world.
 
C

Cousin Stanley

I have a PC behind a firewall, and I'm trying to programmatically
determine the IP address visible from outside the firewall.
....

Steven ....

Following is another alternative that might at least
be worth consideration ....

I use the lynx command shown as a command-line alias
under Debian linux ....
.... print this
....


Current IP Address: 65.39.92.38
 
S

Sergio Correia

The above suggestions seem nice, but I find this one easier:

import urllib2
ext_ip = urllib2.urlopen('http://whatismyip.org/').read()
print ext_ip

The nice thing about the above code is that http://whatismyip.org/
only contains exactly what you want (the ip, nothing more, nothing
less), so no parsing is necessary

Best,
Sergio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top