-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-ip-update.sh
executable file
·86 lines (75 loc) · 3.27 KB
/
dynamic-ip-update.sh
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
#!/bin/bash
echo "Cloudflare dynamic IP setter version 0.1"
# Variable check
ERROR=""
if [ -z "$X_AUTH_EMAIL" ]; then
echo "Require X_AUTH_EMAIL: Username. (e.g. johnappleseed@example.com)"
ERROR="1"
fi
if [ -z "$X_AUTH_KEY" ]; then
echo "Require X_AUTH_KEY: API key. (e.g. 123abc456def789ghi)"
ERROR="1"
fi
if [ -z "$ZONE" ]; then
echo "Require ZONE: Zone name. (e.g. example.com)"
ERROR="1"
fi
if [ -z "$DOMAIN" ]; then
echo "Require DOMAIN: domain name. (e.g. www.example.com)"
ERROR="1"
fi
if [ -z "$DEVICE" ]; then
echo "Require DEVICE: ethernet device. (e.g. eth0)"
ERROR="1"
fi
if [ -z "$PROXIED" ]; then
PROXIED="true"
fi
if [ -n "$ERROR" ]; then
echo 'usage: X_AUTH_EMAIL="johnappleseed@example.com" X_AUTH_KEY="123abc456def789ghi" ZONE="example.com" DOMAIN="www.example.com" DEVICE="eth0" dynamic-ip-update.sh'
exit 1
fi
# Get Global IP address
GLOBAL_IP_V4=$(curl --interface $DEVICE -s -X GET http://v4.ipv6-test.com/api/myip.php)
echo "Your global IPv4 is $GLOBAL_IP_V4"
GLOBAL_IP_V6=$(curl --interface $DEVICE -s -X GET http://v6.ipv6-test.com/api/myip.php)
echo "Your global IPv6 is $GLOBAL_IP_V6"$'\n'
# Get zones
echo "Getting zone records..."$'\n'
JSON=$(curl -s -X GET https://api.cloudflare.com/client/v4/zones -H "X-Auth-Email: $X_AUTH_EMAIL" -H "X-Auth-Key: $X_AUTH_KEY" -H "Content-Type: application/json")
# Get zone id
echo "Serching $ZONE zone..."
ZONE_ID=$(echo $JSON | jq -r ".result[] | select(.name == \"$ZONE\") | .id")
if [ -z "$ZONE_ID" ]; then
echo "$ZONE zone not found."
exit 1
fi
echo "$ZONE zone id is $ZONE_ID""."$'\n'
# Get records
echo "Getting zone records..."$'\n'
JSON=$(curl -s -X GET https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records -H "X-Auth-Email: $X_AUTH_EMAIL" -H "X-Auth-Key: $X_AUTH_KEY" -H "Content-Type: application/json")
# Get domain a record id
echo "Serching $DOMAIN A record..."
RECORD_ID=$(echo $JSON | jq -r ".result[] | select(.name == \"$DOMAIN\") | select(.type == \"A\") | .id")
if [ -n "$RECORD_ID" ]; then
echo "$DOMAIN A record id is $RECORD_ID""."$'\n'
# Set IP Address
echo "Recode update in progress..."
RESULT=$(curl -s -X PUT https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID -H "X-Auth-Email: $X_AUTH_EMAIL" -H "X-Auth-Key: $X_AUTH_KEY" -H "Content-Type: application/json" --data "{\"type\": \"A\", \"name\": \"$DOMAIN\", \"content\": \"$GLOBAL_IP_V4\", \"proxied\": $PROXIED}" | jq -r ".result")
echo "cloudflare server says: $RESULT"$'\n'
else
echo "$DOMAIN A record not found. Skipped."$'\n'
fi
# Get domain aaaa record id
echo "Serching $DOMAIN AAAA record..."
RECORD_ID=$(echo $JSON | jq -r ".result[] | select(.name == \"$DOMAIN\") | select(.type == \"AAAA\") | .id")
if [ -n "$RECORD_ID" ]; then
echo "$DOMAIN AAAA record id is $RECORD_ID""."$'\n'
# Set IP Address
echo "Recode update in progress..."
RESULT=$(curl -s -X PUT https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID -H "X-Auth-Email: $X_AUTH_EMAIL" -H "X-Auth-Key: $X_AUTH_KEY" -H "Content-Type: application/json" --data "{\"type\": \"AAAA\", \"name\": \"$DOMAIN\", \"content\": \"$GLOBAL_IP_V6\", \"proxied\": $PROXIED}" | jq -r ".result")
echo "cloudflare server says: $RESULT"$'\n'
else
echo "$DOMAIN AAAA record not found. Skipped."$'\n'
fi
echo "Script is done!"$'\n'