TMDB Hosts 自动更新与同步 DnsParse 脚本 & 本地版 🌟
本文介绍一套用于自动更新 TMDB 相关域名的 DNS 解析记录,并将其同步到常用媒体服务(如 Emby、Nastools)的本地脚本工具集。所有脚本均可直接在本地运行,无需依赖远程资源。
📖 目录导航
✨ 脚本特点与功能
🌐 多 DNS 提供商支持 :使用多个公共 DNS 解析服务获取最新的 IP 地址,提高解析成功率。
🏓 智能 IP 筛选 :自动 Ping 测试所有解析到的 IP,仅保留可连通的高质量 IP。
💾 无损更新 Hosts :采用标记块(###start###
与 ###end###
)方式更新,保留原有 hosts 内容。
📂 多服务同步 :支持将更新后的 hosts 文件自动同步到 Emby 和 Nastools 的配置目录。
⏰ 完全本地化 :所有脚本和配置均存储在本地,无需网络访问即可执行。
🐍 安装 Python3 1 sudo apt update && sudo apt install python3 -y
🔧 DnsParse.py 本地配置 下载或创建脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 mkdir -p /mnt/mydisk/my-sh/hosts && touch /mnt/mydisk/my-sh/hosts/DnsParse.py && chmod +x /mnt/mydisk/my-sh/hosts/DnsParse.py && cat > /mnt/mydisk/my-sh/hosts/DnsParse.py <<'EOF' import datetime import json import platform import requests from subprocess import Popen, PIPE api = "http://api.ip33.com/dns/resolver" hosts = ["api.themoviedb.org" , "image.tmdb.org" , "www.themoviedb.org" ] dnsProvider = ["156.154.70.1" , "208.67.222.222" ] hostLocate = "/etc/hosts" def pingBatch(ips): if ips is not None and type (ips) == list: for ip in ips[:]: result = pingIp(ip) if not result: ips.remove(ip) def pingIp(ip) -> bool: try: ping_process = Popen(["ping" , "-c" , "1" , ip], stdout=PIPE, stderr=PIPE) ping_output, ping_error = ping_process.communicate() if ping_process.returncode == 0: print (f"[√] IP:{ip} 可以ping通" ) return True else : print (f"[×] IP:{ip} 无法ping通" ) return False except Exception as e: print (f"Ping IP:{ip} 出错:{str(e)}" ) return False def analysis(domain, dns) -> list: params = { "domain" : domain, "type" : "A" , "dns" : dns } headers = { "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60" } try: response = requests.post(url=api, data=params, headers=headers) ipDics = json.loads(response.text)["record" ] ips = [dic["ip" ] for dic in ipDics] return ips except Exception as e: print ("解析dns出错:" ) print (e) def hostWritor(hostDic): platInfo = platform.platform().UPER() if "LINUX" in platInfo: hostFile = "/etc/hosts" else : print ("未能识别当前操作系统,且用户未指定host文件所在目录!" ) return origin = "" with open(hostFile, "r" , encoding="utf-8" ) as f: flag = False for eachLine in f.readlines(): if r"###start###" in eachLine: flag = True elif r"###end###" in eachLine: flag = False else : if not flag: origin = origin + eachLine origin = origin.strip() origin = origin + "\n###start###\n" for eachHost in hostDic: for eachIp in hostDic[eachHost]: origin = origin + eachIp + "\t" + eachHost + "\n" origin = origin + "###最后更新时间:%s###\n" % datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S' ) origin = origin + "###end###\n" with open(hostFile, "w" , encoding="utf-8" ) as f: f.write(origin) if __name__ == '__main__' : resultDic = {} for host in hosts: for dns in dnsProvider: records = analysis(host, dns) pingBatch(records) if records is not None and len(records) > 0: if host not in resultDic: resultDic[host] = records else : resultDic[host] += records hostWritor(resultDic) EOF
查看脚本内容 1 cat /mnt/mydisk/my-sh/hosts/DnsParse.py
查询 Python3 安装路径
Debian 系统通常安装在:/usr/bin/python3
执行脚本 1 /usr/bin/python3 /mnt/mydisk/my-sh/hosts/DnsParse.py
请将 /usr/bin/python3
替换为您系统上 which python3
查询得到的实际路径; 将 /mnt/mydisk/my-sh/hosts/DnsParse.py
修改为您实际存放 DnsParse.py 文件的路径。
⏰ 计划任务设置 以下命令将自动设置计划任务,实现每天自动更新 hosts 并同步到相关服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 { crontab -l; echo "" ; } | crontab - { crontab -l; echo "## 添加更新hosts文件定时任务,每天凌晨一点十分执行" ; } | crontab - { crontab -l; echo "10 1 * * * /usr/bin/python3 /mnt/mydisk/my-sh/hosts/DnsParse.py" ; } | crontab - { crontab -l; echo "" ; } | crontab - { crontab -l; echo "## 同步hosts到emby,每天凌晨一点十五分执行" ; } | crontab - { crontab -l; echo "15 1 * * * rsync -avhzp --progress --delete /etc/hosts /mnt/mydisk/home/emby-amilys/config/hosts" ; } | crontab - { crontab -l; echo "" ; } | crontab - { crontab -l; echo "## 同步hosts到nastools,每天凌晨一点二十分执行" ; } | crontab - { crontab -l; echo "20 1 * * * rsync -avhzp --progress --delete /etc/hosts /mnt/mydisk/home/nastools/config/hosts" ; } | crontab - crontab -l
手动编辑计划任务
使用此命令可以手动编辑和调整计划任务设置。
📋 查看执行结果 检查 hosts 更新是否成功
检查同步到 Emby 的 hosts 文件 1 cat /mnt/mydisk/home/emby-amilys/config/hosts
1 cat /mnt/mydisk/home/nastools/config/hosts
请将上述路径替换为您实际的 Emby 和 Nastools 配置目录路径。
💡 提示 :首次运行前请确保脚本有执行权限,并且相关目录存在。可手动执行脚本测试功能是否正常。路径请根据您的实际环境进行相应调整。
TMDB Hosts 自动更新与同步 DnsParse 脚本 & 本地版 🌟