Skip to content

Commit

Permalink
Check wether Updating is neccessary (#19) (#20)
Browse files Browse the repository at this point in the history
* Add Check if Update needs to be performed

* Remove some print statements in IP address comparison
  • Loading branch information
simonl169 authored May 26, 2024
1 parent 6c287a8 commit 71f4bd5
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions dns_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def resolve_current_server_ip(url, nameservers='8.8.8.8') -> [str, str]:
resolver = pydig.Resolver(nameservers=nameservers)
local_ip = pydig.query(url, 'A')
public_ip = resolver.query(url, 'A')
print(f'\tLocal IP address for {url} is: {local_ip[0]}')
print(f'\tPublic IP address for {url} is: {public_ip[0]}')
# print(f'\tLocal IP address for {url} is: {local_ip[0]}')
# print(f'\tPublic IP address for {url} is: {public_ip[0]}')
return local_ip[0], public_ip[0]


def compare_ip(ip1, ip2) -> bool:
print('\tComparing addresses:')
print(f'\tIP address 1: {ip1}')
print(f'\tIP address 2: {ip2}')
# print('\tComparing addresses:')
# print(f'\tIP address 1: {ip1}')
# print(f'\tIP address 2: {ip2}')
if ip1 == ip2:
return True
else:
Expand Down Expand Up @@ -79,19 +79,27 @@ def update_all_ip(current_ip):

for domain in data['domains']:
print(f"{'':#<40}")
print(f"\tUpdating DynDNS IP for domain: {domain['RECORD_NAME']}...")
response = set_ip(cf, domain, current_ip)

if response.json()['success']:
print(f"\tIP was set successfully!")
print(f"\tPerform Check if Update is necessary...")
domain_ip = resolve_current_server_ip(domain['RECORD_NAME'])[1]
check = compare_ip(current_ip, domain_ip)
if check:
print(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary!")
if notification_service:
notification_service.send_success(f"IP for domain {domain} was successfully set to {current_ip}")
notification_service.send_success(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary!")
else:
print(f"\tThere was an error, see below for more details")
print(f"\tResponse code was: {response.status_code}")
if notification_service:
notification_service.send_error(f"An error occurred, status code {response.status_code}")
print(f"\tResponse json is: {response.json()}")
print(f"\tUpdating DynDNS IP for domain: {domain['RECORD_NAME']}...")
response = set_ip(cf, domain, current_ip)

if response.json()['success']:
print(f"\tIP was set successfully!")
if notification_service:
notification_service.send_success(f"IP for domain {domain} was successfully set to {current_ip}")
else:
print(f"\tThere was an error, see below for more details")
print(f"\tResponse code was: {response.status_code}")
if notification_service:
notification_service.send_error(f"An error occurred, status code {response.status_code}")
print(f"\tResponse json is: {response.json()}")

print('\tDone!')
print(f"{'':#<40}")
Expand Down

0 comments on commit 71f4bd5

Please sign in to comment.