摸鱼周报 ~ 20241101
本周派活过多,摸鱼时间被严重压缩。
忙里偷闲靠摸鱼提升自己,又学到了一些新东西,下周规划一下摸鱼学啥,把这些时间利用起来。
另外由于原先购买的腾讯云DNS过期,续费实在是太贵了,于是决定换域名,阿里云相对来讲比较便宜,目前家庭内网微皮恩和部分终端物联网设备都是用的这个域名来访问家庭公网IP的,那么转到阿里云之后需要重新写一个DDNS的脚本。
程序思路如下:
请求获取当前公网IP地址(直接请求ip.me),得到公网IP后,跟之前的解析记录进行对比,如果不一样就是IP已经更改了,将最新的公网IP通过API(安全起见,阿里云单独建立一个子账号获取Token,权限只给控制DNS解析记录)传入解析,并将上述行为记录到日志中。
程序代码块如下:
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
import json
import re
import requests
import logging
from typing import List
from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alidns20150109 import models as alidns_20150109_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
# 设置日志配置
logging.basicConfig(filename='alyDDNS.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
def create_client() -> Alidns20150109Client:
"""
使用AK&SK初始化账号Client
@return: Client
@throws Exception
"""
config = open_api_models.Config(
access_key_id='This key_id',
access_key_secret='This key_secret'
)
config.endpoint = f'alidns.cn-shenzhen.aliyuncs.com'
return Alidns20150109Client(config)
def update_alydns(value) -> None:
client = create_client()
update_domain_record_request = alidns_20150109_models.UpdateDomainRecordRequest(
record_id='域名记录ID获取方法可百度',
rr='test.com',
type='A',
value=value,
ttl=600
)
runtime = util_models.RuntimeOptions()
try:
client.update_domain_record_with_options(update_domain_record_request, runtime)
logging.info(f"DNS UPDATE: {value}")
except Exception as e:
logging.error(f"DNS UPDATE FAILURE!: {e}")
def get_current_public_ip():
# 获取当前公网IP地址
try:
response = requests.get("https://ip.me")
response.raise_for_status() # 检查请求是否成功
ip = response.text.strip()
logging.info(f"get current_public_ip SUCCESS: {ip}")
return ip
except requests.RequestException as e:
logging.error(f"get current_public_ip FAILURE: {e}")
return
def get_alydnsINFO():
client = create_client()
describe_domain_record_info_request = alidns_20150109_models.DescribeDomainRecordInfoRequest(
record_id='使用个人ID'
)
runtime = util_models.RuntimeOptions()
# 复制代码运行请自行打印 API 的返回值
try:
result = client.describe_domain_record_info_with_options(describe_domain_record_info_request, runtime)
logging.info(f"get DNS INFO: {result.body.value}")
return result.body.value
except Exception as e:
logging.error(f"get DNS INFO FAILURE!: {e}")
return None
def main():
# 获取当前公网IP并比对解析IP,检查是否更换
ipv4_pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
current_public_ip = get_current_public_ip()
if current_public_ip is None or not re.match(ipv4_pattern, current_public_ip):
sys.exit(0)
RecordList = get_alydnsINFO()
if RecordList is None:
sys.exit(0)
# 检查是否需要更新DNS记录
if current_public_ip == RecordList:
logging.info(f"No Change IP! CurrentPublicIP:{current_public_ip}")
else:
logging.info(f"IP Change!!! CurrentPublicIP:{current_public_ip}|Old_DNSIP: {RecordList}")
update_alydns(current_public_ip)
if __name__ == '__main__':
main()
写的比较粗略,见谅
然后把脚本打包成完整的exe,再使用任务计划程序定时运行即可。
其它有关家庭微皮恩还有物联网终端控制的有时间再写吧,一直打算总结一下这些技术内容,给对这些感兴趣的兄弟拍拍坑,可惜我太懒了,有人关注的话我再写吧
-
摸鱼周报
每周五的摸鱼派活动-摸鱼周报 发表你的每周总结
大佬
佬
好
姥
大佬