Skip to content

Instantly share code, notes, and snippets.

@k14i
Created August 10, 2013 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k14i/6201463 to your computer and use it in GitHub Desktop.
Save k14i/6201463 to your computer and use it in GitHub Desktop.
This may be useful when you don't use dynamic DNS sevices.
#!/usr/bin/env python
import urllib2
import re
import os
import socket
#hostname = socket.gethostname()
hostname = ""
urlprovider = 'http://www.ugtop.com/'
targetnode = ''
targetsshdport = '22'
targetdir = "~/var/globalipaddrinfo/%(hostname)s" % locals()
targetfile = "%(targetdir)s/ipaddr" % locals()
privatekey = '~/.ssh/id_rsa'
regex = re.compile('^([0-9]{1,3})(\.([0-9]{1,3})){3}')
file = urllib2.urlopen(urlprovider)
for line in file:
matchobject = regex.match(line)
if matchobject == None:
continue
else:
ipaddressstring = matchobject.group()
cmd = "ssh -i %(privatekey)s -p %(targetsshdport)s %(targetnode)s \"mkdir -p %(targetdir)s; echo %(ipaddressstring)s > %(targetfile)s\"" % locals()
os.system(cmd)
break
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment