Skip to content

Instantly share code, notes, and snippets.

@HouCoder
Last active June 9, 2021 22:25
Show Gist options
  • Save HouCoder/bd9b5b1542e5baa5b75be35b5e80a3fd to your computer and use it in GitHub Desktop.
Save HouCoder/bd9b5b1542e5baa5b75be35b5e80a3fd to your computer and use it in GitHub Desktop.
Godaddy DNS record updater.
#!/bin/bash
# https://developer.godaddy.com/keys
KEY=""
SECRET=""
domain="example.com"
name="a-record"
# Get public IP from ipinfo.io
public_ip="$(curl --silent ipinfo.io/ip)"
# get current record
current_record="$(dig @8.8.8.8 +short $name.$domain)"
if [ "$current_record" != "$public_ip" ]; then
echo New public IP is $public_ip
# Update godaddy A record - https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceTypeName
curl -X PUT "https://api.godaddy.com/v1/domains/$domain/records/A/$name" \
-H "Authorization: sso-key $KEY:$SECRET" \
-H 'Content-Type: application/json' \
--data '[{"type": "A", "name": "'"$name"'", "data": "'"$public_ip"'", "ttl": 3600}]'
else
echo Nothing changed
fi
@HouCoder
Copy link
Author

@unit2nd

脚本内容:获取本机的IP地址,并将其上传至Godaddy的相关域名。

应该是获取本机的 IP 地址,与 DNS 服务器上 A 记录指向的地址做对比,不一致的话更新 godaddy 的相关记录。

其实很简单,适用于路由器不支持 DDNS 的网络使用。

@unit2nd
Copy link

unit2nd commented Jan 27, 2019

我已经在使用你的这个脚本了,刚好可以解决动态IP的问题。
然后执行命令后有个提示说,第13行 dig:未找到命令

虽然没啥影响,但是强迫症看到提示,就想解决这个问题,请问这是什么原因导致的?

来自新手的疑问。

@HouCoder
Copy link
Author

哦,你需要安装 dig 这个工具,ubuntu 的话应该是 $ sudo apt-get install dnsutils 就可以了。

@unit2nd

@onefen123
Copy link

onefen123 commented Feb 20, 2019

我用的是centos7提示:line 22: syntax error near unexpected token `fi'
并且我打印echo $current_record打印不出值,有没联系方式教我一下我的q3006139

@jy02239285
Copy link

路由器运行sh
错误提示
[Router /home/id]# ./godaddy_ddns.sh
New public IP is 100.11.xx.xx
curl: (51) Cert verify failed: BADCERT_NOT_TRUSTED
如果我使用跳过
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
则提示

syntax error: unexpected word (expecting ")")

头疼,暂时还没看到godaddy更新ip.

@HouCoder
Copy link
Author

HouCoder commented Jan 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment