beiklive's blog beiklive's blog
首页
  • 语言学习

    • C/C++
    • Python
    • Qt
  • 系统&引擎

    • Linux
    • Godot
  • 啥都学

    • 夏姬八学
    • 好好学习
  • 折腾记录

    • 树莓派
    • Obsidian
    • 实践记录
  • 技术文档
  • 工具网站
  • Github项目
  • 友情链接
  • 关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

beiklive

全沾艺人
首页
  • 语言学习

    • C/C++
    • Python
    • Qt
  • 系统&引擎

    • Linux
    • Godot
  • 啥都学

    • 夏姬八学
    • 好好学习
  • 折腾记录

    • 树莓派
    • Obsidian
    • 实践记录
  • 技术文档
  • 工具网站
  • Github项目
  • 友情链接
  • 关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CRC校验
  • 自动获取github的hosts并更新本地host文件
    • 代码
    • 定时运行
  • 配置指定IP 走指定网关
  • wireshark 抓取指定程序
  • 使用Git Action自动初始化Gitalk评论
  • Note_Other
beiklive
2024-05-15
目录

自动获取github的hosts并更新本地host文件

前一篇

00.CRC校验

# 代码

实现该功能需要依赖 https://github.com/ineo6/hosts (opens new window) 项目

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests

# 定义源URL和目标文件
source_url = "https://gitlab.com/ineo6/hosts/-/raw/master/hosts"
target_file = "/etc/hosts"

# 定义标记
start_marker = "# >>>>start\n"
end_marker = "\n# <<<<end"

def fetch_latest_content(url):
    # 发送GET请求获取最新内容
    response = requests.get(url)
    if response.status_code == 200:
        return response.text.splitlines()
    else:
        print(f"无法从 {url} 获取内容。")
        return None

def update_host_file_with_new_content(source_content, file_path):
    # 构建新的内容
    new_content = start_marker + '\n' + '\n'.join(source_content) + end_marker + '\n'

    # 替换目标文件中的内容
    try:
        with open(file_path, 'r+') as f:
            file_data = f.read()
            start_pos = file_data.find(start_marker)
            end_pos = file_data.find(end_marker, start_pos + len(start_marker))

            if start_pos != -1 and end_pos != -1:
                f.seek(0)
                f.truncate()
                f.write(file_data[:start_pos])
                f.write(new_content)
                f.write(file_data[end_pos + len(end_marker):])
                print("目标文件已更新。")
            else:
                print("未找到标记,无法替换内容。")

    except FileNotFoundError:
        print(f"文件 {file_path} 未找到。")
    except IOError:
        print(f"无法写入到文件 {file_path}。")

def main():
    # 获取最新内容
    latest_content = fetch_latest_content(source_url)
    if latest_content:
        # 更新目标文件中的内容
        update_host_file_with_new_content(latest_content, target_file)
    else:
        print("未能获取最新内容。")

if __name__ == "__main__":
    main()
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

上面的脚本会获取到 https://gitlab.com/ineo6/hosts/-/raw/master/hosts 的内容,然后插入到/etc/hosts 文件的 start_marker 和 end_marker 之间

请手动在 /etc/hosts 中添加 start_marker 和 end_marker

# 定时运行

在 crontab 中添加定时任务

  1. 打开 crontab 文件
sudo vim  /etc/crontab
1
  1. 添加定时任务

这是我的脚本路径,请自行替换为自己的路径

30 * * * * root /usr/bin/python3 /home/beiklive/Host/updateHost.py >> /home/beiklive/Host/log.log 2>&1
1
  1. 刷新crontab
sudo systemctl restart cron
1
编辑 (opens new window)
#github#python#host
上次更新: 2024/05/22, 14:11:38
CRC校验
配置指定IP 走指定网关

← CRC校验 配置指定IP 走指定网关→

最近更新
01
爬虫技术与法律风险:个人开发者的注意事项
05-22
02
个人开发者的抉择:个人工作室 vs 公司主体 🤔
05-22
03
《计算机网络,自顶向下方法》笔记(一)
05-20
更多文章>
Theme by Vdoing | Copyright © 2024-2024 beiklive | 苏ICP备20038092号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式