Created
January 5, 2019 22:29
-
-
Save dnutiu/554ad3333af7839e56b4495953ca393b to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/python3 | |
| """ | |
| Update the vultr api for the my domain. | |
| """ | |
| import os | |
| import requests | |
| import syslog | |
| PUBLIC_IP_ADDRESS = "https://api.ipify.org" | |
| VULTR_API_UPDATE = "https://api.vultr.com/v1/dns/update_record" | |
| def main(): | |
| api_key = os.getenv('VULTR_API', None) | |
| if api_key is None: | |
| return | |
| request = requests.get(PUBLIC_IP_ADDRESS) | |
| if request.status_code != 200: | |
| syslog.syslog(syslog.LOG_ERR, "Could not get the public ip address!") | |
| return | |
| ip = request.content.decode() | |
| # To get the record id see /dns/records | |
| data = { | |
| "RECORDID": None, | |
| "domain": None, | |
| "data": ip | |
| } | |
| headers = {"API-Key": api_key} | |
| # Update the IP | |
| request = requests.post(VULTR_API_UPDATE, data=data, headers=headers) | |
| if request.status_code != 200: | |
| syslog.syslog(syslog.LOG_ERR, "Could not update the IP! {}".format(request.text)) | |
| return | |
| syslog.syslog(syslog.LOG_NOTICE, 'IP address updated! New address: {}'.format(ip)) | |
| if __name__ == "__main__": | |
| syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_CRON) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment