Client Scripts Usage
Client Scripts
PHP Script
#!/usr/bin/env php
<?php
// Configuration
$apiUrl = 'https://ddns.oit.sk/api/v1/update';
if ($argc < 3 || $argc > 4) {
echo "Usage: php {$argv[0]} <hostname> <token> [ip_address]\n";
exit(1);
}
$hostname = $argv[1];
$token = $argv[2];
$ip = $argv[3] ?? null;
$payload = [
'hostname' => $hostname,
'token' => $token,
];
if ($ip) {
$payload['ip'] = $ip;
}
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($payload),
'ignore_errors' => true, // To read the response body on error
],
];
$context = stream_context_create($options);
$response = file_get_contents($apiUrl, false, $context);
echo "Server response:";
echo $response . "\n";
// Check for errors based on the HTTP response header
$statusCode = 0;
if (isset($http_response_header[0])) {
preg_match('{HTTP/\S*\s(\d{3})}', $http_response_header[0], $match);
if (isset($match[1])) {
$statusCode = (int)$match[1];
}
}
if ($statusCode >= 400) {
fwrite(STDERR, "Error: Received HTTP status code $statusCode\n");
exit(1);
}
echo "Update successful.";
exit(0);
Python Script
#!/usr/bin/env python3
import requests
import argparse
import sys
# Configuration
API_URL = "https://ddns.oit.sk/api/v1/update"
def update_ddns(hostname, token, ip=None):
"""
Sends a DDNS update request.
"""
payload = {
'hostname': hostname,
'token': token,
}
if ip:
payload['ip'] = ip
try:
response = requests.post(API_URL, data=payload)
response.raise_for_status() # Raise an exception for bad status codes
print("Request successful.")
print("Response:")
print(response.json())
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}", file=sys.stderr)
if e.response:
print(f"Response content: {e.response.text}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="DDNS Update Client")
parser.add_argument("hostname", help="The hostname to update.")
parser.add_argument("token", help="The authentication token for the hostname.")
parser.add_argument("--ip", help="Optional: The IP address to set. If not provided, the server will detect it.", default=None)
args = parser.parse_args()
update_ddns(args.hostname, args.token, args.ip)
Bash Script
#!/bin/bash
set -e
# Configuration
API_URL="https://ddns.oit.sk/api/v1/update"
# --- Script ---
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
echo "Usage: $0 <hostname> <token> [ip_address]"
exit 1
fi
HOSTNAME=$1
TOKEN=$2
IP_ADDRESS=$3
# Build the data payload
DATA="hostname=${HOSTNAME}&token=${TOKEN}"
if [ -n "$IP_ADDRESS" ]; then
DATA="${DATA}&ip=${IP_ADDRESS}"
fi
echo "Sending update for ${HOSTNAME}..."
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST -d "$DATA" "$API_URL")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
echo "Server response:"
echo "$BODY"
if [ "$HTTP_CODE" -ge 400 ]; then
echo "Error: Received HTTP status code $HTTP_CODE" >&2
exit 1
fi
echo "Update successful."
exit 0