樹莓派4B動(dòng)態(tài)解析公網(wǎng)IP
????????因?yàn)橐婚_始采用的是Frp內(nèi)網(wǎng)穿透技術(shù),走的阿里云服務(wù)器的通道,所以帶寬最大只支持1M,導(dǎo)致我本地服務(wù)器的流媒體服務(wù)訪問人數(shù)一多就會(huì)引起觀影卡頓,所以只能更換為DDNS技術(shù)來動(dòng)態(tài)解析本地的公網(wǎng)IP,共試了兩種方法,第一種方法我本地測(cè)試有點(diǎn)問題,后續(xù)換為了第二種方法,在此記錄。
一、自定義Python腳本 + 阿里云SDK 實(shí)現(xiàn)DDNS
安裝庫
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-alidns
pip install pyyaml新建目錄
cd /
cd urs
mkdir ddns新建Py腳本
sudo nano aliddns.pyimport os
import re
import time
import json
import platform
import requests
from settings import * # 所有需要修改的數(shù)據(jù)
from aliyunsdkcore.client import AcsClient
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
def LocalIPv6Address():
? ?# 判斷當(dāng)前操作系統(tǒng),使用命令
? ?if(platform.system() == 'Windows'):
? ? ? ?cmd = 'ipconfig'
? ?elif(platform.system() == 'Linux'):
? ? ? ?cmd = 'ifconfig'
? ?else:
? ? ? ?print("操作系統(tǒng)不兼容")
? ? ? ?exit()
? ?v6_pattern = re.compile(r"(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})")
? ?r = os.popen(cmd)
? ?text = r.read()
? ?r.close()
? ?v6_address = v6_pattern.findall(text)
? ?return v6_address[0][0]
def AliDNSUpdate(v6_address):
? ?client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')
? ?request = AddDomainRecordRequest()
? ?request.set_accept_format('json')
? ?request.set_Value(v6_address)
? ?request.set_Type("AAAA")
? ?request.set_RR(RR)
? ?request.set_DomainName(DomainName)
? ?try:
? ? ? ?response = client.do_action_with_exception(request)
? ? ? ?response = str(response, encoding='utf-8')
? ?except:
? ? ? ?response = '{"ERROR":"error"}'
? ?return response
# 如果更新失敗,返回False,否則返回True
def JudgeResponse(r):
? ?data = json.loads(r)
? ?if "ERROR" in data:
? ? ? ?return False
? ?return True
def WriteLog(msg, address):
? ?data = json.loads(msg)
? ?now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
? ?with open(LOGFILE, 'w+', encoding='utf-8') as f:
? ? ? ?f.write(now_time+'\t'+address+'\n')
? ? ? ?for key in data:
? ? ? ? ? ?value = data[key]
? ? ? ? ? ?line = '\t' + str(key) + '\t:\t' + str(value) + '\n'
? ? ? ? ? ?f.write(line)
? ? ? ?f.close()
if __name__ == '__main__':
? ?pre_address = ''
? ?while True:
? ? ? ?v6_address = LocalIPv6Address()
? ? ? ?if v6_address != pre_address:
? ? ? ? ? ?r = AliDNSUpdate(v6_address)
? ? ? ? ? ?WriteLog(r, v6_address)
? ? ? ? ? ?if not JudgeResponse(r):
? ? ? ? ? ? ? ?break
? ? ? ? ? ?pre_address = v6_address
? ? ? ?time.sleep(60)運(yùn)行腳本
python3 aliddns.py
二、開源工具Features實(shí)現(xiàn)DDNS
網(wǎng)址:https://ddns.newfuture.cc/
Git: https://github.com/NewFuture/DDNS/issues?page=2&q=is%3Aissue+is%3Aopen
安裝工具
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple ddns新建目錄
cd /
cd urs
mkdir ddns運(yùn)行
ddns
此時(shí)會(huì)默認(rèn)在此目錄下生成
config.json文件若想指定目錄生成需帶參執(zhí)行 例:
ddns -c path/to/config.json
修改配置文件
{
"$schema": "https://ddns.newfuture.cc/schema/v2.8.json",
"debug": false,
"dns": "alidns",
"id": "阿里云ID",
"index4": "public",
"index6": "default",
"ipv4": [
"域名"
],
"ipv6": [
"域名"
],
"proxy": null,
"token": "阿里云Secret"
"ttl": null
}執(zhí)行
ddns
此時(shí)將會(huì)獲取本地公網(wǎng)IP,然后解析域名
三、設(shè)置cron定時(shí)任務(wù)
查看任務(wù)列表
crontab -l
編輯/添加 任務(wù)
crontab -e
查看定時(shí)服務(wù)狀態(tài)
service cron status
啟動(dòng)定時(shí)服務(wù)
service cron start
重啟定時(shí)服務(wù)
service cron restart
// or
sudo /etc/init.d/cron restart重載定時(shí)配置
service cron reload
終止定時(shí)服務(wù)
service cron stop
定時(shí)任務(wù)規(guī)則
# 0 和 7 都代表星期日
0 - Sun Sunday
1 - Mon Monday
2 - Tue Tuesday
3 - Wed Wednesday
4 - Thu Thursday
5 - Fri Friday
6 - Sat Saturday
7 - Sun Sunday
# 總體格式
┌────────── minute (0 - 59)
│ ┌──────── hour (0 - 23)
│ │ ┌────── day of month (1 - 31)
│ │ │ ┌──── month (1 - 12)
│ │ │ │ ┌── day of week (0 - 6 => Sunday - Saturday, or
│ │ │ │ │ 1 - 7 => Monday - Sunday)
↓ ↓ ↓ ↓ ↓
* * * * * command to be executed
# 舉例
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
#每晚的21:30重啟apache
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
#每月1、10、22日的4 : 45重啟apache
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
#每周六、周日的1 : 10重啟apache
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
#在每天18 : 00至23 : 00之間每隔30分鐘重啟apache
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
#每星期六的11 : 00 pm重啟apache
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
#每一小時(shí)重啟apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
#晚上11點(diǎn)到早上7點(diǎn)之間,每隔一小時(shí)重啟apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
#每月的4號(hào)與每周一到周三的11點(diǎn)重啟apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
#一月一號(hào)的4點(diǎn)重啟apache為ddns添加每分鐘定時(shí)任務(wù)
*/1 * * * * cd /usr/ddns && /home/pi/.local/bin/ddns
*/1 * * * * echo "ddns解析"
注意:在cron中使用命令必須用全路徑,查看全路徑方法
which ddns
錯(cuò)誤排查
若是提示返回信息中顯示
info (No MTA installed, discarding output),可安裝sudo apt-get install postfix查看郵件命令
cat /var/mail/pi修改命令后記得重啟cron服務(wù)
若是日志出現(xiàn)
Skipping @reboot jobs -- not system startup,將/var/run/crond.reboot刪除,然后重啟cron服務(wù)
