大神论坛

找回密码
快速注册
查看: 701 | 回复: 0

[交流讨论] 自编某主机商DDNS脚本

主题

帖子

15

积分

初入江湖

UID
18
积分
15
精华
威望
30 点
违规
大神币
68 枚
注册时间
2021-03-07 22:41
发表于 2021-03-08 22:18
本帖最后由 sliceso 于 2021-03-08 22:18 编辑

0x00 背景
    准备在家搭建一个服务器,动态公网IP正在申请中,由于要使用DDns对域名动态解析,但是我的域名提供商没有相关的服务,只能自己写个脚本
0x01 实现原理
   由于家里到时候是动态公网IP,IP会变化,这种情况就要修改域名提供商的解析地址:

我理解的ddns就是检测家里的外网IP,然后自动修改这个解析地址就成了,于是就诞生了这个脚本
0x02 脚本实现

  • 用户登陆:比较简单吧主要使用sessions()保存cookie方便后续的操作。
  • 获取解析记录:
    通过抓包可找到接口和参数格式
  • 获取本机IP
    通过curl ip.sb获得
  • 修改记录
    同样是抓包找到接口和参数格式
import requests
import os
import time

mysession = requests.session()
headers = {
'Host': 'api.zhujiwu.com',
'Content-Length': '46',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'http://www.zhujiwu.com',
'Referer': 'http://www.zhujiwu.com/',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'close'}


def login(username, pwd):
url = "http://www.zhujiwu.com/www/login.php"
data = {
"cmd": "login",
"username": username,
"password": pwd
}
try:
r = mysession.post(url=url, data=data, headers=headers)
if r.json()['response'] == '200':
print('主机屋账户:【' + username + '】登陆成功!')
print("当前Cookies:", mysession.cookies)
else:
print(r.json())
except Exception as e:
print(e)


def get_history(domain):
url = "http://www.zhujiwu.com/www/domain.php"
data = {
"cmd": "jiexi_query",
"count": "10",
"page": "1",
"domainname": domain,
"title": ""}
r = mysession.post(url=url, data=data, headers=headers)
if r.json()['response'] == "200":
dns_history = r.json()['data']['content']
print(domain)
for i in dns_history:
print(i["id"], i["set_type"], i["title"], i["content"])

return dns_history
else:
return


def get_realIP():
real_IP = os.popen('curl ip.sb').read()
print("本机IP:", real_IP)
return real_IP


def change_dns(domai_id, realIP, domainname):
url = "http://www.zhujiwu.com/www/domain.php"
realIP = realIP.replace("\n", "")
data = {
'cmd': 'jiexi_modify',
'ac_id': domai_id,
'mx': '',
'content': realIP,
'ttl': '120'
}
r = mysession.post(url=url, data=data, headers=headers)
if r.json()['response'] == "200":
print("修改【", domainname, "】解析到【", realIP, "】成功")
else:
print("修改【", domainname, "】解析到【", realIP, "】失败")


if __name__ == '__main__':
username = ''
pwd = ''
domain = ""
login(username, pwd)
while 1:
dns_history = get_history(domain)
realIP = get_realIP().replace("\n", "")
for i in dns_history:
if realIP != i["content"]:
print("检测IP变更,修改解析:", end=" ")
change_dns(i["id"], realIP, i["title"])

time.sleep(60)


返回顶部