run.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import dnspod
  4. import ip
  5. import json
  6. import argparse
  7. import time
  8. def get_config(key=None, file="config.json"):
  9. if not hasattr(get_config, "config"):
  10. try:
  11. with open(file) as configfile:
  12. get_config.config = json.load(configfile)
  13. except:
  14. exit('fail to load config from file: %s' % file)
  15. if key:
  16. return get_config.config.get(key)
  17. else:
  18. return get_config.config
  19. def update():
  20. print "="*25+" "+time.ctime()+" "+"="*25
  21. index4=get_config('index4') or "default"
  22. if str(index4).isdigit():
  23. ipv4 = ip.local_v4(index4)
  24. else:
  25. ipv4 = getattr(ip,index4+"_v4")()
  26. print 'update ipv4 to:', ipv4
  27. if ipv4 != None:
  28. for domain in get_config('ipv4'):
  29. print dnspod.update_record(domain, ipv4, 'A')
  30. v6_domains=get_config("ipv6") or "default"
  31. if len(v6_domains) > 0:
  32. index6=get_config('index6')
  33. if str(index6).isdigit():
  34. ipv6 = ip.local_v6(index6)
  35. else:
  36. ipv6 = getattr(ip,index6+"_v6")()
  37. print 'update ipv6 to:', ipv6
  38. if ipv6 != None:
  39. for domain in v6_domains:
  40. print dnspod.update_record(domain, ipv6, 'AAAA')
  41. if __name__ == '__main__':
  42. parser = argparse.ArgumentParser()
  43. parser.add_argument('-c',default="config.json")
  44. get_config(file=parser.parse_args().c)
  45. dnspod.TOKEN = "%s,%s" % (get_config('id'), get_config('token'))
  46. dnspod.PROXY = get_config('proxy')
  47. ip.DEBUG = get_config('debug')
  48. update()