Changed the script not to use OOP and to have arguments
This commit is contained in:
parent
3bdf45f015
commit
fc197ea2a2
1 changed files with 75 additions and 55 deletions
116
gandyndns.py
116
gandyndns.py
|
|
@ -3,65 +3,85 @@
|
||||||
# @author: lordof20th
|
# @author: lordof20th
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import argparse
|
||||||
|
|
||||||
class GanDynDns:
|
parser = argparse.ArgumentParser(
|
||||||
def __init__(self, fqdn, rrset_name, rrset_type, apikey) -> None:
|
description="A script which connect to Gandi.net API to change IP associated to a DNS record")
|
||||||
self.fqdn = fqdn
|
parser.add_argument("-v", "--verbose",
|
||||||
self.rrset_name = rrset_name
|
help="Enable verbose mode", action="store_true")
|
||||||
self.rrset_type = rrset_type
|
parser.add_argument("domain", metavar="DOMAIN",
|
||||||
self.domain_record_string = f"DNS {self.rrset_type} record for {self.rrset_name}.{self.fqdn}"
|
help="The domain for which you want to write a DNS record. Example: example.com")
|
||||||
self.apiUrl = f"https://api.gandi.net/v5/livedns/domains/{fqdn}/records/{rrset_name}/{rrset_type}"
|
parser.add_argument("subdomain", metavar="SUBDOMAIN",
|
||||||
self.headers = {"Authorization": f"Apikey {apikey}", 'User-Agent': 'Mozilla/5.0', "Content-Type": "application/json"}
|
help="The subdomain to point to. Examples: 'sub' or '@'")
|
||||||
self.update_dns_IP()
|
parser.add_argument("apikey", metavar="APIKEY", help="Your Gandi.net API key")
|
||||||
|
parser.add_argument("--type", metavar="TYPE", default='A',
|
||||||
|
help="The type of DNS record to create. Default: A")
|
||||||
|
|
||||||
def retrieve_dns_IP(self):
|
args = parser.parse_args()
|
||||||
"""Retrieves the IP in the DNS record using the fqdn, rrset_name (for instance subdomain like www etc) and rrset_type (the DNS record type A, CNAME ...).
|
print(args)
|
||||||
|
|
||||||
The function uses requests library and connect to Gandi.net API
|
verbose = args.verbose
|
||||||
|
domain = args.domain
|
||||||
|
apikey = args.apikey
|
||||||
|
subdomain = args.subdomain
|
||||||
|
type = args.type
|
||||||
|
|
||||||
Returns:
|
domain_record_string = f"DNS {type} record for {subdomain}.{domain}"
|
||||||
retrievedDnsIp (str): string of the IP address in the DNS record"""
|
|
||||||
|
|
||||||
response = requests.get(self.apiUrl, headers=self.headers)
|
domain_record_string_lenght = len(domain_record_string)
|
||||||
|
|
||||||
responseJson = response.json() # IPs are stored in a list as string
|
apiUrl = f"https://api.gandi.net/v5/livedns/domains/{domain}/records/{subdomain}/{type}"
|
||||||
try:
|
|
||||||
retrievedDnsIp = responseJson['rrset_values'][0]
|
|
||||||
except KeyError as key_error:
|
|
||||||
print(f"{key_error} means the record doesn't exist, we'll return an empty string instead")
|
|
||||||
retrievedDnsIp =""
|
|
||||||
return retrievedDnsIp
|
|
||||||
|
|
||||||
def retrieve_public_IP(self):
|
headers = {"Authorization": f"Apikey {apikey}",
|
||||||
"""Retrieves the public IP by connecting to ipinfo.io API
|
'User-Agent': 'Mozilla/5.0', "Content-Type": "application/json"}
|
||||||
|
|
||||||
Returns:
|
|
||||||
data['ip'] (str) : string of the public IP address"""
|
|
||||||
endpoint = "https://ipinfo.io/json"
|
|
||||||
response = requests.get(endpoint, verify = True)
|
|
||||||
|
|
||||||
data = response.json()
|
def retrieve_public_IP():
|
||||||
return data['ip']
|
"""Retrieves the public IP by connecting to ipinfo.io API
|
||||||
|
|
||||||
def ips_are_equals(self):
|
Returns:
|
||||||
"""The method compares both IPs and returns a boolean for the equality test
|
data['ip'] (str) : string of the public IP address"""
|
||||||
|
endpoint = "https://ipinfo.io/json"
|
||||||
|
response = requests.get(endpoint, verify=True)
|
||||||
|
|
||||||
Returns:
|
data = response.json()
|
||||||
(bool) : result of the IP equality test"""
|
return data['ip']
|
||||||
self.currentPublicIP = self.retrieve_public_IP()
|
|
||||||
self.dnsIP = self.retrieve_dns_IP()
|
|
||||||
return self.currentPublicIP == self.dnsIP
|
|
||||||
|
|
||||||
def update_dns_IP(self):
|
|
||||||
"""Updates the IP in the DNS record using the IP provided (acquired by retrieve_public_IP) if it is different from the DNS IP"""
|
|
||||||
|
|
||||||
if not self.ips_are_equals():
|
def retrieve_dns_IP(apiUrl, headers):
|
||||||
data = {
|
"""Retrieves the IP in the DNS record using the domain, rrset_name (for instance subdomain like www etc) and rrset_type (the DNS record type A, CNAME ...).
|
||||||
"rrset_values": [self.currentPublicIP]
|
|
||||||
}
|
|
||||||
print(f"{self.domain_record_string : <40} | Old IP : {self.dnsIP} replaced -> by New IP : {self.currentPublicIP}")
|
|
||||||
requests.put(url=self.apiUrl, headers=self.headers, json=data)
|
|
||||||
else:
|
|
||||||
print(f"{self.domain_record_string : <40} | {'IPs are the same.':<17}")
|
|
||||||
|
|
||||||
main = GanDynDns("example.org", "@", "A", "your-api-key")
|
The function uses requests library and connect to Gandi.net API
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
retrievedDnsIp (str): string of the IP address in the DNS record"""
|
||||||
|
|
||||||
|
response = requests.get(apiUrl, headers=headers)
|
||||||
|
|
||||||
|
responseJson = response.json() # IPs are stored in a list as string
|
||||||
|
try:
|
||||||
|
retrievedDnsIp = responseJson['rrset_values'][0]
|
||||||
|
except KeyError as key_error:
|
||||||
|
print(
|
||||||
|
f"{key_error} means the record doesn't exist, we'll return an empty string instead")
|
||||||
|
retrievedDnsIp = ""
|
||||||
|
return retrievedDnsIp
|
||||||
|
|
||||||
|
|
||||||
|
def update_dns_IP(apiUrl, headers):
|
||||||
|
"""Updates the IP in the DNS record using the IP provided (acquired by retrieve_public_IP) if it is different from the DNS IP"""
|
||||||
|
publicIP = retrieve_public_IP()
|
||||||
|
dnsIP = retrieve_dns_IP(apiUrl, headers)
|
||||||
|
|
||||||
|
if not publicIP == dnsIP:
|
||||||
|
data = {
|
||||||
|
"rrset_values": [publicIP]
|
||||||
|
}
|
||||||
|
print(f"{domain_record_string : <{domain_record_string_lenght}} | Old IP : {dnsIP} replaced -> by New IP : {publicIP}")
|
||||||
|
requests.put(url=apiUrl, headers=headers, json=data)
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
f"{domain_record_string : <{domain_record_string_lenght}} | {'IPs are the same.':<17}")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
update_dns_IP(apiUrl, headers)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue