Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import urllib2
- import time
- import threading
- import ConfigParser
- from BeautifulSoup import BeautifulStoneSoup
- def _(a, c = "MAIN"): print("[%s %s] %s"%(time.strftime("%H:%M:%S", time.localtime()), c.upper(), a))
- def load_config():
- config = ConfigParser.RawConfigParser()
- config.read('conf.ini')
- out = []
- for section in config.sections():
- timeout = config.getint(section, 'timeout')
- token = config.get(section, 'token')
- domain = [config.get(section, 'subdomain'), config.get(section, 'domain')]
- out.append([timeout, token, domain])
- return out
- current = threading.local()
- _('running')
- def request(method, data):
- data['token'] = current.token
- r = requests.get('https://pddimp.yandex.ru/nsapi/%s.xml'%method, params=data)
- return [r.url, r.text]
- def get_host_info(host):
- soup = BeautifulStoneSoup(request('get_domain_records', {'domain':'nanomice.su'})[1])
- if soup.contents[2].domains.error.contents[0] == 'ok':
- for record in soup.contents[2].domains.response:
- if record['domain'] == host:
- return [int(record['id']), record.contents[0]]
- return False
- def my_ip():
- ret = urllib2.urlopen('https://enabledns.com/ip')
- return ret.read()
- def main():
- conf = load_config()
- for host in conf:
- current.timeout, current.token, current.domain = host
- host_info = get_host_info('.'.join(current.domain))
- if host_info:
- host_id, host_ip = host_info
- current_ip = my_ip()
- if host_ip != current_ip:
- _('ip changed (%s)'%' -> '.join([host_ip, current_ip]))
- soup = BeautifulStoneSoup(request('edit_a_record', {'domain':current.domain[1], 'subdomain':current.domain[0], 'record_id':host_id, 'content':current_ip})[1])
- status = soup.contents[2].domains.error.contents[0]
- if status == 'ok':
- _('successful changed ip')
- else:
- _('error : %s'%status)
- else:
- _('ip not changed')
- if __name__ == "__main__":
- while True:
- main()
- time.sleep(current.timeout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement