Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

运行无效果,但未报错 #7

Open
ywly opened this issue May 1, 2020 · 26 comments
Open

运行无效果,但未报错 #7

ywly opened this issue May 1, 2020 · 26 comments

Comments

@ywly
Copy link

ywly commented May 1, 2020

每次./脚本后
输出“no file.need ip” 和 “Updating zone_identifier & record_identifier”后无反应
cf面板未更新ip,且本地未生成两个存档数据的txt文件
我使用了全局api key 并且在两台机器上分别使用debian和centos做过测试,也用文件内使用方法在/usr目录下实验过
能想到的原因均测试过,实在没办法请教大佬们,希望有大佬解惑

@yilin101
Copy link

yilin101 commented May 2, 2020

同样问题
现在是不是只能用令牌api了?

@ywly
Copy link
Author

ywly commented May 2, 2020

同样问题
现在是不是只能用令牌api了?

Global API Key,我用这个不能用,令牌是哪个?

@yilin101
Copy link

yilin101 commented May 2, 2020

同样问题
现在是不是只能用令牌api了?

Global API Key,我用这个不能用,令牌是哪个?

在Global API Key上面 不过我试了也不成功

@ywly ywly closed this as completed May 2, 2020
@ywly
Copy link
Author

ywly commented May 2, 2020

同样问题
现在是不是只能用令牌api了?

Global API Key,我用这个不能用,令牌是哪个?

在Global API Key上面 不过我试了也不成功

应该是代码问题,我试了每一个接口都有正常的数据
我不小心关闭了这个,有办法重新打开吗

@ywly ywly reopened this May 2, 2020
@yilin101
Copy link

yilin101 commented May 2, 2020

同样问题
现在是不是只能用令牌api了?

Global API Key,我用这个不能用,令牌是哪个?

在Global API Key上面 不过我试了也不成功

应该是代码问题,我试了每一个接口都有正常的数据
我不小心关闭了这个,有办法重新打开吗

打开了

我怎么感觉是cf 的问题
https://community.cloudflare.com/t/no-longer-able-to-update-dns-records-via-api/143942/49

@ywly
Copy link
Author

ywly commented May 2, 2020

同样问题
现在是不是只能用令牌api了?

Global API Key,我用这个不能用,令牌是哪个?

在Global API Key上面 不过我试了也不成功

应该是代码问题,我试了每一个接口都有正常的数据
我不小心关闭了这个,有办法重新打开吗

打开了

我怎么感觉是cf 的问题
https://community.cloudflare.com/t/no-longer-able-to-update-dns-records-via-api/143942/49

是的,我看了看文档,是因为CFRECORD_ID这个值取不到,所以脚本退出了

@chenyang12315
Copy link

chenyang12315 commented May 2, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。

不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

@yilin101
Copy link

yilin101 commented May 3, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。

不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

ZONE_ID和RECORD_ID 可以获取 我也获取到过
不过确实不知道问题在哪 头疼

@ywly
Copy link
Author

ywly commented May 3, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。
不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

ZONE_ID和RECORD_ID 可以获取 我也获取到过
不过确实不知道问题在哪 头疼

如果两个都可以获取,就把提交的那个命令中\去掉试试,那个文档中,有人这样可以解决

@ghost
Copy link

ghost commented May 5, 2020

同错误

@ywly
Copy link
Author

ywly commented May 6, 2020

`#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

Automatically update your CloudFlare DNS record to the IP, Dynamic DNS

Can retrieve cloudflare Domain id and list zone's, because, lazy

Place at:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh

run crontab -e and add next line:

*/1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1

or you need log:

*/1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

Usage:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \ # fqdn of the record you want to update

-z example.com \ # will show you all zones if forgot, but you need this

Optional flags:

-f false|true \ # force dns update, disregard local stored ip

default config

API key, see https://www.cloudflare.com/a/account/my-account,

incorrect api-key results in E_UNAUTH error

CFKEY=xxxxx

Username, eg: malei@example.com

CFUSER=xxxxx

Zone name, eg: github.com

CFZONE_NAME=xxxx

Hostname to update, eg: www.github.com

CFRECORD_NAME=xxxxx

Record type, A(IPv4)|AAAA(IPv6), default IPv4

CFRECORD_TYPE=A

Cloudflare TTL for record, between 120 and 86400 seconds

CFTTL=120

Ignore local file, update ip anyway

FORCE=false

Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ...

WANIPSITE="http://icanhazip.com"

get parameter

while getopts k:u:h:z:f: opts; do
case ${opts} in
k) CFKEY=${OPTARG} ;;
u) CFUSER=${OPTARG} ;;
h) CFRECORD_NAME=${OPTARG} ;;
z) CFZONE_NAME=${OPTARG} ;;
f) FORCE=${OPTARG} ;;
esac
done

If required settings are missing just exit

if [ "$CFKEY" = "" ]; then
echo "Missing api-key, get at: https://www.cloudflare.com/a/account/my-account"
echo "and save in ${0} or using the -k flag"
exit 2
fi
if [ "$CFUSER" = "" ]; then
echo "Missing username, probably your email-address"
echo "and save in ${0} or using the -u flag"
exit 2
fi
if [ "$CFRECORD_NAME" = "" ]; then
echo "Missing hostname, what host do you want to update?"
echo "save in ${0} or using the -h flag"
exit 2
fi

If the hostname is not a FQDN

if [ "$CFRECORD_NAME" != "$CFZONE_NAME" ] && ! [ -z "${CFRECORD_NAME##*$CFZONE_NAME}" ]; then
CFRECORD_NAME="$CFRECORD_NAME.$CFZONE_NAME"
echo " => Hostname is not a FQDN, assuming $CFRECORD_NAME"
fi

Get current and old WAN ip

WAN_IP=curl -s ${WANIPSITE}
if [ -f $HOME/.wan_ip-cf.txt ]; then
OLD_WAN_IP=cat $HOME/.wan_ip-cf.txt
else
echo "No file, need IP"
OLD_WAN_IP=""
fi

If WAN IP is unchanged an not -f flag, exit here

if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then
echo "WAN IP Unchanged, to update anyway use flag -f true"
exit 0
fi

Get zone_identifier & record_identifier

ID_FILE=$HOME/.id-cf.txt
if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ]
&& [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ]
&& [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then
CFZONE_ID=$(sed -n '1,1p' "$ID_FILE")
CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE")
else
echo "Updating zone_identifier & record_identifier"
CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]' | head -1 )
CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id": ")[^"]
')
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE
echo "$CFRECORD_NAME" >> $ID_FILE
fi

If WAN is changed, update cloudflare

echo "Updating DNS to $WAN_IP"

RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID"
-H "X-Auth-Email: $CFUSER"
-H "X-Auth-Key: $CFKEY"
-H "Content-Type: application/json"
--data "{"id":"$CFZONE_ID","type":"$CFRECORD_TYPE","name":"$CFRECORD_NAME","content":"$WAN_IP", "ttl":$CFTTL}")

if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ "$(echo $RESPONSE | grep ""success": true")" != "" ]; then
echo "Updated succesfuly!"
echo $WAN_IP > $HOME/.wan_ip-cf.txt
exit
else
echo 'Something went wrong :('
echo "Response: $RESPONSE"
exit 1
fi
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

@ywly
Copy link
Author

ywly commented May 6, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。
不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

ZONE_ID和RECORD_ID 可以获取 我也获取到过
不过确实不知道问题在哪 头疼

我上面这个可以用

@ywly
Copy link
Author

ywly commented May 6, 2020

同错误

我上面发这个可以用,自己改的

@ywly
Copy link
Author

ywly commented May 6, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。

不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

我发的这个可以用,可以试一下,等作者修复
不太会用issue发代码,格式乱了

@ghost
Copy link

ghost commented May 6, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。
不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

我发的这个可以用,可以试一下,等作者修复
不太会用issue发代码,格式乱了

可否发一份文件 jiangkechen@yandex.com

@ywly
Copy link
Author

ywly commented May 6, 2020

遇到了相同的问题。在提示:

root@debian:~# ./cf-v4-ddns.sh
No file, need IP
Updating zone_identifier & record_identifier

后直接退出。
不过我尝试手动获取CFZONE_ID和CFRECORD_ID都成功了,看起来CF那边是没什么问题的。不太理解出现了什么问题。

我发的这个可以用,可以试一下,等作者修复
不太会用issue发代码,格式乱了

可否发一份文件 jiangkechen@yandex.com

已发送

@skytt
Copy link

skytt commented May 6, 2020

好像是正则的时候有空格没有匹配到CFRECORD_ID导致的,加了个\s后正常了,我改了一下发了个pull

xiaoxin2016 added a commit to xiaoxin2016/cloudflare-api-v4-ddns that referenced this issue May 12, 2020
The regular expression has been changed to match 32-bit strings (CFZONE_ID and CFRECORD_ID format) starting with 【[{"id":"】 and ending with 【"】.

corresponding: yulewang#7
@xiaoxin2016
Copy link
Contributor

xiaoxin2016 commented May 12, 2020

确实是正则表达式匹配导致的错误,但是加了个[\s*]似乎不影响我的运行结果,还是异常退出。
我改了一下正则表达式,改成以【[{"id":"】开头,以【"】结尾的32位字符串。

@yilin101
Copy link

`#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

Automatically update your CloudFlare DNS record to the IP, Dynamic DNS

Can retrieve cloudflare Domain id and list zone's, because, lazy

Place at:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh

run crontab -e and add next line:

*/1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1

or you need log:

*/1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

Usage:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \ # fqdn of the record you want to update

-z example.com \ # will show you all zones if forgot, but you need this

Optional flags:

-f false|true \ # force dns update, disregard local stored ip

default config

API key, see https://www.cloudflare.com/a/account/my-account,

incorrect api-key results in E_UNAUTH error

CFKEY=xxxxx

Username, eg: malei@example.com

CFUSER=xxxxx

Zone name, eg: github.com

CFZONE_NAME=xxxx

Hostname to update, eg: www.github.com

CFRECORD_NAME=xxxxx

Record type, A(IPv4)|AAAA(IPv6), default IPv4

CFRECORD_TYPE=A

Cloudflare TTL for record, between 120 and 86400 seconds

CFTTL=120

Ignore local file, update ip anyway

FORCE=false

Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ...

WANIPSITE="http://icanhazip.com"

get parameter

while getopts k:u:h:z:f: opts; do
case ${opts} in
k) CFKEY=${OPTARG} ;;
u) CFUSER=${OPTARG} ;;
h) CFRECORD_NAME=${OPTARG} ;;
z) CFZONE_NAME=${OPTARG} ;;
f) FORCE=${OPTARG} ;;
esac
done

If required settings are missing just exit

if [ "$CFKEY" = "" ]; then
echo "Missing api-key, get at: https://www.cloudflare.com/a/account/my-account"
echo "and save in ${0} or using the -k flag"
exit 2
fi
if [ "$CFUSER" = "" ]; then
echo "Missing username, probably your email-address"
echo "and save in ${0} or using the -u flag"
exit 2
fi
if [ "$CFRECORD_NAME" = "" ]; then
echo "Missing hostname, what host do you want to update?"
echo "save in ${0} or using the -h flag"
exit 2
fi

If the hostname is not a FQDN

if [ "$CFRECORD_NAME" != "$CFZONE_NAME" ] && ! [ -z "${CFRECORD_NAME##*$CFZONE_NAME}" ]; then
CFRECORD_NAME="$CFRECORD_NAME.$CFZONE_NAME"
echo " => Hostname is not a FQDN, assuming $CFRECORD_NAME"
fi

Get current and old WAN ip

WAN_IP=curl -s ${WANIPSITE}
if [ -f $HOME/.wan_ip-cf.txt ]; then
OLD_WAN_IP=cat $HOME/.wan_ip-cf.txt
else
echo "No file, need IP"
OLD_WAN_IP=""
fi

If WAN IP is unchanged an not -f flag, exit here

if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then
echo "WAN IP Unchanged, to update anyway use flag -f true"
exit 0
fi

Get zone_identifier & record_identifier

ID_FILE=$HOME/.id-cf.txt
if [ -f $ID_FILE ] &amp;&amp; [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ]
&& [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ]
&& [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then
CFZONE_ID=$(sed -n '1,1p' "$ID_FILE")
CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE")
else
echo "Updating zone_identifier & record_identifier"
CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]' | head -1 ) CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id": ")[^"]')
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE
echo "$CFRECORD_NAME" >> $ID_FILE
fi

If WAN is changed, update cloudflare

echo "Updating DNS to $WAN_IP"

RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID"
-H "X-Auth-Email: $CFUSER"
-H "X-Auth-Key: $CFKEY"
-H "Content-Type: application/json"
--data "{"id":"$CFZONE_ID","type":"$CFRECORD_TYPE","name":"$CFRECORD_NAME","content":"$WAN_IP", "ttl":$CFTTL}")

if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ "$(echo $RESPONSE | grep ""success": true")" != "" ]; then
echo "Updated succesfuly!"
echo $WAN_IP > $HOME/.wan_ip-cf.txt
exit
else
echo 'Something went wrong :('
echo "Response: $RESPONSE"
exit 1
fi
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

也发一份给我吧 谢谢
yilin101#qq.com

eicky pushed a commit to eicky/cloudflare-api-v4-ddns that referenced this issue May 16, 2020
@eicky eicky mentioned this issue May 16, 2020
@Scholar-m
Copy link

/ usr / bin / env bash

设置-o errexit
设置-o名词
设置-o pipefail

自动将您的CloudFlare DNS记录更新为IP,动态DNS

可以检索cloudflare域ID和列表区域,因为,

放置在:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod + x / usr / local / bin / cf-ddns.sh

运行crontab -e并添加下一行:

* / 1 * * * * /usr/local/bin/cf-ddns.sh> / dev / null 2>&1

或者您需要日志:

* / 1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

用法:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \#要更新的记录的fqdn

-z example.com \#如果忘记了,将显示所有区域,但是您需要

可选标志:

-f false | true \#强制dns更新,忽略本地存储的ip

默认配置

API密钥,请参见https://www.cloudflare.com/a/account/my-account,

错误的api键会导致E_UNAUTH错误

CFKEY = xxxxx

用户名,例如:malei@example.com

CFUSER = xxxxx

区域名称,例如:github.com

CFZONE_NAME = xxxx

要更新的主机名,例如:www.github.com

CFRECORD_NAME = xxxxx

记录类型,A(IPv4)| AAAA(IPv6),默认IPv4

CFRECORD_TYPE = A

Cloudflare TTL记录,介于120到86400秒之间

CFTTL = 120

忽略本地文件,仍然更新IP

FORCE =假

用于检索WAN ip的站点,其他示例包括:bot.whatismyipaddress.com,https://api.ipify.org/ ...

WANIPSITE =“ http://icanhazip.com

获取参数

而getopts k:u:h:z:f:opts;

k)CFKEY = $ {OPTARG}中做$ {opts}的情况;
u)CFUSER = $ {OPTARG} ;;
h)CFRECORD_NAME = $ {OPTARG} ;;
z)CFZONE_NAME = $ {OPTARG} ;;
f)FORCE = $ {OPTARG} ;;
esac
完成

如果缺少所需的设置,请退出

如果[“ $ CFKEY” =“”]; 然后
回声“缺少API密钥,获得在:https://www.cloudflare.com/a/account/my-account
回声“并保存在$ {0}或使用-k标志”
2号出口
科幻
如果[ “ $ CFUSER” =“”]; 然后
回显“缺少用户名,可能是您的电子邮件地址”,
回显“并保存在$ {0}或使用-u标志” , 如果[“ $ CFRECORD_NAME” =“”],则
退出2
fi
。然后
回显“缺少主机名,您要更新哪个主机?”
回声“保存在$ {0}或使用-h标志”
出口2
网络连接

如果主机名不是FQDN

如果[[“ $ CFRECORD_NAME”!=“ $ CFZONE_NAME”] &&![-z“ $ {CFRECORD_NAME ## * $ CFZONE_NAME}”];然后
CFRECORD_NAME =“ $ CFRECORD_NAME。$ CFZONE_NAME”
echo“ =>主机名不是FQDN,假设$ CFRECORD_NAME”
fi

获取当前和旧的WAN IP

WAN_IP = curl -s ${WANIPSITE}
如果[-f $ HOME / .wan_ip-cf.txt]; 然后
OLD_WAN_IP = cat $HOME/.wan_ip-cf.txt
否则
回显“无文件,需要IP”
OLD_WAN_IP =“”
fi

如果WAN IP不变,则不是-f标志,请在此处退出

如果[“ $ WAN_IP” =“ $ OLD_WAN_IP”] && [“ $ FORCE” = false]; 然后
回显“ WAN IP未更改,无论如何都要使用标志-f true进行更新”,
退出0
fi

获取zone_identifier和record_identifier

ID_FILE = $ HOME / .ID-cf.txt
如果[$ -f ID_FILE] && [$(WC -l $ ID_FILE |切-d “” -f 1)== 4]
&& [“$(SED -N' 3,1p'“ $ ID_FILE”)“ ==” $ CFZONE_NAME“]
&& [” $(sed -n'4,1p'“ $ ID_FILE”)“ ==” $ CFRECORD_NAME“]; 然后
CFZONE_ID = $(sed -n'1,1p'“ $ ID_FILE”)
CFRECORD_ID = $(sed -n'2,1p'“ $ ID_FILE”)
否则
回显“更新zone_identifier和record_identifier”
CFZONE_ID = $(curl -s -X GET“ https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME ” -H“ X-Auth-Email:$ CFUSER” -H“ X-Auth-Key:$ CFKEY”- H“内容类型:application / json” | grep -Po' (?<=“ id”:“)[^”] '| 头-1) CFRECORD_ID = $(curl -s -X GET“https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME “ -H” X-Auth-Email:$ CFUSER“ -H” X-Auth-Key:$ CFKEY“ -H“内容类型:应用程序/ json” | grep -Po'(?<=“ id”:“)[^”] ')
回声“ $ CFZONE_ID”> $ ID_FILE
回声“ $ CFRECORD_ID” >> $ ID_FILE
回声“ $ CFZONE_NAME” >> $ ID_FILE
回显“ $ CFRECORD_NAME” >> $ ID_FILE
fi

如果更改了WAN,请更新cloudflare

回显“将DNS更新为$ WAN_IP”

RESPONSE = $(curl -s -X PUT“ https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID
-H“ X-Auth-Email:$ CFUSER”
-H“ X-Auth-Key:$ CFKEY“
-H”内容类型:application / json“
--data” {“ id”:“ $ CFZONE_ID”,“ type”:“ $ CFRECORD_TYPE”,“名称”:“ $ CFRECORD_NAME “,” content“:” $ WAN_IP“,” ttl“:$ CFTTL}”)

if [“ $ RESPONSE”!=“” $ {RESPONSE%success *}“] && [” $(echo $ RESPONSE | grep“” success“:true”)“!=”“]; 然后
回显“更新成功!”
回声$ WAN_IP> $ HOME / .wan_ip-cf.txt
退出
其他
回声'出事了:('
回声“回应:$ RESPONSE”
1个出口
科幻
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

这个能发一份给我么 ssvt60@163.com

@eicky
Copy link

eicky commented May 17, 2020

/ usr / bin / env bash

设置-o errexit
设置-o名词
设置-o pipefail

自动将您的CloudFlare DNS记录更新为IP,动态DNS

可以检索cloudflare域ID和列表区域,因为,

放置在:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod + x / usr / local / bin / cf-ddns.sh

运行crontab -e并添加下一行:

* / 1 * * * * /usr/local/bin/cf-ddns.sh> / dev / null 2>&1

或者您需要日志:

* / 1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

用法:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \#要更新的记录的fqdn

-z example.com \#如果忘记了,将显示所有区域,但是您需要

可选标志:

-f false | true \#强制dns更新,忽略本地存储的ip

默认配置

API密钥,请参见https://www.cloudflare.com/a/account/my-account,

错误的api键会导致E_UNAUTH错误

CFKEY = xxxxx

用户名,例如:malei@example.com

CFUSER = xxxxx

区域名称,例如:github.com

CFZONE_NAME = xxxx

要更新的主机名,例如:www.github.com

CFRECORD_NAME = xxxxx

记录类型,A(IPv4)| AAAA(IPv6),默认IPv4

CFRECORD_TYPE = A

Cloudflare TTL记录,介于120到86400秒之间

CFTTL = 120

忽略本地文件,仍然更新IP

FORCE =假

用于检索WAN ip的站点,其他示例包括:bot.whatismyipaddress.com,https://api.ipify.org/ ...

WANIPSITE =“ http://icanhazip.com

获取参数

而getopts k:u:h:z:f:opts;

k)CFKEY = $ {OPTARG}中做$ {opts}的情况;
u)CFUSER = $ {OPTARG} ;;
h)CFRECORD_NAME = $ {OPTARG} ;;
z)CFZONE_NAME = $ {OPTARG} ;;
f)FORCE = $ {OPTARG} ;;
esac
完成

如果缺少所需的设置,请退出

如果[“ $ CFKEY” =“”]; 然后
回声“缺少API密钥,获得在:https://www.cloudflare.com/a/account/my-account
回声“并保存在$ {0}或使用-k标志”
2号出口
科幻
如果[ “ $ CFUSER” =“”]; 然后
回显“缺少用户名,可能是您的电子邮件地址”,
回显“并保存在$ {0}或使用-u标志” , 如果[“ $ CFRECORD_NAME” =“”],则
退出2
fi
。然后
回显“缺少主机名,您要更新哪个主机?”
回声“保存在$ {0}或使用-h标志”
出口2
网络连接

如果主机名不是FQDN

如果[[“ $ CFRECORD_NAME”!=“ $ CFZONE_NAME”] &&![-z“ $ {CFRECORD_NAME ## * $ CFZONE_NAME}”];然后
CFRECORD_NAME =“ $ CFRECORD_NAME。$ CFZONE_NAME”
echo“ =>主机名不是FQDN,假设$ CFRECORD_NAME”
fi

获取当前和旧的WAN IP

WAN_IP = curl -s ${WANIPSITE}
如果[-f $ HOME / .wan_ip-cf.txt]; 然后
OLD_WAN_IP = cat $HOME/.wan_ip-cf.txt
否则
回显“无文件,需要IP”
OLD_WAN_IP =“”
fi

如果WAN IP不变,则不是-f标志,请在此处退出

如果[“ $ WAN_IP” =“ $ OLD_WAN_IP”] && [“ $ FORCE” = false]; 然后
回显“ WAN IP未更改,无论如何都要使用标志-f true进行更新”,
退出0
fi

获取zone_identifier和record_identifier

ID_FILE = $ HOME / .ID-cf.txt
如果[$ -f ID_FILE] && [$(WC -l $ ID_FILE |切-d “” -f 1)== 4]
&& [“$(SED -N' 3,1p'“ $ ID_FILE”)“ ==” $ CFZONE_NAME“]
&& [” $(sed -n'4,1p'“ $ ID_FILE”)“ ==” $ CFRECORD_NAME“]; 然后
CFZONE_ID = $(sed -n'1,1p'“ $ ID_FILE”)
CFRECORD_ID = $(sed -n'2,1p'“ $ ID_FILE”)
否则
回显“更新zone_identifier和record_identifier”
CFZONE_ID = $(curl -s -X GET“ https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME ” -H“ X-Auth-Email:$ CFUSER” -H“ X-Auth-Key:$ CFKEY”- H“内容类型:application / json” | grep -Po' (?<=“ id”:“)[^”] '| 头-1) CFRECORD_ID = $(curl -s -X GET“https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME “ -H” X-Auth-Email:$ CFUSER“ -H” X-Auth-Key:$ CFKEY“ -H“内容类型:应用程序/ json” | grep -Po'(?<=“ id”:“)[^”] ')
回声“ $ CFZONE_ID”> $ ID_FILE
回声“ $ CFRECORD_ID” >> $ ID_FILE
回声“ $ CFZONE_NAME” >> $ ID_FILE
回显“ $ CFRECORD_NAME” >> $ ID_FILE
fi

如果更改了WAN,请更新cloudflare

回显“将DNS更新为$ WAN_IP”
RESPONSE = $(curl -s -X PUT“ https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID
-H“ X-Auth-Email:$ CFUSER”
-H“ X-Auth-Key:$ CFKEY“
-H”内容类型:application / json“
--data” {“ id”:“ $ CFZONE_ID”,“ type”:“ $ CFRECORD_TYPE”,“名称”:“ $ CFRECORD_NAME “,” content“:” $ WAN_IP“,” ttl“:$ CFTTL}”)
if [“ $ RESPONSE”!=“” $ {RESPONSE%success *}“] && [” $(echo $ RESPONSE | grep“” success“:true”)“!=”“]; 然后
回显“更新成功!”
回声$ WAN_IP> $ HOME / .wan_ip-cf.txt
退出
其他
回声'出事了:('
回声“回应:$ RESPONSE”
1个出口
科幻
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

这个能发一份给我么 ssvt60@163.com

看我提交的PR,合并一下就好了

@yilin101
Copy link

/ usr / bin / env bash

设置-o errexit
设置-o名词
设置-o pipefail

自动将您的CloudFlare DNS记录更新为IP,动态DNS

可以检索cloudflare域ID和列表区域,因为,

放置在:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod + x / usr / local / bin / cf-ddns.sh

运行crontab -e并添加下一行:

* / 1 * * * * /usr/local/bin/cf-ddns.sh> / dev / null 2>&1

或者您需要日志:

* / 1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

用法:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \#要更新的记录的fqdn

-z example.com \#如果忘记了,将显示所有区域,但是您需要

可选标志:

-f false | true \#强制dns更新,忽略本地存储的ip

默认配置

API密钥,请参见https://www.cloudflare.com/a/account/my-account,

错误的api键会导致E_UNAUTH错误

CFKEY = xxxxx

用户名,例如:malei@example.com

CFUSER = xxxxx

区域名称,例如:github.com

CFZONE_NAME = xxxx

要更新的主机名,例如:www.github.com

CFRECORD_NAME = xxxxx

记录类型,A(IPv4)| AAAA(IPv6),默认IPv4

CFRECORD_TYPE = A

Cloudflare TTL记录,介于120到86400秒之间

CFTTL = 120

忽略本地文件,仍然更新IP

FORCE =假

用于检索WAN ip的站点,其他示例包括:bot.whatismyipaddress.com,https://api.ipify.org/ ...

WANIPSITE =“ http://icanhazip.com

获取参数

而getopts k:u:h:z:f:opts;

k)CFKEY = $ {OPTARG}中做$ {opts}的情况;
u)CFUSER = $ {OPTARG} ;;
h)CFRECORD_NAME = $ {OPTARG} ;;
z)CFZONE_NAME = $ {OPTARG} ;;
f)FORCE = $ {OPTARG} ;;
esac
完成

如果缺少所需的设置,请退出

如果[“ $ CFKEY” =“”]; 然后
回声“缺少API密钥,获得在:https://www.cloudflare.com/a/account/my-account
回声“并保存在$ {0}或使用-k标志”
2号出口
科幻
如果[ “ $ CFUSER” =“”]; 然后
回显“缺少用户名,可能是您的电子邮件地址”,
回显“并保存在$ {0}或使用-u标志” , 如果[“ $ CFRECORD_NAME” =“”],则
退出2
fi
。然后
回显“缺少主机名,您要更新哪个主机?”
回声“保存在$ {0}或使用-h标志”
出口2
网络连接

如果主机名不是FQDN

如果[[“ $ CFRECORD_NAME”!=“ $ CFZONE_NAME”] &&![-z“ $ {CFRECORD_NAME ## * $ CFZONE_NAME}”];然后
CFRECORD_NAME =“ $ CFRECORD_NAME。$ CFZONE_NAME”
echo“ =>主机名不是FQDN,假设$ CFRECORD_NAME”
fi

获取当前和旧的WAN IP

WAN_IP = curl -s ${WANIPSITE}
如果[-f $ HOME / .wan_ip-cf.txt]; 然后
OLD_WAN_IP = cat $HOME/.wan_ip-cf.txt
否则
回显“无文件,需要IP”
OLD_WAN_IP =“”
fi

如果WAN IP不变,则不是-f标志,请在此处退出

如果[“ $ WAN_IP” =“ $ OLD_WAN_IP”] && [“ $ FORCE” = false]; 然后
回显“ WAN IP未更改,无论如何都要使用标志-f true进行更新”,
退出0
fi

获取zone_identifier和record_identifier

ID_FILE = $ HOME / .ID-cf.txt
如果[$ -f ID_FILE] && [$(WC -l $ ID_FILE |切-d “” -f 1)== 4]
&& [“$(SED -N' 3,1p'“ $ ID_FILE”)“ ==” $ CFZONE_NAME“]
&& [” $(sed -n'4,1p'“ $ ID_FILE”)“ ==” $ CFRECORD_NAME“]; 然后
CFZONE_ID = $(sed -n'1,1p'“ $ ID_FILE”)
CFRECORD_ID = $(sed -n'2,1p'“ $ ID_FILE”)
否则
回显“更新zone_identifier和record_identifier”
CFZONE_ID = $(curl -s -X GET“ https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME ” -H“ X-Auth-Email:$ CFUSER” -H“ X-Auth-Key:$ CFKEY”- H“内容类型:application / json” | grep -Po' (?<=“ id”:“)[^”] '| 头-1) CFRECORD_ID = $(curl -s -X GET“https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME “ -H” X-Auth-Email:$ CFUSER“ -H” X-Auth-Key:$ CFKEY“ -H“内容类型:应用程序/ json” | grep -Po'(?<=“ id”:“)[^”] ')
回声“ $ CFZONE_ID”> $ ID_FILE
回声“ $ CFRECORD_ID” >> $ ID_FILE
回声“ $ CFZONE_NAME” >> $ ID_FILE
回显“ $ CFRECORD_NAME” >> $ ID_FILE
fi

如果更改了WAN,请更新cloudflare

回显“将DNS更新为$ WAN_IP”
RESPONSE = $(curl -s -X PUT“ https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID
-H“ X-Auth-Email:$ CFUSER”
-H“ X-Auth-Key:$ CFKEY“
-H”内容类型:application / json“
--data” {“ id”:“ $ CFZONE_ID”,“ type”:“ $ CFRECORD_TYPE”,“名称”:“ $ CFRECORD_NAME “,” content“:” $ WAN_IP“,” ttl“:$ CFTTL}”)
if [“ $ RESPONSE”!=“” $ {RESPONSE%success *}“] && [” $(echo $ RESPONSE | grep“” success“:true”)“!=”“]; 然后
回显“更新成功!”
回声$ WAN_IP> $ HOME / .wan_ip-cf.txt
退出
其他
回声'出事了:('
回声“回应:$ RESPONSE”
1个出口
科幻
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

这个能发一份给我么 ssvt60@163.com

看我提交的PR,合并一下就好了

你好 下载了你的 可以用 但是

提示是这样子

No file, need IP
Updating zone_identifier & record_identifier
Updating DNS to x.x.x.x
Updated succesfuly!

@ywly
Copy link
Author

ywly commented May 18, 2020

`#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

Automatically update your CloudFlare DNS record to the IP, Dynamic DNS

Can retrieve cloudflare Domain id and list zone's, because, lazy

Place at:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh

run crontab -e and add next line:

*/1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1

or you need log:

*/1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

Usage:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \ # fqdn of the record you want to update

-z example.com \ # will show you all zones if forgot, but you need this

Optional flags:

-f false|true \ # force dns update, disregard local stored ip

default config

API key, see https://www.cloudflare.com/a/account/my-account,

incorrect api-key results in E_UNAUTH error

CFKEY=xxxxx

Username, eg: malei@example.com

CFUSER=xxxxx

Zone name, eg: github.com

CFZONE_NAME=xxxx

Hostname to update, eg: www.github.com

CFRECORD_NAME=xxxxx

Record type, A(IPv4)|AAAA(IPv6), default IPv4

CFRECORD_TYPE=A

Cloudflare TTL for record, between 120 and 86400 seconds

CFTTL=120

Ignore local file, update ip anyway

FORCE=false

Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ...

WANIPSITE="http://icanhazip.com"

get parameter

while getopts k:u:h:z:f: opts; do
case ${opts} in
k) CFKEY=${OPTARG} ;;
u) CFUSER=${OPTARG} ;;
h) CFRECORD_NAME=${OPTARG} ;;
z) CFZONE_NAME=${OPTARG} ;;
f) FORCE=${OPTARG} ;;
esac
done

If required settings are missing just exit

if [ "$CFKEY" = "" ]; then
echo "Missing api-key, get at: https://www.cloudflare.com/a/account/my-account"
echo "and save in ${0} or using the -k flag"
exit 2
fi
if [ "$CFUSER" = "" ]; then
echo "Missing username, probably your email-address"
echo "and save in ${0} or using the -u flag"
exit 2
fi
if [ "$CFRECORD_NAME" = "" ]; then
echo "Missing hostname, what host do you want to update?"
echo "save in ${0} or using the -h flag"
exit 2
fi

If the hostname is not a FQDN

if [ "$CFRECORD_NAME" != "$CFZONE_NAME" ] && ! [ -z "${CFRECORD_NAME##*$CFZONE_NAME}" ]; then
CFRECORD_NAME="$CFRECORD_NAME.$CFZONE_NAME"
echo " => Hostname is not a FQDN, assuming $CFRECORD_NAME"
fi

Get current and old WAN ip

WAN_IP=curl -s ${WANIPSITE}
if [ -f $HOME/.wan_ip-cf.txt ]; then
OLD_WAN_IP=cat $HOME/.wan_ip-cf.txt
else
echo "No file, need IP"
OLD_WAN_IP=""
fi

If WAN IP is unchanged an not -f flag, exit here

if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then
echo "WAN IP Unchanged, to update anyway use flag -f true"
exit 0
fi

Get zone_identifier & record_identifier

ID_FILE=$HOME/.id-cf.txt
if [ -f $ID_FILE ] &amp;&amp; [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ]
&& [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ]
&& [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then
CFZONE_ID=$(sed -n '1,1p' "$ID_FILE")
CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE")
else
echo "Updating zone_identifier & record_identifier"
CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]' | head -1 ) CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id": ")[^"]')
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE
echo "$CFRECORD_NAME" >> $ID_FILE
fi

If WAN is changed, update cloudflare

echo "Updating DNS to $WAN_IP"
RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID"
-H "X-Auth-Email: $CFUSER"
-H "X-Auth-Key: $CFKEY"
-H "Content-Type: application/json"
--data "{"id":"$CFZONE_ID","type":"$CFRECORD_TYPE","name":"$CFRECORD_NAME","content":"$WAN_IP", "ttl":$CFTTL}")
if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ "$(echo $RESPONSE | grep ""success": true")" != "" ]; then
echo "Updated succesfuly!"
echo $WAN_IP > $HOME/.wan_ip-cf.txt
exit
else
echo 'Something went wrong :('
echo "Response: $RESPONSE"
exit 1
fi
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

也发一份给我吧 谢谢
yilin101#qq.com

已发送

@ywly
Copy link
Author

ywly commented May 18, 2020

/ usr / bin / env bash

设置-o errexit
设置-o名词
设置-o pipefail

自动将您的CloudFlare DNS记录更新为IP,动态DNS

可以检索cloudflare域ID和列表区域,因为,

放置在:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod + x / usr / local / bin / cf-ddns.sh

运行crontab -e并添加下一行:

* / 1 * * * * /usr/local/bin/cf-ddns.sh> / dev / null 2>&1

或者您需要日志:

* / 1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

用法:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \#要更新的记录的fqdn

-z example.com \#如果忘记了,将显示所有区域,但是您需要

可选标志:

-f false | true \#强制dns更新,忽略本地存储的ip

默认配置

API密钥,请参见https://www.cloudflare.com/a/account/my-account,

错误的api键会导致E_UNAUTH错误

CFKEY = xxxxx

用户名,例如:malei@example.com

CFUSER = xxxxx

区域名称,例如:github.com

CFZONE_NAME = xxxx

要更新的主机名,例如:www.github.com

CFRECORD_NAME = xxxxx

记录类型,A(IPv4)| AAAA(IPv6),默认IPv4

CFRECORD_TYPE = A

Cloudflare TTL记录,介于120到86400秒之间

CFTTL = 120

忽略本地文件,仍然更新IP

FORCE =假

用于检索WAN ip的站点,其他示例包括:bot.whatismyipaddress.com,https://api.ipify.org/ ...

WANIPSITE =“ http://icanhazip.com

获取参数

而getopts k:u:h:z:f:opts;

k)CFKEY = $ {OPTARG}中做$ {opts}的情况;
u)CFUSER = $ {OPTARG} ;;
h)CFRECORD_NAME = $ {OPTARG} ;;
z)CFZONE_NAME = $ {OPTARG} ;;
f)FORCE = $ {OPTARG} ;;
esac
完成

如果缺少所需的设置,请退出

如果[“ $ CFKEY” =“”]; 然后
回声“缺少API密钥,获得在:https://www.cloudflare.com/a/account/my-account
回声“并保存在$ {0}或使用-k标志”
2号出口
科幻
如果[ “ $ CFUSER” =“”]; 然后
回显“缺少用户名,可能是您的电子邮件地址”,
回显“并保存在$ {0}或使用-u标志” , 如果[“ $ CFRECORD_NAME” =“”],则
退出2
fi
。然后
回显“缺少主机名,您要更新哪个主机?”
回声“保存在$ {0}或使用-h标志”
出口2
网络连接

如果主机名不是FQDN

如果[[“ $ CFRECORD_NAME”!=“ $ CFZONE_NAME”] &&![-z“ $ {CFRECORD_NAME ## * $ CFZONE_NAME}”];然后
CFRECORD_NAME =“ $ CFRECORD_NAME。$ CFZONE_NAME”
echo“ =>主机名不是FQDN,假设$ CFRECORD_NAME”
fi

获取当前和旧的WAN IP

WAN_IP = curl -s ${WANIPSITE}
如果[-f $ HOME / .wan_ip-cf.txt]; 然后
OLD_WAN_IP = cat $HOME/.wan_ip-cf.txt
否则
回显“无文件,需要IP”
OLD_WAN_IP =“”
fi

如果WAN IP不变,则不是-f标志,请在此处退出

如果[“ $ WAN_IP” =“ $ OLD_WAN_IP”] && [“ $ FORCE” = false]; 然后
回显“ WAN IP未更改,无论如何都要使用标志-f true进行更新”,
退出0
fi

获取zone_identifier和record_identifier

ID_FILE = $ HOME / .ID-cf.txt
如果[$ -f ID_FILE] && [$(WC -l $ ID_FILE |切-d “” -f 1)== 4]
&& [“$(SED -N' 3,1p'“ $ ID_FILE”)“ ==” $ CFZONE_NAME“]
&& [” $(sed -n'4,1p'“ $ ID_FILE”)“ ==” $ CFRECORD_NAME“]; 然后
CFZONE_ID = $(sed -n'1,1p'“ $ ID_FILE”)
CFRECORD_ID = $(sed -n'2,1p'“ $ ID_FILE”)
否则
回显“更新zone_identifier和record_identifier”
CFZONE_ID = $(curl -s -X GET“ https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME ” -H“ X-Auth-Email:$ CFUSER” -H“ X-Auth-Key:$ CFKEY”- H“内容类型:application / json” | grep -Po' (?<=“ id”:“)[^”] '| 头-1) CFRECORD_ID = $(curl -s -X GET“https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME “ -H” X-Auth-Email:$ CFUSER“ -H” X-Auth-Key:$ CFKEY“ -H“内容类型:应用程序/ json” | grep -Po'(?<=“ id”:“)[^”] ')
回声“ $ CFZONE_ID”> $ ID_FILE
回声“ $ CFRECORD_ID” >> $ ID_FILE
回声“ $ CFZONE_NAME” >> $ ID_FILE
回显“ $ CFRECORD_NAME” >> $ ID_FILE
fi

如果更改了WAN,请更新cloudflare

回显“将DNS更新为$ WAN_IP”
RESPONSE = $(curl -s -X PUT“ https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID
-H“ X-Auth-Email:$ CFUSER”
-H“ X-Auth-Key:$ CFKEY“
-H”内容类型:application / json“
--data” {“ id”:“ $ CFZONE_ID”,“ type”:“ $ CFRECORD_TYPE”,“名称”:“ $ CFRECORD_NAME “,” content“:” $ WAN_IP“,” ttl“:$ CFTTL}”)
if [“ $ RESPONSE”!=“” $ {RESPONSE%success *}“] && [” $(echo $ RESPONSE | grep“” success“:true”)“!=”“]; 然后
回显“更新成功!”
回声$ WAN_IP> $ HOME / .wan_ip-cf.txt
退出
其他
回声'出事了:('
回声“回应:$ RESPONSE”
1个出口
科幻
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

这个能发一份给我么 ssvt60@163.com

已发送

@eicky
Copy link

eicky commented May 18, 2020

/ usr / bin / env bash

设置-o errexit
设置-o名词
设置-o pipefail

自动将您的CloudFlare DNS记录更新为IP,动态DNS

可以检索cloudflare域ID和列表区域,因为,

放置在:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod + x / usr / local / bin / cf-ddns.sh

运行crontab -e并添加下一行:

* / 1 * * * * /usr/local/bin/cf-ddns.sh> / dev / null 2>&1

或者您需要日志:

* / 1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

用法:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \#要更新的记录的fqdn

-z example.com \#如果忘记了,将显示所有区域,但是您需要

可选标志:

-f false | true \#强制dns更新,忽略本地存储的ip

默认配置

API密钥,请参见https://www.cloudflare.com/a/account/my-account,

错误的api键会导致E_UNAUTH错误

CFKEY = xxxxx

用户名,例如:malei@example.com

CFUSER = xxxxx

区域名称,例如:github.com

CFZONE_NAME = xxxx

要更新的主机名,例如:www.github.com

CFRECORD_NAME = xxxxx

记录类型,A(IPv4)| AAAA(IPv6),默认IPv4

CFRECORD_TYPE = A

Cloudflare TTL记录,介于120到86400秒之间

CFTTL = 120

忽略本地文件,仍然更新IP

FORCE =假

用于检索WAN ip的站点,其他示例包括:bot.whatismyipaddress.com,https://api.ipify.org/ ...

WANIPSITE =“ http://icanhazip.com

获取参数

而getopts k:u:h:z:f:opts;

k)CFKEY = $ {OPTARG}中做$ {opts}的情况;
u)CFUSER = $ {OPTARG} ;;
h)CFRECORD_NAME = $ {OPTARG} ;;
z)CFZONE_NAME = $ {OPTARG} ;;
f)FORCE = $ {OPTARG} ;;
esac
完成

如果缺少所需的设置,请退出

如果[“ $ CFKEY” =“”]; 然后
回声“缺少API密钥,获得在:https://www.cloudflare.com/a/account/my-account
回声“并保存在$ {0}或使用-k标志”
2号出口
科幻
如果[ “ $ CFUSER” =“”]; 然后
回显“缺少用户名,可能是您的电子邮件地址”,
回显“并保存在$ {0}或使用-u标志” , 如果[“ $ CFRECORD_NAME” =“”],则
退出2
fi
。然后
回显“缺少主机名,您要更新哪个主机?”
回声“保存在$ {0}或使用-h标志”
出口2
网络连接

如果主机名不是FQDN

如果[[“ $ CFRECORD_NAME”!=“ $ CFZONE_NAME”] &&![-z“ $ {CFRECORD_NAME ## * $ CFZONE_NAME}”];然后
CFRECORD_NAME =“ $ CFRECORD_NAME。$ CFZONE_NAME”
echo“ =>主机名不是FQDN,假设$ CFRECORD_NAME”
fi

获取当前和旧的WAN IP

WAN_IP = curl -s ${WANIPSITE}
如果[-f $ HOME / .wan_ip-cf.txt]; 然后
OLD_WAN_IP = cat $HOME/.wan_ip-cf.txt
否则
回显“无文件,需要IP”
OLD_WAN_IP =“”
fi

如果WAN IP不变,则不是-f标志,请在此处退出

如果[“ $ WAN_IP” =“ $ OLD_WAN_IP”] && [“ $ FORCE” = false]; 然后
回显“ WAN IP未更改,无论如何都要使用标志-f true进行更新”,
退出0
fi

获取zone_identifier和record_identifier

ID_FILE = $ HOME / .ID-cf.txt
如果[$ -f ID_FILE] && [$(WC -l $ ID_FILE |切-d “” -f 1)== 4]
&& [“$(SED -N' 3,1p'“ $ ID_FILE”)“ ==” $ CFZONE_NAME“]
&& [” $(sed -n'4,1p'“ $ ID_FILE”)“ ==” $ CFRECORD_NAME“]; 然后
CFZONE_ID = $(sed -n'1,1p'“ $ ID_FILE”)
CFRECORD_ID = $(sed -n'2,1p'“ $ ID_FILE”)
否则
回显“更新zone_identifier和record_identifier”
CFZONE_ID = $(curl -s -X GET“ https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME ” -H“ X-Auth-Email:$ CFUSER” -H“ X-Auth-Key:$ CFKEY”- H“内容类型:application / json” | grep -Po' (?<=“ id”:“)[^”] '| 头-1) CFRECORD_ID = $(curl -s -X GET“https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME “ -H” X-Auth-Email:$ CFUSER“ -H” X-Auth-Key:$ CFKEY“ -H“内容类型:应用程序/ json” | grep -Po'(?<=“ id”:“)[^”] ')
回声“ $ CFZONE_ID”> $ ID_FILE
回声“ $ CFRECORD_ID” >> $ ID_FILE
回声“ $ CFZONE_NAME” >> $ ID_FILE
回显“ $ CFRECORD_NAME” >> $ ID_FILE
fi

如果更改了WAN,请更新cloudflare

回显“将DNS更新为$ WAN_IP”
RESPONSE = $(curl -s -X PUT“ https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID
-H“ X-Auth-Email:$ CFUSER”
-H“ X-Auth-Key:$ CFKEY“
-H”内容类型:application / json“
--data” {“ id”:“ $ CFZONE_ID”,“ type”:“ $ CFRECORD_TYPE”,“名称”:“ $ CFRECORD_NAME “,” content“:” $ WAN_IP“,” ttl“:$ CFTTL}”)
if [“ $ RESPONSE”!=“” $ {RESPONSE%success *}“] && [” $(echo $ RESPONSE | grep“” success“:true”)“!=”“]; 然后
回显“更新成功!”
回声$ WAN_IP> $ HOME / .wan_ip-cf.txt
退出
其他
回声'出事了:('
回声“回应:$ RESPONSE”
1个出口
科幻
`
这是我自己修改版,可以用,等作者大佬修复继续用大佬的

这个能发一份给我么 ssvt60@163.com

看我提交的PR,合并一下就好了

你好 下载了你的 可以用 但是

提示是这样子

No file, need IP
Updating zone_identifier & record_identifier
Updating DNS to x.x.x.x
Updated succesfuly!

第一次本来就没有文件啊,你后面运行就不会这样了,你看一下作者源码就知道了

yulewang added a commit that referenced this issue May 18, 2020
to2false pushed a commit to to2false/cloudflare-api-ddns that referenced this issue Nov 21, 2021
to2false pushed a commit to to2false/cloudflare-api-ddns that referenced this issue Nov 21, 2021
@Noa886
Copy link

Noa886 commented Feb 25, 2023

`#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail

Automatically update your CloudFlare DNS record to the IP, Dynamic DNS

Can retrieve cloudflare Domain id and list zone's, because, lazy

Place at:

curl https://raw.githubusercontent.com/MaleiSIyin/cfv4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh

run crontab -e and add next line:

*/1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1

or you need log:

*/1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1

Usage:

cf-ddns.sh -k cloudflare-api-key \

-u user@example.com \

-h host.example.com \ # fqdn of the record you want to update

-z example.com \ # will show you all zones if forgot, but you need this

Optional flags:

-f false|true \ # force dns update, disregard local stored ip

default config

API key, see https://www.cloudflare.com/a/account/my-account,

incorrect api-key results in E_UNAUTH error

CFKEY=xxxxx

Username, eg: malei@example.com

CFUSER=xxxxx

Zone name, eg: github.com

CFZONE_NAME=xxxx

Hostname to update, eg: www.github.com

CFRECORD_NAME=xxxxx

Record type, A(IPv4)|AAAA(IPv6), default IPv4

CFRECORD_TYPE=A

Cloudflare TTL for record, between 120 and 86400 seconds

CFTTL=120

Ignore local file, update ip anyway

FORCE=false

Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ...

WANIPSITE="http://icanhazip.com"

get parameter

while getopts k:u:h:z:f: opts; do case ${opts} in k) CFKEY=${OPTARG} ;; u) CFUSER=${OPTARG} ;; h) CFRECORD_NAME=${OPTARG} ;; z) CFZONE_NAME=${OPTARG} ;; f) FORCE=${OPTARG} ;; esac done

If required settings are missing just exit

if [ "$CFKEY" = "" ]; then echo "Missing api-key, get at: https://www.cloudflare.com/a/account/my-account" echo "and save in ${0} or using the -k flag" exit 2 fi if [ "$CFUSER" = "" ]; then echo "Missing username, probably your email-address" echo "and save in ${0} or using the -u flag" exit 2 fi if [ "$CFRECORD_NAME" = "" ]; then echo "Missing hostname, what host do you want to update?" echo "save in ${0} or using the -h flag" exit 2 fi

If the hostname is not a FQDN

if [ "$CFRECORD_NAME" != "$CFZONE_NAME" ] && ! [ -z "${CFRECORD_NAME##*$CFZONE_NAME}" ]; then CFRECORD_NAME="$CFRECORD_NAME.$CFZONE_NAME" echo " => Hostname is not a FQDN, assuming $CFRECORD_NAME" fi

Get current and old WAN ip

WAN_IP=curl -s ${WANIPSITE} if [ -f $HOME/.wan_ip-cf.txt ]; then OLD_WAN_IP=cat $HOME/.wan_ip-cf.txt else echo "No file, need IP" OLD_WAN_IP="" fi

If WAN IP is unchanged an not -f flag, exit here

if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then echo "WAN IP Unchanged, to update anyway use flag -f true" exit 0 fi

Get zone_identifier & record_identifier

ID_FILE=$HOME/.id-cf.txt if [ -f Misplaced & $ID_FILE ] &amp;&amp; [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ] &amp;&amp; [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ] && [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then CFZONE_ID=$(sed -n '1,1p' "$ID_FILE") CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE") else echo "Updating zone_identifier & record_identifier" CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]' | head -1 ) CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id": ")[^"]') echo "$CFZONE_ID" > $ID_FILE echo "$CFRECORD_ID" >> $ID_FILE echo "$CFZONE_NAME" >> $ID_FILE echo "$CFRECORD_NAME" >> $ID_FILE fi

If WAN is changed, update cloudflare

echo "Updating DNS to $WAN_IP"

RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" --data "{"id":"$CFZONE_ID","type":"$CFRECORD_TYPE","name":"$CFRECORD_NAME","content":"$WAN_IP", "ttl":$CFTTL}")

if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ "$(echo $RESPONSE | grep ""success": true")" != "" ]; then echo "Updated succesfuly!" echo $WAN_IP > $HOME/.wan_ip-cf.txt exit else echo 'Something went wrong :(' echo "Response: $RESPONSE" exit 1 fi ` 这是我自己修改版,可以用,等作者大佬修复继续用大佬的

发一份给我吧 谢谢 33736675@qq.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants