WIP for checking public ip against known CF A record value

This commit is contained in:
2024-01-12 16:53:33 +01:00
parent 50220f83a6
commit ffa4ab949b
2 changed files with 194 additions and 0 deletions

48
main.py Normal file
View File

@@ -0,0 +1,48 @@
import os
import requests
from bulk_dns_update import updateAllZones, setAPIKey, getDDNSAddresszoneId
from dotenv import load_dotenv
load_dotenv()
currentIP = None
DDNS_ZONE = os.getenv('DDNS_ZONE')
def publicAddress():
global currentIP
print('Getting public IP from ifconfg.me...')
r = requests.get('https://ifconfig.me')
currentIP = r.text
print('Public IP: {}'.format(currentIP))
def cloudflareDDNS():
print('Checking IP recorded in Cloudflare...')
ddnsRecord = getDDNSAddresszoneId(DDNS_ZONE)
recordedIP = ddnsRecord['content']
print('Found ddns recorded IP: {}'.format(recordedIP))
if currentIP != recordedIP:
print('Public IP has changed, updating all A records.')
updateAllZones(recordedIP, currentIP)
else:
print('is same, exiting')
def main():
apiKey = os.getenv('API_KEY')
if apiKey is None:
raise Exception('In .env file or environment set Cloudflare variable: API_KEY')
if DDNS_ZONE is None:
raise Exception('In .env file or environment; set Cloudflare zone where addr. points to current IP.')
setAPIKey(apiKey)
publicAddress()
cloudflareDDNS()
if __name__ == '__main__':
main()