forked from xkszltl/Roaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnspod_ddns.sh
executable file
·153 lines (137 loc) · 6.55 KB
/
dnspod_ddns.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
set -e
# ================================================================
# Configuration
# ================================================================
. "$(dirname "$0")/pkgs/env/cred.sh"
TokenDnspodCN="$CRED_USR_DNSPOD_CN_KEY,$CRED_USR_DNSPOD_CN_SECRET"
TokenDnspodIntl="$CRED_USR_DNSPOD_INTL_KEY,$CRED_USR_DNSPOD_INTL_SECRET"
TokenDNSCOMKey="$CRED_USR_DNSCOM_KEY"
TokenDNSCOMSecret="$CRED_USR_DNSCOM_SECRET"
# ----------------------------------------------------------------
Domain=codingcafe.org
# ================================================================
# Main
# ================================================================
LastDir=$(mktemp -d)
cd "$LastDir"
while true; do
echo '========================================'
for Rec in def {snmp,ifcfg}.c{t,u}cc; do
# IP=`curl -s ns1.dnspod.net:6666 $Interface`
if grep -q '^snmp\.' <<< "$Rec"; then
grep -q '\.ctcc$' <<< "$Rec" && Interface='Dialer10'
grep -q '\.cucc$' <<< "$Rec" && Interface='Dialer20'
IP=$(snmpwalk -v3 -u monitor -x AES -m IP-MIB 10.0.0.1 ipAdEntIfIndex | sed -n 's/.*\.\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*[[:space:]]'"$(snmpwalk -v3 -u monitor -x AES -m IF-MIB 10.0.0.1 ifName | sed -n 's/.*\.\([0-9]*\).*[[:space:]]'$Interface'$/\1/p')"'$/\1/p') || echo 'ERROR: Failed to retrieve SNMP data for "'"$Rec"'"'
elif grep -q '^ifcfg\.' <<< "$Rec"; then
IP=$(curl -sSL https://ifcfg.net/ --interface "$(sed 's/^......//' <<<$Rec)") || echo 'ERROR: Failed to retrieve IP for "'"$Rec"'"'
else
IP=$(curl -sSL https://ifcfg.net/) || echo 'ERROR: Failed to retrieve IP for "'"$Rec"'"'
fi;
IP=$(xargs <<< "$IP" | sed -n 's/^[[:space:]]*\([^[:space:]]*\).*$/\1/p' | head -n 1);
[ $IP ] || IP='0.0.0.0'
# Dnspod China
( set -e
set +x
API="https://dnsapi.cn"
Token="login_token=$TokenDnspodCN"
mkdir -p "Dnspod/CN"
cd "$_"
DomainID=$(curl "$API/Domain.List" \
-sSLX POST \
-d "$Token" \
-d "format=json" \
| jq -r '.domains[] | select(.name=="'"$Domain"'") | .id')
Records=$(curl "$API/Record.List" \
-sSLX POST \
-d "$Token" \
-d "format=json" \
-d "domain=$Domain" \
| jq '.records[]')
[ "$Records" ]
RecID=$(jq -r 'select(.name=="'"$Rec"'") | .id' <<< "$Records")
[ "$RecID" ]
if [ ! -e "$Rec" ]; then
jq -r 'select(.name=="'"$Rec"'") | .value' <<< "$Records" > "$Rec"
echo "[Dnspod-CN] Set $Rec.$Domain to "`cat "$Rec"`
fi
LastIP=`cat "$Rec"`
if [ `sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1\*2\^24\+\2\*2\^16\+\3\*2\^8\+\4/' <<<$IP | bc` -eq `sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1\*2\^24\+\2\*2\^16\+\3\*2\^8\+\4/' <<<$LastIP | bc` ]; then
echo "[Dnspod-CN] Nothing change for $Rec.$Domain ($IP)"
else
echo "[Dnspod-CN] Update $Rec.$Domain ($LastIP -> $IP)"
curl "$API/Record.Ddns" \
-sSLX POST \
-d "$Token" \
-d "domain=$Domain" \
-d "format=json" \
-d "record_id=$RecID" \
-d "record_line=默认" \
-d "sub_domain=$Rec" \
-d "value=$IP" \
| jq '.'
rm -rf "$Rec"
fi
) || echo "ERROR: Failed to update [Dnspod-CN]"
# Dnspod Intl
( set -e
set +x
API="https://api.dnspod.com"
Token="user_token=$TokenDnspodIntl"
mkdir -p "Dnspod/Intl"
cd "$_"
DomainID=$(curl "$API/Domain.List" \
-sSLX POST \
-d "$Token" \
-d "format=json" \
| jq -r '.domains[] | select(.name=="'"$Domain"'") | .id')
Records=$(curl "$API/Record.List" \
-sSLX POST \
-d "$Token" \
-d "format=json" \
-d "domain=$Domain" \
| jq '.records[]')
[ "$Records" ]
RecID=$(jq -r 'select(.name=="'"$Rec"'") | .id' <<< "$Records")
[ "$RecID" ]
if [ ! -e "$Rec" ]; then
jq -r 'select(.name=="'"$Rec"'") | .value' <<< "$Records" > "$Rec"
echo "[Dnspod-Intl] Set $Rec.$Domain to "`cat "$Rec"`
fi
LastIP=`cat "$Rec"`
if [ `sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1\*2\^24\+\2\*2\^16\+\3\*2\^8\+\4/' <<<$IP | bc` -eq `sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1\*2\^24\+\2\*2\^16\+\3\*2\^8\+\4/' <<<$LastIP | bc` ]; then
echo "[Dnspod-Intl] Nothing change for $Rec.$Domain ($IP)"
else
echo "[Dnspod-Intl] Update $Rec.$Domain ($LastIP -> $IP)"
curl "$API/Record.Ddns" \
-sSLX POST \
-d "$Token" \
-d "domain=$Domain" \
-d "format=json" \
-d "record_id=$RecID" \
-d "record_line=default" \
-d "sub_domain=$Rec" \
-d "value=$IP" \
| jq '.'
rm -rf "$Rec"
fi
) || echo "ERROR: Failed to update [Dnspod-Intl]"
# DNS.com
( set -e
set +x
API="https://www.dns.com/api"
Token="apiKey=$TokenDNSCOMKey"
mkdir -p "DNS.com"
cd "$_"
# curl "$API/domain/list/" \
# -sSLX POST \
# -H 'Content-type:text/html;charset=utf-8' \
# -d "$Token" \
# -d "hash="$(md5 <<< "$Token$TokenDNSCOMSecret") \
# | jq '.'
) || echo "ERROR: Failed to update [DNS.com]"
done
sleep 10
done
cd
rm -rf "$LastDir"