树莓派4B动态解析公网IP
因为一开始采用的是Frp内网穿透技术,走的阿里云服务器的通道,所以带宽最大只支持1M,导致我本地服务器的流媒体服务访问人数一多就会引起观影卡顿,所以只能更换为DDNS技术来动态解析本地的公网IP,共试了两种方法,第一种方法我本地测试有点问题,后续换为了第二种方法,在此记录。
一、自定义Python脚本 + 阿里云SDK 实现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.py
import os
import re
import time
import json
import platform
import requests
from settings import * # 所有需要修改的数据
from aliyunsdkcore.client import AcsClient
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
def LocalIPv6Address():
# 判断当前操作系统,使用命令
if(platform.system() == 'Windows'):
cmd = 'ipconfig'
elif(platform.system() == 'Linux'):
cmd = 'ifconfig'
else:
print("操作系统不兼容")
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)运行脚本
python3 aliddns.py
二、开源工具Features实现DDNS
网址: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运行
ddns
此时会默认在此目录下生成
config.json
文件若想指定目录生成需带参执行 例:
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
}
执行
ddns
此时将会获取本地公网IP,然后解析域名
三、设置cron定时任务
查看任务列表
crontab -l
编辑/添加 任务
crontab -e
查看定时服务状态
service cron status
启动定时服务
service cron start
重启定时服务
service cron restart
// or
sudo /etc/init.d/cron restart重载定时配置
service cron reload
终止定时服务
service cron stop
定时任务规则
# 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
#每一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
#晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
#每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
#一月一号的4点重启apache为ddns添加每分钟定时任务
*/1 * * * * cd /usr/ddns && /home/pi/.local/bin/ddns
*/1 * * * * echo "ddns解析"
注意:在cron中使用命令必须用全路径,查看全路径方法
which ddns
错误排查
若是提示返回信息中显示
info (No MTA installed, discarding output)
,可安装sudo apt-get install postfix
查看邮件命令
cat /var/mail/pi
修改命令后记得重启cron服务
若是日志出现
Skipping @reboot jobs -- not system startup
,将/var/run/crond.reboot
删除,然后重启cron服务
评论