动态IP解析免费域名托管到Cloudflare和GcoreDNS免费解析G-core动态DNS接口API


动态IP解析免费域名托管到Cloudflare和GcoreDNS免费解析G-core动态DNS接口API

B站视频:https://www.bilibili.com/video/BV1K14y1J7EA/
youtube视频:https://youtu.be/cZfN4af_LGg
博客:https://www.wanuse.com/2022/12/ipcloudflaregcorednsg-corednsapi.html
本记录用到的工具和地址:
FinalShell SSH工具,服务器管理,远程桌面加速软件Windows版下载地址
https://www.cloudflare.com/zh-cn/
https://auth.gcore.com/login/signin?lang=en



先登录gcore的账号点击dns

如果没有账号可以点击 这个链接注册,要使用邮箱注册

https://auth.gcore.com/login/signup?lang=en

不要使用Google账号和Github账号登录,没有密码的账号是不能使用API接口的

gcore-dns动态ip解析api.png
gcore-dns动态ip解析api.png (151.73 KiB) 


复制gcore两个域名dns服务器地址
ns1.gcorelabs.net
ns2.gcdn.services

gcore-dns动态ip解析api2.png
gcore-dns动态ip解析api2.png (123.17 KiB) 

打开免费域名管理添加两个服务器地址

(不知道管理页在哪 请查看免费域名托管到Cloudflare DNS解析域名解析更快更高效
B站视频:https://www.bilibili.com/video/BV1AP4y197Ax
youtube视频:https://youtu.be/2irKP1vBS4M
博客:https://www.wanuse.com/2022/11/cloudflare-dns.html)
gcore-dns动态ip解析api3.png
gcore-dns动态ip解析api3.png (150.95 KiB) 

回到gcore dns管理页点击添加域名

gcore-dns动态ip解析api4 拷贝.png
gcore-dns动态ip解析api4 拷贝.png (76.88 KiB) 


在输入框填入域名点击继续


[/code]
gcore-dns动态ip解析api5.png
gcore-dns动态ip解析api5.png (38.13 KiB) 

扫描出现在已有的dns解析记录点击继续。
之前没有做过解析的域名是没有dns记录的。

gcore-dns动态ip解析api6.png
gcore-dns动态ip解析api6.png (146.87 KiB) 

下面这一步添加域名dns服务器之前已经完成,
点击继续完成域名托管(等待域名服务响应)

gcore-dns动态ip解析api7.png
gcore-dns动态ip解析api7.png (212.21 KiB)

回到刚登录时候的页面,点击账户头像去创建API接口密钥

gcore-dns动态ip解析api8.png
gcore-dns动态ip解析api8.png (62.62 KiB) 

按照下图点击创建密钥

gcore-dns动态ip解析api9.png
gcore-dns动态ip解析api9.png (230.96 KiB) 

密钥名随意填写 下一项选择管理员admini...

gcore-dns动态ip解析api10.png
gcore-dns动态ip解析api10.png (113.73 KiB) 
下图填写完成后点创建

gcore-dns动态ip解析api11.png
gcore-dns动态ip解析api11.png (142.21 KiB) 

点创建后弹出密钥一定要复制保存好,密钥只出现一次。

gcore-dns动态ip解析api12.png
gcore-dns动态ip解析api12.png (118.52 KiB) 

回到DNS》域名管理页添加一条解析记录用于动态ip解析

gcore-dns动态ip解析api14.png
gcore-dns动态ip解析api14.png (62.24 KiB) 


下面填写了一条A记录的解析
这样就可以使用API接口来更新这条记录

gcore-dns动态ip解析api13.png
gcore-dns动态ip解析api13.png (126.52 KiB) 

下面就是使用脚本自动更新ip地址
首先你的服务器(路由器,linux,windows等等需要有python)
这是一个python脚本,curl以后出脚本sh
把脚本上传到服务器

gcore-dns动态ip解析api15.png
gcore-dns动态ip解析api15.png (412.99 KiB) 

下面是脚本要修改成你自己的域名配置
可以回到上视频链接

gcore-dns动态ip解析api16.png
gcore-dns动态ip解析api16.png (298.37 KiB) 

复制下面代码保存成.py格式上传


DNS免费解析G-core动态DNS接口API测试代码 python片段

代码: 

#!/usr/bin/python
import requests
from requests.structures import CaseInsensitiveDict

#动态DNS提交链接
url = "https://api.gcorelabs.com/dns/v2/zones/openip.ml/ipv6.openip.ml/A"

headers = CaseInsensitiveDict()
headers["Authorization"] = "APIKey 3796$4be9dc5611ab95333fbbffefa4ec192fca5a2d73a11cc4b1645ae4a1b7e5af6935f7de1543fdf821519b6d4e0c36ebc1c3bdae9ebd91749c0a2f3a17df0c5d"
headers["Content-Type"] = "application/json"

data = '{ "resource_records": [{"content": ["2.2.2.2"]}]}'
resp = requests.put(url, headers=headers, data=data)

print(resp.json())


python 动态更新ipv6和ipv4地址完整版

代码: 

#!/usr/bin/python
import requests
from requests.structures import CaseInsensitiveDict
#获取公网ipv4
ip = requests.get('https://ipinfo.io/ip').text.strip()
#获取公网ipv6
#ip = requests.get('https://6.ipw.cn/').text.strip()
#读取历史ip
rip = open('/home/ddd/www_wanuse_com.txt','r')
jip = rip.read()
#动态DNS提交链接
url = "https://api.gcorelabs.com/dns/v2/zones/openip.ml/ipv4.openip.ml/A"

if ip==jip:
    print('IP未更新 '+ip)
else:
    print('IP已更新 '+ip)
    #打开存放ip的api.txt文件
    wip = open('www_wanuse_com.txt','w')
    wip.write(ip)
    wip.close()
    headers = CaseInsensitiveDict()
    headers["Authorization"] = "APIKey 3796$4be9dc5611ab95333fbbffefa4ec192fca52d73a11cc4b1645ae4a1b7e5af6935f407de1543fdf821519b6d4e0c36ebc1c3bdae9ebd91749c0a2f3a17df0c5d"
    headers["Content-Type"] = "application/json"
    data = '{ "resource_records": [{"content": ["'+ip+'"]}]}'
    resp = requests.put(url, headers=headers, data=data)
    print(resp.status_code)

下载地址: https://cowtransfer.com/s/3d147d4f32e84e
点击链接查看 [ dns1.py ] 输入传输口令 nx00st


设置定时计划
在终端 输入 vi /etc/crontab
按键 i 键进入编辑模式
在最下面插入一行
*/5 * * * * ddd python3 /home/ddd/dns1.py
然后按键盘ESC 输入:wq回车保存
输入service cron restart重启服务

gcore-dns动态ip解析api17.png
gcore-dns动态ip解析api17.png (169.09 KiB) 

输入service cron status 可以查看计划任务执行记录

gcore-dns动态ip解析api18.png
gcore-dns动态ip解析api18.png (229.2 KiB)