-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectel-ddns
109 lines (83 loc) · 2.87 KB
/
selectel-ddns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Get the directory path of the script
SCRIPT_DIR=$(dirname "$(realpath "$0")")
# File to store the current IP address
IP_FILE="$SCRIPT_DIR/ip.txt"
function check_requirements {
for PROGRAM in jq curl; do
if ! [ -x "$(command -v $PROGRAM)" ]; then
echo "Error: $PROGRAM is not installed. Please install $PROGRAM before running this script." >&2
exit 1
fi
done
# Check if the .env file exists
if [ ! -f "$SCRIPT_DIR/.env" ]; then
echo 'Error: .env file not found. Please create a .env file in the same directory as this script.' >&2
exit 1
fi
source "$SCRIPT_DIR/.env"
# Check if the token is set
if [ -z "$TOKEN" ]; then
echo 'Error: Selectel API token not found in .env file. Please add the token to the .env file.' >&2
exit 1
fi
# Check if the domains list is set
if [ -z "$DOMAINS" ]; then
echo 'Error: Domain list not found in .env file. Please add the domain list to the .env file.' >&2
exit 1
fi
# Check if the IP file exists
if [ ! -f "$IP_FILE" ]; then
echo "" >"$IP_FILE"
fi
}
function get_domain_id {
local domain="${1}"
echo "Getting domain ID for ${domain}"
local response=$(curl -sS -H "X-Token: ${TOKEN}" "${API_ENDPOINT}${domain}")
if [[ $(echo $response | jq -r ".error") != "null" ]]; then
error_message=$(echo $response | jq -r ".error")
echo "Error obtaining domain id: $error_message" >&2
exit 1
fi
local record_id=$(echo $response | jq -r ".id")
echo $record_id
}
function update_record {
local domain_id="${1}"
local record_id=$(echo "${2}")
echo "Updating record with ID: ${record_id}"
local json='{"type": "A", "content": "'${CURRENT_IP}'"}'
local url="${API_ENDPOINT}${domain_id}/records/${record_id}"
local response=$(curl -sS -X PUT -H "X-token: $TOKEN" -H "Content-Type: application/json" -d "${json}" "${url}")
if [[ $(echo "$response" | jq -r ".error") == "null" ]]; then
echo "Record updated" >&2
return 0
else
error_message=$(echo $response | jq -r ".error")
echo "Error: $error_message" >&2
return 1
fi
}
function update_domain_records {
get_domain_id $MAIN_DOMAIN
local main_domain_id=$?
local url="${API_ENDPOINT}${MAIN_DOMAIN}/records/"
declare -a domain_names=($DOMAINS)
local list=$(IFS=,; echo "${domain_names[*]}")
local response=$(curl -sS -H "X-Token: ${TOKEN}" -H "Content-Type: application/json" "${url}")
local d=$(echo $response | jq -r --arg list $list '.[] | select(.name as $a | $list | index($a)) | select(.type=="A") | .id')
for resource_id in $d; do
update_record ${main_domain_id} ${resource_id}
done
}
check_requirements
EXISTING_IP=$(cat "$IP_FILE")
CURRENT_IP=$(curl -sS https://checkip.amazonaws.com/)
echo "Current IP: ${CURRENT_IP}"
if [ "${EXISTING_IP}" == "${CURRENT_IP}" ]; then
echo "The IP is actual."
exit 0
fi
update_domain_records
echo "${CURRENT_IP}" >"${IP_FILE}"