一、前言
这个脚本忘了从哪里搞来的了,今天要用的时候发现并用不了???然后测试了一下发现api调用有一处错误,就修改了一下。然后又丢上来水一篇文章。
二、正文
1.参数讲解
1 2 3 4 5 6 7 8 9 10 |
# CloudFlare 注册邮箱 auth_email="" # CloudFlare Global API Key ,在cloudflare登录后,查看个人资料,在APi令牌页面查看 auth_key="" # 根域名,比如你要ddns www.google.com,那么根域名就填google.com zone_name="" # DDNS的域名,比如你想ddns www.google.com,那么就填www.google.com record_name="" # IP类型,填ipv4或ipv6 type="ipv6" |
2.代码本体
最后更新于2022-01-20。
那么就是代码本体了。把下面的内容保存为一个脚本,然后在服务器上使用crontab定时运行就可以了,一个小时一次就可以,api调用貌似是有限制的1小时5次。
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 |
#!/bin/bash ############## 用户配置(修改这里) ############### # CloudFlare 注册邮箱 auth_email="" # CloudFlare Global API Key auth_key="" # 根域名 zone_name="" # DDNS的域名 record_name="" # IP类型,填ipv4或ipv6 type="ipv6" ############# 脚本配置(不懂不要修改) ############ # 变动前的公网 IP 保存位置 ip_file="./ip.txt" # 域名识别信息保存位置 id_file="./cloudflare.ids" # 监测日志保存位置 log_file="./cloudflare.log" ################ 下面的不懂不要改 ############### ################## 功能定义 #################### record_type="" ip="" zone_identifier="" record_identifier="" update="" #日志 log() { if [ "$1" ]; then echo -e "[$(date)] - $1" >> $log_file fi } #获取本机IP get_ip() { if [ $type == "ipv4" ]; then record_type="A" ip=$(curl -s http://v4.ipv6-test.com/api/myip.php) elif [ $type == "ipv6" ]; then record_type="AAAA" ip=$(curl -s http://v6.ipv6-test.com/api/myip.php) else echo "Type wrong" log "Type wrong" exit 0 fi } #判断IP是否变化,不变化则结束程序 check_ip_change() { if [ -f $ip_file ]; then old_ip=$(cat $ip_file) if [ "$ip" == "$old_ip" ]; then echo "IP has not changed." log "IP has not changed." exit 0 fi fi } #获取域名zone_id和子域名记录ID get_id() { if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then zone_identifier=$(head -1 $id_file) record_identifier=$(tail -1 $id_file) else zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 ) rec_response_json=`curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json"` record_identifier=`echo $rec_response_json | grep -Po '(?<="id":")[^"]*'` echo "$zone_identifier" > $id_file echo "$record_identifier" >> $id_file fi } #更新 DNS 记录 update_dns() { update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"$record_type\",\"name\":\"$record_name\",\"content\":\"$ip\"}") } ################### 脚本主体 ################### log "Script start." #获取IP get_ip #判断是否成功获取到IP if [ "$ip" == "" ]; then echo "Can not get IP address.Please check your network connection." log "Can not get IP address.Please check your network connection." exit 0 fi #检查IP是否变化 check_ip_change #获取域名zone_id和子域名记录ID get_id #判断是否成功获取到ID if [ "$zone_identifier" == "" ]; then echo "Can not get zone_id." log "Can not get zone_id." exit 0 elif [ "$record_identifier" == "" ]; then echo "Can not get record_id." log "Can not get record_id." exit 0 fi #更新 DNS 记录 update_dns #判断是否成功 if [[ $update == *"\"success\":false"* ]]; then log "API UPDATE FAILED. DUMPING RESULTS:\n$update" echo -e "API UPDATE FAILED. DUMPING RESULTS:\n$update" exit 0 else echo "$ip" > $ip_file log "$record_name IP changed to: $ip" echo "$record_name IP changed to: $ip" fi |
发表回复