美文网首页
CloudFlare API 在 openwrt上实现 DDNS

CloudFlare API 在 openwrt上实现 DDNS

作者: haolang | 来源:发表于2020-05-07 21:50 被阅读0次

    配置设备:

    斐讯n1盒子 f大openwrt版本 f大openwrt

    参考链接:

    代码如下

    #!/bin/bash
    auth_email="youemail@mail.com"
    auth_key="*********"
    record_name="example.com"
    # ipv6 为 AAAA记录 ,ipv4 为 A 记录
    record_type="AAAA"  
    # ip 的获取要根据实际情况修改
    ip=$(ifconfig br-lan |grep inet6|grep Global|grep -v /60|awk '{print $3}'|awk -F/ '{print $1}')
    ip_file="ip.txt"
    
    # 判断 IP 是否变化
    old_ip=$(cat $ip_file)
    if [ "$ip" == "$old_ip" ]; then
        echo "IP has not changed."
        exit 0
    fi
    
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | awk -F , '{print $1}' | awk -F \" '{print $6}' )
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=$record_type" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep \"id\" | awk -F \" '{print $4}')
    echo "$zone_identifier"
    echo "$record_identifier"
    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\"}"
    # 保存变化后的ip
    echo "$ip"> $ip_file
    

    部分shell脚本说明

    ifconfig -a br-lan  //获取br-lan接口的ipv6地址
    awk '{print $3}'    //grep Global 获取带有Global字样的行
    grep -v /60        //删除带有 /60 的行,即本地ipv6地址
    awk '{print $3}'   // 使用空格或tab分割字符串并取得分割后的第三个部分
    awk -F/ '{print $1}'   // 使用 / 分割字符串获取第一部分
    

    修改前错误出现错误:API UPDATE FAILED. DUMPING RESULTS

    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
    #若不删除上面部分会出现以下错误提示,原因未知
    API UPDATE FAILED. DUMPING RESULTS:
    {"success":false,"errors":[{"code":7003,"message":"Could not route to \/zones\/dns_records, perhaps your object identifier is invalid?"},{"code":"7000","message":"No route for that URI"}],"messages":[],"result":null}
    

    相关文章

      网友评论

          本文标题:CloudFlare API 在 openwrt上实现 DDNS

          本文链接:https://www.haomeiwen.com/subject/jthtghtx.html