How To Use Python To Get Your External IP

top-ten-programming-languages1.1

I was having some issues involving losing connectivity to a system of mine due to a dynamic IP. Normally one would just use a dynamic DNS service to solve the problem, but in this case there were complications that kept that from being a solution.

So that’s where Python comes in. I threw this “solution” (It’s in quotes for a reason) together, which goes to my own rudimentary IP-finding site, pulls my address, and returns it to me. Here’s the code:

import urllibimport resite = urllib.urlopen(“https://danielmiessler.com/ip”).read()grab = re.findall(‘d{2,3}.d{2,3}.d{2,3}.d{2,3}’,site)address = grab[0]print address

So then in cron.hourly (Gentoo) I can do:

/usr/bin/address | mailx -s “New IP” me@mysecondaddress &> /etc/cron_output

It’s nasty, but it works. 🙂

[ Jul 15, 2005 ]

Related posts: