PDA

View Full Version : get your public IP with my little doo-hickey here


inkedmn
08-30-2002, 07:51 PM
whipped this up while setting up my mom's machine. she's got a dsl router with 192.168 addressing internally. but i'd like to be able to access her stuff over the internet, so i'd need her public ip. wrote this little script to get it. this should work from within any internal network to get the IP from your router/gateway/etc...


import urllib, re

def getIp():
conn = urllib.urlopen("http://www.whatismyip.com")
html = conn.read()
textonly = re.sub('<.*?>', '', html)
lines = textonly.strip().split('\n')
return lines[0].strip().split(' ')[3]

print "Your public IP is: " + getIp()


btw, had this running in about 3 minutes...
~praise python :)

gufmn
08-30-2002, 08:06 PM
nice

ChefNinja
08-30-2002, 09:38 PM
Traceback (most recent call last):
File "C:/Python22/ip.py", line 10, in ?
print "Your public IP is: " + getIp()
File "C:/Python22/ip.py", line 8, in getIp
return lines[0].strip().split(' ')[3]
IndexError: list index out of range

ChefNinja
08-30-2002, 09:40 PM
k... fixed it by putting

import re


on a different line... my computer is weird :rolleyes:

Bradmont
08-30-2002, 10:27 PM
Originally posted by inkedmn
whipped this up while setting up my mom's machine. she's got a dsl router with 192.168 addressing internally. but i'd like to be able to access her stuff over the internet, so i'd need her public ip.

run a traceroute google.com and grab the second IP ;)

inkedmn
08-30-2002, 11:46 PM
Originally posted by Bradmont


run a traceroute google.com and grab the second IP ;)

that would work as well...

but parsing a basic html page was easier to code than parsing the output of a tracert (at the time).

jemfinch
09-02-2002, 06:32 PM
And more secure and portable. Traceroute isn't available on all systems.

Jeremy

inkedmn
09-02-2002, 07:16 PM
not to mention the fact that, for somebody like me (who has a dialup ;)), waiting for a traceroute (which, on windows machines, takes as many hops as necessary to get to the destination IP up to 30) could take quite awhile to finish.

then, there's parsing the output. i think my way is much easier.

IMHO, of course :)