#!/usr/bin/python import httplib,sys,urllib,json,time from BeautifulSoup import BeautifulSoup class updater: debug = False headers = { "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729; .NET4.0E", "X-Behavioral-Ad-Opt-Out": "1", "X-Do-Not-Track" : "1", } def __init__(self,username,password,domainname,subdomain): self.username = username self.password = password self.domain = domainname self.subdomain = subdomain self.getCSRFToken() self.getLoginCookie() self.getPublicIP() def getPublicIP(self): conn = httplib.HTTPConnection("ifconfig.me") conn.request("GET", "/ip") r1 = conn.getresponse() data1 = r1.read() self.publicip = data1 if self.debug: print "Got public IP: %s" % self.publicip return self.publicip def getLoginCookie(self): try: conn = httplib.HTTPSConnection("www.123-reg.co.uk") headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} params = urllib.urlencode({'X-CSRF-Token':self.token,'username': self.username, 'password': self.password, 'login': 'Log Me In','login_submit':''}) conn.request("POST", "/public/login", params, dict(self.headers.items() + headers.items())) res = conn.getresponse() if self.debug: print res.read() self.headers["Cookie"] += res.getheader("Set-Cookie") if self.debug: print "Get login cookie: %s " % self.headers["Cookie"] return self.headers["Cookie"] except: print "Fatal Error: getting login cookie" print sys.exc_info()[0] sys.exit(-1) def getMainPage(self): conn = httplib.HTTPSConnection("www.123-reg.co.uk") conn.request("GET","/secure/cp/ca.cgi?domain=%s" % self.domain,headers = self.headers) r1 = conn.getresponse() page = r1.read() return page def getCSRFToken(self): print "Getting csrf token" conn = httplib.HTTPSConnection("www.123-reg.co.uk") conn.request("GET","/") resp = conn.getresponse() self.headers["Cookie"] = resp.getheader("Set-Cookie") if self.debug: print "Got Cookie: %s" % self.headers["Cookie"] soup = BeautifulSoup(resp.read()) token = soup.find("meta")["content"] if token != "": self.token = token else: print "ERROR: cant get token!" def updateSubDomainNew(self): conn = httplib.HTTPSConnection("www.123-reg.co.uk") headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} params = "domain=%s&type=A&host=%s&data=%s&mx_priority=0&ttl=&rr_id=45543760" % (self.domain,self.subdomain,self.publicip) conn.request("POST", "/secure/cpanel/manage-dns/edit_dns_record", params, dict(self.headers.items() + headers.items())) resp = conn.getresponse() resp = resp.read() if self.debug: print resp try: if json.loads(resp)["json"]["is_success"] == 1: return "Successfully updated IP address!" else: return json.loads(resp)["json"]["message"] except KeyError: return "Error failed to decode json: %s" % resp def updateDomain(self,postdata): conn = httplib.HTTPSConnection("www.123-reg.co.uk") headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} params = urllib.urlencode(postdata) conn.request("POST", "/secure/cp/ca.cgi", params, dict(self.headers.items() + headers.items())) def processPage(self): soup = BeautifulSoup(self.getMainPage()) print soup.findAll({'form' : True}) postdata = soup.findAll({'form' : True})[3] #find the subdomain and replace the ip with the new public ip publicip = self.publicip #check to see if the ip is already up to date is so exit if postdata.find(text=self.subdomain).findAllNext('td')[1].findNext()['value'] == publicip: sys.exit(0) postdata.find(text=self.subdomain).findAllNext('td')[1].findNext()['value'] = self.publicip selecttags = postdata.findAll({'select': True}) inputtags = postdata.findAll({'input': True}) postData = {} #loop through all the select options and get the ones which are selected for selecttag in selecttags: options = selecttag.findAll({'option':True}) name = selecttag['name'] for option in options: try: if option['selected'] == 'selected': postData[selecttag['name']] = option['value'] except: continue #loop through all the input tags as well and extravt them for inputtag in inputtags: if inputtag['name'][:3] != u'del': postData[inputtag['name']] = "" try: postData[inputtag['name']] = inputtag['value'] except: continue self.updateDomain(postData) if __name__ == "__main__": domain = "neiljsecurity.co.uk" subdomain = "foo" username = "" #123-reg username password = "" #123-reg password while True: try: ud = updater(username,password,domain,subdomain) print ud.updateSubDomainNew() #wait 1 hour except KeyboardInterrupt: print "Exiting!" sys.exit(1) except: continue