Use if __name_ == "__main__"
This commit is contained in:
@@ -68,78 +68,82 @@ def updateCloudflare(zone, email, apiKey, host, ip):
|
|||||||
else:
|
else:
|
||||||
print("Updating failed, errors were: " + str(result["errors"]))
|
print("Updating failed, errors were: " + str(result["errors"]))
|
||||||
|
|
||||||
print(
|
def main():
|
||||||
'''
|
print(
|
||||||
------------------------------
|
'''
|
||||||
Cloudflare updater
|
------------------------------
|
||||||
www.jakecharman.co.uk
|
Cloudflare updater
|
||||||
------------------------------
|
www.jakecharman.co.uk
|
||||||
'''
|
------------------------------
|
||||||
)
|
'''
|
||||||
|
)
|
||||||
|
|
||||||
# Get the directory of the script.
|
# Get the directory of the script.
|
||||||
scriptDir = os.path.dirname(os.path.realpath(__file__)) + "/"
|
scriptDir = os.path.dirname(os.path.realpath(__file__)) + "/"
|
||||||
|
|
||||||
# Read in parameters from the config file.
|
# Read in parameters from the config file.
|
||||||
try:
|
try:
|
||||||
configFile = open(scriptDir + "updateCloudflare.conf", "r")
|
configFile = open(scriptDir + "updateCloudflare.conf", "r")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("Configuration file does not exist.")
|
print("Configuration file does not exist.")
|
||||||
exit(1)
|
|
||||||
|
|
||||||
configLines = configFile.readlines()
|
|
||||||
for line in configLines:
|
|
||||||
if line[0] == "#":
|
|
||||||
continue
|
|
||||||
# Remove any spaces from the line then split it to an array on the = sign.
|
|
||||||
splitLn = line.replace(" ", "").strip().split('#')[0].split("=")
|
|
||||||
|
|
||||||
# Pull in the known config lines.
|
|
||||||
if (splitLn[0] == "email"):
|
|
||||||
authEmail = splitLn[1]
|
|
||||||
elif (splitLn[0] == "apiKey"):
|
|
||||||
apiKey = splitLn[1]
|
|
||||||
elif (splitLn[0] == "host"):
|
|
||||||
hostToUpdate = splitLn[1]
|
|
||||||
else:
|
|
||||||
# Error out on an unknown config line.
|
|
||||||
print("Unknown config: " + splitLn[0])
|
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# Get the Zone ID for the given hostname.
|
configLines = configFile.readlines()
|
||||||
zoneID = getZone(authEmail, apiKey, hostToUpdate)
|
for line in configLines:
|
||||||
|
if line[0] == "#":
|
||||||
|
continue
|
||||||
|
# Remove any spaces from the line then split it to an array on the = sign.
|
||||||
|
splitLn = line.replace(" ", "").strip().split('#')[0].split("=")
|
||||||
|
|
||||||
# Open the temp file.
|
# Pull in the known config lines.
|
||||||
try:
|
if (splitLn[0] == "email"):
|
||||||
file = open(scriptDir + 'updateCloudflare.lastip', 'r')
|
authEmail = splitLn[1]
|
||||||
lastip = file.read()
|
elif (splitLn[0] == "apiKey"):
|
||||||
except FileNotFoundError:
|
apiKey = splitLn[1]
|
||||||
lastip = ""
|
elif (splitLn[0] == "host"):
|
||||||
|
hostToUpdate = splitLn[1]
|
||||||
|
else:
|
||||||
|
# Error out on an unknown config line.
|
||||||
|
print("Unknown config: " + splitLn[0])
|
||||||
|
exit(1)
|
||||||
|
|
||||||
if lastip == "":
|
# Get the Zone ID for the given hostname.
|
||||||
storedIP = False
|
zoneID = getZone(authEmail, apiKey, hostToUpdate)
|
||||||
print("No stored IP... checking Cloudflare API")
|
|
||||||
cloudflareIP = checkCloudflare(zoneID, authEmail, apiKey, hostToUpdate)
|
|
||||||
else:
|
|
||||||
storedIP = lastip
|
|
||||||
print("Stored IP is " + lastip)
|
|
||||||
|
|
||||||
# Get our external IP
|
# Open the temp file.
|
||||||
ip = requests.get('https://api.ipify.org').text
|
try:
|
||||||
print("Our current IP is " + ip)
|
file = open(scriptDir + 'updateCloudflare.lastip', 'r')
|
||||||
if storedIP:
|
lastip = file.read()
|
||||||
if ip == storedIP:
|
except FileNotFoundError:
|
||||||
print("IP has not changed since last run... Exiting")
|
lastip = ""
|
||||||
exit(0)
|
|
||||||
else:
|
if lastip == "":
|
||||||
print("IP has changed since last run... Updating Cloudflare")
|
storedIP = False
|
||||||
storeIP(ip)
|
print("No stored IP... checking Cloudflare API")
|
||||||
updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip)
|
cloudflareIP = checkCloudflare(zoneID, authEmail, apiKey, hostToUpdate)
|
||||||
else:
|
|
||||||
storeIP(ip)
|
|
||||||
if ip == cloudflareIP:
|
|
||||||
print("Cloudflare matches our current IP... Exiting")
|
|
||||||
exit(0)
|
|
||||||
else:
|
else:
|
||||||
print("Cloudflare IP does not match our current IP... Updating Cloudflare.")
|
storedIP = lastip
|
||||||
updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip)
|
print("Stored IP is " + lastip)
|
||||||
|
|
||||||
|
# Get our external IP
|
||||||
|
ip = requests.get('https://api.ipify.org').text
|
||||||
|
print("Our current IP is " + ip)
|
||||||
|
if storedIP:
|
||||||
|
if ip == storedIP:
|
||||||
|
print("IP has not changed since last run... Exiting")
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
|
print("IP has changed since last run... Updating Cloudflare")
|
||||||
|
storeIP(ip)
|
||||||
|
updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip)
|
||||||
|
else:
|
||||||
|
storeIP(ip)
|
||||||
|
if ip == cloudflareIP:
|
||||||
|
print("Cloudflare matches our current IP... Exiting")
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
|
print("Cloudflare IP does not match our current IP... Updating Cloudflare.")
|
||||||
|
updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Reference in New Issue
Block a user