Files
config-ros/main.py

94 lines
2.4 KiB
Python
Raw Normal View History

2025-08-18 15:48:15 +08:00
from librouteros import connect,Api
import csv
import ssl
import threading
2025-09-08 12:55:20 +08:00
from steps import *
2025-08-18 15:48:15 +08:00
'''
自动配置 ros 脚本需要安装 librouteros
2025-09-28 18:05:21 +08:00
```
2025-08-18 15:48:15 +08:00
pip install librouteros
2025-09-28 18:05:21 +08:00
```
2025-08-18 15:48:15 +08:00
配置文件格式为 CSV包含以下字段
- index: 配置索引重要这个 index 用来生成对应 l2tp-out 用户名中的序号每个节点都是唯一且固定的
- name: 城市名
- code: 城市代码
- gateway: 网关地址
- public: 公网 IP
- mask: 公网子网掩码
- private: 内网 IP
2025-09-28 18:05:21 +08:00
如果需要添加或修改配置项 config.*.bak.csv 文件复制配置项到 config.csv 文件中
2025-08-18 15:48:15 +08:00
2025-09-28 18:05:21 +08:00
如果需要修改配置内容在下面 配置执行步骤 部分添加或修改函数调用
2025-08-18 15:48:15 +08:00
'''
threads = []
failed = []
2025-09-08 12:55:20 +08:00
# 配置执行步骤
steps = [
2025-09-28 18:05:21 +08:00
configNet
2025-09-08 12:55:20 +08:00
]
2025-08-18 15:48:15 +08:00
def main():
# 加载配置文件
config_data = []
with open('config.csv', 'r', encoding='utf-8-sig') as file:
reader = csv.DictReader(file)
for row in reader:
config_data.append(row)
# 多线程执行 ros 配置
for _, config in enumerate(config_data):
thread = threading.Thread(target=start, args=(config,))
thread.start()
threads.append(thread)
# 等待所有线程完成
for thread in threads:
thread.join()
# 输出结果
print('=' * 20)
print('配置完成统计({}/{}), 失败项:{}'.format(len(config_data)-len(failed), len(config_data), len(failed)))
for _, (item, err) in enumerate(failed):
print('{},{},{},{}'.format(item['public'], item['name'], item['code'], err))
def start(config):
print('{}: 配置{}({})'.format(str(config['index']).zfill(3), config['name'], config['code']))
# 连接到 ros
conn: Api
try:
ctx = ssl.create_default_context()
ctx.check_hostname = False
2025-09-06 16:32:47 +08:00
ctx.load_verify_locations(f'certs/{config["cert"]}')
2025-08-18 15:48:15 +08:00
conn = connect(
username='admin',
password='wyongk9815',
host=config['public'],
port=8729,
ssl_wrapper=ctx.wrap_socket,
)
except Exception as err:
2025-09-08 12:55:20 +08:00
failed.append((config, f'连接失败: {err}'))
2025-08-18 15:48:15 +08:00
return
# 配置 ros
try:
2025-09-08 12:55:20 +08:00
for step in steps:
step(conn, config)
2025-08-18 15:48:15 +08:00
except Exception as err:
2025-09-08 12:55:20 +08:00
failed.append((config, f'配置失败: {err}'))
2025-08-18 15:48:15 +08:00
# 关闭连接
conn.close()
main()