dyndns/Update_DDNS_OVH.sh

86 lines
2.8 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# Define variables (replace with your credentials)
WORK_DIR="/path/to/workdir" # Update with your desired working directory
LOGIN_OVH="your_dynhost_login@domain.tld"
PWD_OVH="your_dynhost_password"
URL_OVH="subdomain.domain.tld"
# Define IP retrieval URLs
ipv4_url="https://api.ipify.org"
ipv6_url="https://api6.ipify.org"
# Define IP regex patterns
ipv4_pattern="^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
ipv6_pattern="^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$" # Simple IPv6 regex (can be improved)
# Function to update OVH record (CORRECTED)
update_ovh_ddns() {
local ip="$1"
local ip_type="$2"
echo "Updating $ip_type address ($ip) at OVH..."
# Create the authentication string
local auth_string="$LOGIN_OVH:$PWD_OVH"
# Encode the authentication string using base64
local encoded_auth=$(printf "%s" "$auth_string" | base64)
# Construct the Authorization header
local auth_header="Authorization: Basic $encoded_auth"
local update_url="https://dns.eu.ovhapis.com/nic/update?system=dyndns&hostname=$URL_OVH&myip=$ip"
# Use curl with the corrected header
local response=$(curl -s -H "$auth_header" "$update_url")
if [[ "$response" == "good $ip" || "$response" == "nochg $ip" ]]; then
echo "OVH update successful for $ip_type. OVH response: $response"
echo "$ip" > "$WORK_DIR/maj_${ip_type}_ip.txt"
elif [[ "$response" == "abuse" ]]; then
echo "OVH update failed for $ip_type. OVH response: $response - Too many updates, wait a bit"
else
echo "Error updating OVH for $ip_type: $response"
fi
}
# Check if work directory exists
if [ ! -d "$WORK_DIR" ]; then
echo "Work directory doesn't exist, creating..."
mkdir -p "$WORK_DIR"
echo "0.0.0.0" > "$WORK_DIR/maj_ipv4_ip.txt"
echo "::" > "$WORK_DIR/maj_ipv6_ip.txt" # Default value for IPv6
fi
# Process IPv4
ipv4=$(curl -s "$ipv4_url" | tr -d '\r')
if [[ $ipv4 =~ $ipv4_pattern ]]; then
old_ipv4=$(cat "$WORK_DIR/maj_ipv4_ip.txt" || echo "0.0.0.0")
if [[ $ipv4 != $old_ipv4 ]]; then
echo "** Your IPv4 address has changed **"
echo "New IPv4: $ipv4"
echo "Old IPv4: $old_ipv4"
update_ovh_ddns "$ipv4" "ipv4"
else
echo "Your IPv4 address hasn't changed."
fi
else
echo "Warning: Retrieved IPv4 address ($ipv4) is not in a valid format."
fi
# Process IPv6
ipv6=$(curl -s "$ipv6_url" | tr -d '\r')
if [[ $ipv6 =~ $ipv6_pattern ]]; then
old_ipv6=$(cat "$WORK_DIR/maj_ipv6_ip.txt" || echo "::")
if [[ $ipv6 != $old_ipv6 ]]; then
echo "** Your IPv6 address has changed **"
echo "New IPv6: $ipv6"
echo "Old IPv6: $old_ipv6"
update_ovh_ddns "$ipv6" "ipv6"
else
echo "Your IPv6 address hasn't changed."
fi
else
echo "Warning: Retrieved IPv6 address ($ipv6) is not in a valid format."
fi