Last active
April 18, 2018 12:52
-
-
Save RyukGremory/9d4ace551961a8a0b0c446311a8b9805 to your computer and use it in GitHub Desktop.
Script to update a domain record in digital ocean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import requests | |
| import urllib | |
| import socket | |
| API_TOKEN ='AUTO_GENERATE_TOKEN_DIGITAL_OCEAN' | |
| API_URL_BASE='HTTPS://api.digitalocean.com/v2/' | |
| SUB_DOMAIN='sub-domain to update' | |
| DOMAIN='domain' | |
| headers = {'Content-Type': 'application/json', | |
| 'Authorization': 'Bearer {}'.format(API_TOKEN)} | |
| def Localip(): | |
| return urllib.request.urlopen('https://ident.me').read().decode('utf8') | |
| def Domainip(dominio): | |
| return socket.gethostbyname('{}'.format(dominio)) | |
| def Domain_info(SUB_DOMAIN,DOMAIN): | |
| API_URL = '{}domains/{}/records'.format(API_URL_BASE,DOMAIN) | |
| response = requests.get(API_URL, headers=headers) | |
| if response.status_code == 200: | |
| informacion = json.loads(response.content.decode('utf-8'))['domain_records'] | |
| for k in informacion: | |
| if k['name'] == SUB_DOMAIN: | |
| return k | |
| else: | |
| return None | |
| def update(DOMAIN_NAME,RECORD_ID,NEW_IP): | |
| API_URL_UPDATE = '{}domains/{}/records/{}'.format(API_URL_BASE,DOMAIN_NAME,RECORD_ID) | |
| data = '{"data":"'+NEW_IP+'"}' | |
| response = requests.put(API_URL_UPDATE, headers=headers, data=data) | |
| #print(response.content.decode('utf-8')) | |
| return 0 | |
| def main(): | |
| domain = Domain_info(SUB_DOMAIN,DOMAIN) | |
| localIp = Localip() | |
| domainIp = Domainip(DOMAIN) | |
| if localIp != domainIp: | |
| if domain is not None and domain['type'] == 'A': | |
| response = update(DOMAIN, domain['id'],Localip()) | |
| else: | |
| response = '[!] Request Failed:{}'.format(domain) | |
| elif Localip == Domainip: | |
| response = 'Nocambio' | |
| else: | |
| pass | |
| return response | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment