cfip.acc phiên bản khác

Cập nhật IP cho tất cả record A, AAAA thuộc một tài khoản cloudflare.com

cfip.acc [-f][-e auth_email][-k auth_key][-4 ip_v4][-6 ip_v6][-x pattern[,pattern2]]

-f : luôn cập nhật
-e : AUTH_EMAIL, có thể ghi qua option này hay ghi trong script
-k : AUTH_KEY, có thể ghi qua option này hay ghi trong script
-4 : ip version 4, mặc định là WAN IP v4
-6 : ip version 6, phải ghi nếu muốn dùng ip v6
-x : bỏ qua không cập nhật các domain, subdomain theo mẫu regexp. 'example' thay cho domain có chữ example như example.net, example.com và các subdomain của nó, tuy nhiên '^example.com$' chỉ thay cho domain example.com vì không khớp với example.com.vn hay bất kỳ subdomain nào

Script

#!/bin/bash
# cfip.acc
# Update IP for all A, AAAA records belonging to cloudflare.com account
# © 2020 LNT <lnt@lyle.info>
# version 20220324
#
cmd=()
[ ! `command -v jq` ] && cmd+=('jq')
[ ! `command -v dig` ] && cmd+=('dnsutils')
if (( ${#cmd[@]} )); then
  q="Chúng ta cần các ứng dụng \"${cmd[@]}\". Cài đặt (Y/n)? "
  read -n1 -t5 -p "$q" a
  echo
  [ "${a:-Y}" == 'Y' ] && apt install -y ${cmd[@]} || exit
fi
AUTH_EMAIL='<your_auth_email>'
AUTH_KEY='<your_auth_key>'
E='→ '
E0="Lỗi!"
E1="Sai haythiếu AUTH EMAIL/KEY!"
E2="IP không thay đổi!"
SHM=/dev/shm/cfIP
[ -d $SHM ]||mkdir $SHM
ff=0;X='';ip4='';ip6=''
while [ "$1" != "" ]; do
case $1 in
-f) ff=1;rm -rf $SHM/*;;
-4) shift;ip4="$1";;
-6) shift;ip6="$1";;
-e) shift;AUTH_EMAIL="$1";;
-k) shift;AUTH_KEY="$1";;
-x) shift;X="$1";;
 *) echo Cập nhật IP cho tất cả record A, AAAA thuộc một tài khoản cloudflare.com
    echo $(basename $0) [-f][-e auth_email][-k auth_key][-4 ip_v4][-6 ip_v6][-x pattern[,pattern2]]
    exit 1
esac
shift
done
[[ ! $ip4 =~ ([0-9]{1,3}\.){3}[0-9]{1,3} ]]&&ip4=`dig txt ch +short @1.1.1.1 whoami.cloudflare|sed 's/"//g'`
[[ "$ip6" && ! $ip6 =~ ([0-9]{1,4}:){3}[0-9]{1,4} ]]&&ip6=`dig txt ch +short @2606:4700:4700::1111 whoami.cloudflare|sed 's/"//g'`
[[ ! "$AUTH_EMAIL" || ! "$AUTH_KEY" ]]&&{ echo "$E$E1";exit 1; }
[ -e "$SHM/ip" ]&&oIP=$(<$SHM/ip)||oIP=''
[[ $ff == 0 && "$ip4" == "$oIP" ]]&&{ echo "$E$E2";exit 1; }||printf $ip4>$SHM/ip
X=`sed 's/,/ /g'<<<$X`;x=''
for e in $X;do [[ "$x" ]]&&x="$x or (.name|test(\"$e\"))"||x="(.name|test(\"$e\"))";done
BASE_URL='https://api.cloudflare.com/client/v4/zones'
Z=`curl -s -X GET "$BASE_URL" -H "Content-Type:application/json" -H "X-Auth-Key:$AUTH_KEY" -H "X-Auth-Email:$AUTH_EMAIL"|jq -r '.result|.[]|select(.status=="active")|with_entries(select(.key == ("id","name")))|"\(.id)=\(.name)"' 2>/dev/null`
[ -z "$Z" ]&&{ echo "$E$E1";exit 1; }
for z in $Z;do
  echo "+ Domain ${z#*=}"
  zid=${z%=*}
  [[ "$ip6" ]]&&typ="A,AAAA"||typ="A"
  tmp=`curl -s -X GET "${BASE_URL}/$zid/dns_records?type=$typ" -H "X-Auth-Email:${AUTH_EMAIL}" -H "X-Auth-Key:${AUTH_KEY}" -H "Content-Type:application/json"|jq -r '.result[]|{id,name,content,type}'|jq -s .`
  [[ "$x" ]]&&hosts=$(echo "$tmp"|jq ".[]|del(select($x))|select(. != null)"|jq -s .)||hosts=$(echo "$tmp"|jq -s ".[]")
  echo "$hosts"|jq -r '.[]|[.id,.name,.content,.type]|@tsv'|
  while IPS=$'\t' read id name cIP type;do
    [ $type == 'A' ]&&ip="$ip4"||ip="$ip6"
    if [ "$ip" == "$cIP" ];then ret="$E$E2";else
      ret=`curl -s -X PUT ${BASE_URL}/$zid/dns_records/$id \
      -H "X-Auth-Email:${AUTH_EMAIL}" -H "X-Auth-Key:${AUTH_KEY}" -H "Content-Type:application/json" \
      --data "{\"type\":\"$type\",\"name\":\"$name\",\"content\":\"$ip\",\"ttl\":3600,\"proxied\":false}"|jq -r '.success'`
      [[ $ret ]]&&ret="$cIP $E$ip"||ret="$E$E0"
    fi
    echo "  $name: $ret"
  done
done

Comments Off on cfip.acc phiên bản khác

Filed under Software

Comments are closed.