rsedlr

dynamicDNS.py

May 13th, 2020
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import requests, schedule, time
  2. from datetime import datetime
  3.  
  4.  
  5. def dynamicDNS():
  6.     mydomain = 'domain.com'
  7.     myhostname = "gateway"
  8.     gdapikey = "api_key:key_secret"
  9.     logdest = "local7.info"
  10.  
  11.     myip = requests.get('https://api.ipify.org').text
  12.  
  13.     url = f'https://api.godaddy.com/v1/domains/{mydomain}/records/A/{myhostname}'
  14.     headers = {'Authorization': f'sso-key {gdapikey}'}
  15.     gdip = requests.get(url, headers=headers).json()[0]['data']
  16.  
  17.     print(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} -- Current External IP is {myip}, GoDaddy DNS IP is {gdip}')
  18.  
  19.     if (gdip != myip and myip != ''):
  20.         print('\nIP address has changed! Updating on GoDaddy')
  21.         url = f'https://api.godaddy.com/v1/domains/{mydomain}/records/A/{myhostname}'
  22.         headers = {'Authorization': (f'sso-key {gdapikey}'), 'Content-Type': 'application/json'}
  23.         data = '[{\"data\": "%s"}]' % myip
  24.         response = requests.put(url, headers=headers, data=data)
  25.         if (response.status_code == 200):
  26.             print(f'Changed IP on {myhostname}.{mydomain} from {gdip} to {myip}')
  27.         else:
  28.             print(f"error, response code: {response.status_code}")
  29.            
  30.  
  31. schedule.every(10).minutes.do(dynamicDNS)
  32.  
  33. while True:
  34.     schedule.run_pending()
  35.     time.sleep(1)
Add Comment
Please, Sign In to add comment