Skip to content

Instantly share code, notes, and snippets.

@aoriani
Created November 23, 2015 04:16
Show Gist options
  • Save aoriani/74e586705799016739d1 to your computer and use it in GitHub Desktop.
Save aoriani/74e586705799016739d1 to your computer and use it in GitHub Desktop.
A simple python script that tweets the external public IP to a private Twitter account. The idea is to create an alternative to dynamic DNS services. In practice you only need the IP of your remote host. The script is going to be run daily by cron. The beauty of Twitter API is that it prevents duplicated message. Thus you will only get a new tw…
#!/usr/bin/env python
import sys
import tweepy
import urllib2
CONSUMER_KEY = "<your consumer key>"
CONSUMER_SECRET = "<your consumer secret>"
ACCESS_TOKEN = "<your access token>"
ACCESS_TOKEN_SECRET = "<your access secret>"
def get_external_ip():
conn = urllib2.urlopen("http://ipinfo.io/ip")
external_ip = conn.read()
conn.close()
return external_ip
if __name__ == "__main__":
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
try:
API = tweepy.API(auth)
API.update_status(get_external_ip())
except tweepy.error.TweepError as error:
print >> sys.stderr, "Error: %s" % error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment