mirror of
https://github.com/coolsnowwolf/packages.git
synced 2025-05-02 11:22:42 +08:00
Fix grep patterns
copy from https://github.com/openwrt/packages/blob/master/net/ddns-scripts/files/update_cloudflare_com_v4.sh
This commit is contained in:
parent
a54d824e8f
commit
724a08e5e5
@ -29,6 +29,7 @@
|
|||||||
# used variables
|
# used variables
|
||||||
local __HOST __DOMAIN __TYPE __URLBASE __PRGBASE __RUNPROG __DATA __IPV6 __ZONEID __RECID __PROXIED
|
local __HOST __DOMAIN __TYPE __URLBASE __PRGBASE __RUNPROG __DATA __IPV6 __ZONEID __RECID __PROXIED
|
||||||
local __URLBASE="https://api.cloudflare.com/client/v4"
|
local __URLBASE="https://api.cloudflare.com/client/v4"
|
||||||
|
local __TTL=120
|
||||||
|
|
||||||
# split __HOST __DOMAIN from $domain
|
# split __HOST __DOMAIN from $domain
|
||||||
# given data:
|
# given data:
|
||||||
@ -84,7 +85,7 @@ cloudflare_transfer() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
# check for error
|
# check for error
|
||||||
grep -q '"success":true' $DATFILE || {
|
grep -q '"success":\s*true' $DATFILE || {
|
||||||
write_log 4 "CloudFlare reported an error:"
|
write_log 4 "CloudFlare reported an error:"
|
||||||
write_log 7 "$(cat $DATFILE)" # report error
|
write_log 7 "$(cat $DATFILE)" # report error
|
||||||
return 1 # HTTP-Fehler
|
return 1 # HTTP-Fehler
|
||||||
@ -127,13 +128,12 @@ fi
|
|||||||
__PRGBASE="$__PRGBASE --header 'X-Auth-Email: $username' "
|
__PRGBASE="$__PRGBASE --header 'X-Auth-Email: $username' "
|
||||||
__PRGBASE="$__PRGBASE --header 'X-Auth-Key: $password' "
|
__PRGBASE="$__PRGBASE --header 'X-Auth-Key: $password' "
|
||||||
__PRGBASE="$__PRGBASE --header 'Content-Type: application/json' "
|
__PRGBASE="$__PRGBASE --header 'Content-Type: application/json' "
|
||||||
# __PRGBASE="$__PRGBASE --header 'Accept: application/json' "
|
|
||||||
|
|
||||||
# read zone id for registered domain.TLD
|
# read zone id for registered domain.TLD
|
||||||
__RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones?name=$__DOMAIN'"
|
__RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones?name=$__DOMAIN'"
|
||||||
cloudflare_transfer || return 1
|
cloudflare_transfer || return 1
|
||||||
# extract zone id
|
# extract zone id
|
||||||
__ZONEID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
__ZONEID=$(grep -o '"id":\s*"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
||||||
[ -z "$__ZONEID" ] && {
|
[ -z "$__ZONEID" ] && {
|
||||||
write_log 4 "Could not detect 'zone id' for domain.tld: '$__DOMAIN'"
|
write_log 4 "Could not detect 'zone id' for domain.tld: '$__DOMAIN'"
|
||||||
return 127
|
return 127
|
||||||
@ -143,14 +143,14 @@ __ZONEID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
|||||||
__RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones/$__ZONEID/dns_records?name=$__HOST&type=$__TYPE'"
|
__RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones/$__ZONEID/dns_records?name=$__HOST&type=$__TYPE'"
|
||||||
cloudflare_transfer || return 1
|
cloudflare_transfer || return 1
|
||||||
# extract record id
|
# extract record id
|
||||||
__RECID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
__RECID=$(grep -o '"id":\s*"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
||||||
[ -z "$__RECID" ] && {
|
[ -z "$__RECID" ] && {
|
||||||
write_log 4 "Could not detect 'record id' for host.domain.tld: '$__HOST'"
|
write_log 4 "Could not detect 'record id' for host.domain.tld: '$__HOST'"
|
||||||
return 127
|
return 127
|
||||||
}
|
}
|
||||||
|
|
||||||
# extract current stored IP
|
# extract current stored IP
|
||||||
__DATA=$(grep -o '"content":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
__DATA=$(grep -o '"content":\s*"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
||||||
|
|
||||||
# check data
|
# check data
|
||||||
[ $use_ipv6 -eq 0 ] \
|
[ $use_ipv6 -eq 0 ] \
|
||||||
@ -178,11 +178,11 @@ __DATA=$(grep -o '"content":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
|
|||||||
# update is needed
|
# update is needed
|
||||||
# let's build data to send
|
# let's build data to send
|
||||||
# set proxied parameter
|
# set proxied parameter
|
||||||
__PROXIED=$(grep -o '"proxied":[^",]*' $DATFILE | grep -o '[^:]*$')
|
__PROXIED=$(grep -o '"proxied":\s*[^",]*' $DATFILE | grep -o '[^:]*$')
|
||||||
|
|
||||||
# use file to work around " needed for json
|
# use file to work around " needed for json
|
||||||
cat > $DATFILE << EOF
|
cat > $DATFILE << EOF
|
||||||
{"id":"$__ZONEID","type":"$__TYPE","name":"$__HOST","content":"$__IP","proxied":$__PROXIED}
|
{"id":"$__ZONEID","type":"$__TYPE","name":"$__HOST","content":"$__IP","ttl":$__TTL,"proxied":$__PROXIED}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# let's complete transfer command
|
# let's complete transfer command
|
||||||
@ -190,4 +190,3 @@ __RUNPROG="$__PRGBASE --request PUT --data @$DATFILE '$__URLBASE/zones/$__ZONEID
|
|||||||
cloudflare_transfer || return 1
|
cloudflare_transfer || return 1
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user