Here is a script that demonstrates how to handle errors, successful updates, and general messages from the DynDNS service:
``
python
import sys
from datetime import datetime
# Replace 'your DynDNS credentials' with your actual credentials:
dyn_dns_username = 'username'
dyn_dns_password = 'password'
def check_dyndns_status(output):
if not (dyn_dns_username and dyn_dns_password):
sys.exit(1, "No DynDNS credentials provided")
successful = False
updates = []
# Search for status messages containing the word 'updated'
while True:
try:
pattern = r"Updated: .*ok:.+"
matches = output.splitlines()
for match in matches:
if match.strip() and 'Updated' not in match.lower():
continue
elif 'status:' + dyn_dns_username in match or ('command=' + dyn_dns_username) in match:
# Extract the command that caused this update (if any)
command = ""
lines = match.splitlines()
for line in lines[2:]:
if ' ' not in line and '=' not in line:
continue
elif '=' in line:
command, value = line.split('=')
successful = True
# Print out the updated details
print(f"Updated DynDNS: Dyn DNS service was {value}")
updates.append({
"timestamp": datetime.now(),
"command": command,
"new_value": value
})
except Exception as e:
sys.exit(1, f"Error processing status message: {e}")
# Print out the successful updates and commands that caused these updates
if successful:
print("\nSuccessful Updates:")
for update in updates[:10]:
print(f"{update['timestamp']}: {{command}={update['new_value']}}")
# Get DynDNS output and check for updates every minute
import time
while True:
dyn_dns_output = subprocess.check_output(['curl', '-s', f"https://{dyn_dns_username}:{dyn_dns_password}@dyn.ch/{dyn_dns_username}/status.json"])
check_dyndns_status(dyn_dns_output)
# Check if there was an error or a status message containing 'continued'
if "ERROR=" in dyn_dns_output and " continued" not in dynam['output']:
print("DynDNS Error:", dynam['error'])
else:
continue
# After 1 minute, try to check again for updates every 10 seconds
time.sleep(60000)
`
Please note that you will need to replace username
and password
with your actual DynDNS credentials.
This script splits the output into lines corresponding to updates ("Updated: ...ok:.+"
), then checks each line. If no update is found, it exits with errors. For successful updates, it prints out a message including the timestamp, command that caused the update, and new value.
As for error handling, if an error occurs while processing a status message or reading from a DynDNS output stream, the script will exit immediately with 1 (Error)` to alert the user about any issues.