Created
May 8, 2018 19:43
-
-
Save anviar/22924819ddd3f70661f06317c2d2326a to your computer and use it in GitHub Desktop.
GoDaddy API sample
This file contains 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
[godaddy] | |
api_key = | |
api_secret = | |
root_domain = | |
API_ROOT = https://api.godaddy.com/v1 | |
[test] | |
sub_domain = | |
service_url = | |
ips = 127.0.0.1, 127.0.0.2 | |
method = get | |
timeout = 5 |
This file contains 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
#!/usr/bin/env python3 | |
import datetime | |
import json,argparse,time | |
import os,requests,configparser | |
from socket import gethostbyname | |
# Preparing arguments | |
argparser=argparse.ArgumentParser(description='Test service') | |
argparser.add_argument('-s','--service', help='previous statistics', type=str, required=True) | |
argparser.add_argument('--update', help='update record if test failed', action="store_true" ) | |
args = argparser.parse_args() | |
config = configparser.ConfigParser () | |
config.read(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'godaddy.cfg')) | |
api_headers = {'Authorization' : 'sso-key %s:%s' % (config.get('godaddy', 'api_key'), | |
config.get('godaddy', 'api_secret'))} | |
api_headers.update({'Accept': 'application/json' }) | |
api_headers.update({'Content-Type': 'application/json' }) | |
resolved_ip = gethostbyname('.'.join((config.get(args.service, 'sub_domain'),config.get('godaddy', 'root_domain')))) | |
def dns_update(data): | |
api_body=[{ 'type': 'A', | |
'name': config.get(args.service, 'sub_domain'), | |
'data': data }] | |
url = '/'.join(( | |
config.get('godaddy', 'API_ROOT'), | |
'domains', | |
config.get('godaddy', 'root_domain'), | |
'records', | |
'A', | |
config.get(args.service, 'sub_domain') | |
)) | |
response = requests.put(url, json=api_body, headers=api_headers).text | |
print ('URL:' + url) | |
print ('Body:' + str(api_body)) | |
print ('Response:' + str(response)) | |
def dns_get_all(): | |
url = '/'.join((config.get('godaddy', 'API_ROOT'), 'domains')) | |
response = requests.get( url, headers=api_headers) | |
if response.status_code != 200: | |
print("Error %s: %s" % (response.status_code, json.loads(response.text).get('message'))) | |
return 1 | |
return json.loads(response.text) | |
def dns_get_records(): | |
url = '/'.join((config.get('godaddy', 'API_ROOT'), 'domains', config.get('godaddy', 'root_domain'), 'records')) | |
response = requests.get( url, headers=api_headers) | |
if response.status_code != 200: | |
print("Error %s: %s" % (response.status_code, json.loads(response.text).get('message'))) | |
return 1 | |
return json.loads(response.text) | |
def dns_get_record(): | |
url = '/'.join(( | |
config.get('godaddy', 'API_ROOT'), | |
'domains', | |
config.get('godaddy', 'root_domain'), | |
'records', | |
'A', | |
config.get(args.service, 'sub_domain') | |
)) | |
response = requests.get( url, headers=api_headers) | |
if response.status_code != 200: | |
print("Error %s: %s" % (response.status_code, json.loads(response.text).get('message'))) | |
return 1 | |
return json.loads(response.text)[0] | |
def check_record(name): | |
for record in dns_get_records(): | |
if record.get('name') == name: | |
return True | |
#print(record.get('name')) | |
return False | |
def check_service_get(): | |
try: | |
response = requests.get( config.get(args.service, 'service_url'), timeout=int(config.get(args.service, 'timeout'))) | |
except Exception as ex: | |
return str(ex) | |
if response.status_code == 200: | |
return None | |
return 'Got code:%s'% response.status_code | |
#for record in dns_get_records(): | |
# if record.get('type') in ('CNAME','A'): | |
# print (record) | |
#exit(0) | |
service_check = check_service_get() | |
if service_check is not None: | |
print(time.strftime("%Y-%m-%d %H:%M") +' > ' + service_check) | |
godaddy_dns_record = dns_get_record() | |
if godaddy_dns_record.get('type') == 'A': | |
if godaddy_dns_record.get('data') != resolved_ip: | |
print ('Resolved:%s\nGoDaddy conf:%s' % (resolved_ip, godaddy_dns_record.get('data'))) | |
else: | |
for ip in config.get(args.service, 'ips').split(','): | |
if ip != godaddy_dns_record.get('data'): | |
new_ip = ip.strip() | |
break | |
print ('Updating %s -> %s' %(godaddy_dns_record.get('data'), new_ip)) | |
dns_update(new_ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment