手写 DDNS 实现,爽到!

0x00.前言

因为电信宽带提供公网ip地址,wz打了个电话就申请到了,然而每次获取到的公网ip并不是固定的,感觉极低的概率能获取到上次的拨号的地址
又需要访问家里的一些服务,开始时是干脆记住ip地址,后来越来越懒了决定干脆用域名的A记录解析好了,于是就有了本文的内容

0x01.代码

核心接口
核心接口

CloudFlare_DDNS.py

1
#!/usr/bin/env/ python3
2
# -*- coding: utf-8 -*-
3
"""
4
    :Author: yuangezhizao
5
    :Time: 2019/7/18 0018 19:40
6
    :Site: https://www.yuangezhizao.cn
7
    :Copyright: © 2019 yuangezhizao <root@yuangezhizao.cn>
8
"""
9
import time
10
11
import requests
12
13
headers = {
14
    'X-Auth-Email': '<rm>',
15
    'X-Auth-Key': '<rm>',
16
    'Content-Type': 'application/json',
17
}
18
19
20
def update_dns_record():
21
    ip = requests.get('https://lab.yuangezhizao.cn/ip').json()['IP']
22
23
    data = '{"type":"A","name":"home.yuangezhizao.cn","content":"' + ip + '"}'
24
25
    response = requests.put(
26
        'https://api.cloudflare.com/client/v4/zones/<zone_identifier>/dns_records/<identifier>/',
27
        headers=headers, data=data)
28
29
    print(response.json())
30
31
32
try:
33
    update_dns_record()
34
except Exception as e:
35
    print(e)

zone_identifieridentifier不方便确定的话,可以F12来下官方页面,那个页面走的接口数据也是一样的

0x02.定时运行

Windows计划任务请!群辉大法好!
套件中心里可以安装Python3,再开启SSH服务,连进去之后安装pip3就可以下载requests包了hhh

Python3
Python3
任务计划
任务计划
常规
常规
计划
计划
任务设置
任务设置
未安装包
未安装包

安装pip3,首先下载或者wget https://bootstrap.pypa.io/get-pip.py扔到自己的NAS分区中,然后sudo python3 get-pip.py
会有大概率ReadTimeoutError,多次重试之后终于安装成功……

一片红
一片红
1
yuangezhizao@NAS:/volume1/ygzz$ sudo python3 get-pip.py 
2
Password: 
3
Collecting pip
4
  Downloading https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl (1.4MB)
5
     |████████████████████████████████| 1.4MB 8.2kB/s 
6
Collecting setuptools
7
  Downloading https://files.pythonhosted.org/packages/b2/86/095d2f7829badc207c893dd4ac767e871f6cd547145df797ea26baea4e2e/setuptools-41.2.0-py2.py3-none-any.whl (576kB)
8
     |████████████████████████████████| 583kB 9.5kB/s 
9
Collecting wheel
10
  Downloading https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl
11
Installing collected packages: pip, setuptools, wheel
12
  WARNING: The script wheel is installed in '/volume1/@appstore/py3k/usr/local/bin' which is not on PATH.
13
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
14
Successfully installed pip-19.2.2 setuptools-41.2.0 wheel-0.33.6

安上了之后赶紧配置镜像源(如下是一顿骚操作,可见注释

1
yuangezhizao@NAS:/volume1/ygzz$ sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
2
Password: 
3
sudo: pip3: command not found # 没在 PATH 里诶
4
yuangezhizao@NAS:/volume1/ygzz$ sudo cat /etc/profile
5
#/etc/profile: system-wide .profile file for ash.
6
7
umask 022
8
9
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin
10
export PATH
11
12
PGDATA=/var/services/pgsql
13
export PGDATA
14
15
TERMINFO=/usr/share/terminfo
16
export TERMINFO
17
18
TERM=${TERM:-cons25}
19
export TERM
20
21
PAGER=more
22
export PAGER
23
24
export LC_ALL=en_US.utf8
25
export LANG=en_US.utf8
26
27
PS1="`hostname`> "
28
29
alias dir="ls -al"
30
alias ll="ls -la"
31
32
ulimit -c unlimited
33
34
if [ -f /etc.defaults/.bashrc_profile ]; then
35
	source /etc.defaults/.bashrc_profile
36
fi
37
yuangezhizao@NAS:/volume1/ygzz$ sudo vim /etc/profile # 永久生效法
38
yuangezhizao@NAS:/volume1/ygzz$ sudo source /etc/profile
39
sudo: source: command not found  # 永久生效不能?
40
yuangezhizao@NAS:/volume1/ygzz$ echo $PATH 
41
/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin
42
yuangezhizao@NAS:/volume1/ygzz$ export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin:/volume1/@appstore/py3k/usr/local/bin
43
yuangezhizao@NAS:/volume1/ygzz$ echo $PATH # 那就临时生效吧……
44
/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin:/volume1/@appstore/py3k/usr/local/bin
45
yuangezhizao@NAS:/volume1/ygzz$ sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U # 最新 ok
46
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
47
Requirement already up-to-date: pip in /volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages (19.2.2)
48
yuangezhizao@NAS:/volume1/ygzz$ sudo pip3 install requests # 终于可以安装了
49
Collecting requests
50
  Downloading https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB)
51
     |████████████████████████████████| 61kB 18kB/s 
52
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests)
53
  Downloading https://files.pythonhosted.org/packages/e6/60/247f23a7121ae632d62811ba7f273d0e58972d75e58a94d329d51550a47d/urllib3-1.25.3-py2.py3-none-any.whl (150kB)
54
     |████████████████████████████████| 153kB 14kB/s 
55
Collecting idna<2.9,>=2.5 (from requests)
56
  Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
57
     |████████████████████████████████| 61kB 13kB/s 
58
Collecting chardet<3.1.0,>=3.0.2 (from requests)
59
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
60
     |████████████████████████████████| 143kB 13kB/s 
61
Collecting certifi>=2017.4.17 (from requests)
62
  Downloading https://files.pythonhosted.org/packages/69/1b/b853c7a9d4f6a6d00749e94eb6f3a041e342a885b87340b79c1ef73e3a78/certifi-2019.6.16-py2.py3-none-any.whl (157kB)
63
     |████████████████████████████████| 163kB 12kB/s 
64
Installing collected packages: urllib3, idna, chardet, certifi, requests
65
Successfully installed certifi-2019.6.16 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.3
运行成功
运行成功

0x03.引用

基于 Cloudflare DNS API 部署 IPv6 DDNS

未完待续……