排行榜
原创运维中心

python脚本每10分钟自动执行阿里云dns解析

本文阅读 1 分钟
首页 运维中心 正文
广告

如果使用python执行自动阿里云解析,请参考这篇文章:利用python脚本实现自动阿里云域名DNS解析


本文章解决批量执行的问题。

先给出目录结构:

alidns

├── abc

│ ├── local.unfit.cn.py

│ ├── test.admin.unfit.cn.py

│ ├── test.api.unfit.cn.py

│ ├── test.napi.unfit.cn.py

│ ├── test.store.unfit.cn.py

│ └── test.xxjob.unfit.cn.py

└── dns.py


贴上dns.py的代码:

import os
import subprocess
def run_python_files_in_directory(directory):
    # 确保给定的路径是目录  
    if not os.path.isdir(directory):
        print(f"Error: {directory} is not a directory.")
        return
    # 遍历目录中的所有文件  
    for filename in os.listdir(directory):
        if filename.endswith(".py") and filename != "__init__.py":  # 排除__init__.py文件(如果需要)  
            # 构建文件的完整路径  
            file_path = os.path.join(directory, filename)
            print(f"Running {file_path}...")
            # 使用subprocess运行Python文件  
            # 注意:这里假设你的Python环境已正确设置(即python3在PATH中)  
            try:
                # 使用subprocess.run()运行Python文件  
                # 可以根据需要添加其他参数,如shell=True(但注意安全性)  
                # 这里我们使用python3来确保使用Python 3版本  
                subprocess.run(["/usr/local/bin/python3/bin/python3", file_path], check=True)
            except subprocess.CalledProcessError as e:
                print(f"Error running {file_path}: {e}")
# 使用示例  
directory_path = "/home/wwwroot/alidns/abc"
run_python_files_in_directory(directory_path)


crontab -e
*/10 * * * * /usr/local/bin/python3/bin/python3 /home/wwwroot/alidns/dns.py


dns.py脚本会把abc目录下的所有.py文件都执行一次。

原创文章,作者:大洋哥,如若转载,请注明出处:https://www.unfit.cn/archives/191.html
CentOS ups 断网自动关机脚本
« 上一篇 02-26
python批量检查ssl过期时间并给企业微信发通知
下一篇 » 02-26
广告