Skip to content

Instantly share code, notes, and snippets.

@macklinu
Created September 20, 2012 20:23
Show Gist options
  • Save macklinu/3758136 to your computer and use it in GitHub Desktop.
Save macklinu/3758136 to your computer and use it in GitHub Desktop.
checkip
#!/usr/bin/env python
import subprocess
import httplib, urllib
tempfile = open('/Users/macklinu/Documents/Scripts/.externalip/temp.txt', 'r+')
currentip = subprocess.Popen(['curl', 'ifconfig.me/ip'], stdout=subprocess.PIPE).communicate()[0].replace('\n', '')
def main():
checkIP()
def checkIP():
previousip = tempfile.readlines()[0].replace('\n', '')
if currentip == '':
print 'ERROR: NO CONNECTION'
print 'Previous IP: ' + previousip
print 'Current IP: ' + currentip
if previousip == currentip:
print 'Same external IP as before :)'
else:
print 'DIFFERENT external IP address!'
updateTemp()
#pushoverNotification()
# this doesn't
def updateTemp():
for line in tempfile:
line = line.replace(previousip, currentip)
tempfile.write(line + "\n")
tempfile.close()
# this works
def pushoverNotification():
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": "FemZz88Hl6gJpnj4UXdeRHvraZwqjK",
"user": "5alDvCSp7aJ2Je40S8nlQ3g3pnmUEZ",
"title": "Navi says 'Listen!'",
"message": "Gimpserv has a new IP: " + currentip,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment