import os
import re
import json
def validate_mac(mac):
“”“校验 MAC 地址格式是否正确”“”
pattern = re.compile(r’^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$')
return pattern.match(mac.strip().upper()) is not None
def update_tddp_config_file(mac, device_id):
config_path = ‘/etc/config/tddp’
if not os.path.exists(config_path): print(f"错误:文件 {config_path} 不存在!") return False try: with open(config_path, 'r') as f: lines = f.readlines() except Exception as e: print(f"读取文件 {config_path} 失败:{e}") return False formatted_mac = ":".join(mac.replace('-', ':').split(':')) updated_lines = [] for line in lines: if line.strip().startswith("option macaddr"): updated_lines.append(f"\toption macaddr '{formatted_mac}'\n") elif line.strip().startswith("option deviceId"): updated_lines.append(f"\toption deviceId '{device_id}'\n") else: updated_lines.append(line) try: with open(config_path, 'w') as f: f.writelines(updated_lines) print("✅ /etc/config/tddp 更新成功!") return True except Exception as e: print(f"写入文件 {config_path} 失败:{e}") return False
def update_json_file(file_path, update_data):
if not os.path.exists(file_path):
print(f"错误:文件 {file_path} 不存在!")
return False
try: with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) except json.JSONDecodeError: print(f"错误:文件 {file_path} 内容不是有效的 JSON 格式。") return False data.update(update_data) try: with open(file_path, 'w', encoding='utf-8') as f: json.dump(data, f, indent=4) print(f"✅ 文件 {file_path} 更新成功!") return True except Exception as e: print(f"写入文件 {file_path} 失败:{e}") return False
def main():
# 用户输入 MAC 地址
input_mac = input(“请输入 MAC 地址(格式:00:2D:0F:72:17:01 或 00-2D-0F-72-17-01):”).strip().upper()
if not validate_mac(input_mac):
print(“❌ 错误:MAC 地址格式不正确!”)
return
# 统一格式为 - if ':' in input_mac: formatted_input_mac = "-".join(input_mac.split(':')) else: formatted_input_mac = input_mac # 用户输入 device_id input_device_id = input("请输入 device_id:").strip() if not input_device_id: print("❌ 错误:device_id 不能为空!") return # 第一步:更新 /etc/config/tddp update_tddp_config_file(formatted_input_mac, input_device_id) # 第二步:更新 /tmp/sysinfo/device-info.json device_info_path = '/tmp/sysinfo/device-info.json' device_info_update = { "model_id": "72123002", "model_version": "2.0", "product_version": "2.0", "product_id": "72123002" } update_json_file(device_info_path, device_info_update) # 第三步:更新 /tmp/sysinfo/tddp.json tddp_path = '/tmp/sysinfo/tddp.json' tddp_update = { "mac": formatted_input_mac, "device_id": input_device_id } update_json_file(tddp_path, tddp_update) print("\n🎉 所有文件更新完成!")
if name == ‘main’:
main()
import os
import re
import subprocess
import time
def find_and_kill_java_processes():
try:
# 执行 ps -ef 命令
output = subprocess.check_output([‘ps’, ‘-ef’], stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e:
print(“执行 ps 命令失败:”, e)
return
# 筛选包含 Central 或 Controller 的 java 进程 lines = output.splitlines() target_pids = [] for line in lines: if 'java' in line and ('Central' in line or 'Controller' in line): # 使用正则提取 PID(确保匹配用户、PID、PPID 等字段) match = re.match(r'\S+\s+(\d+)\s+(\d+)', line) if match: pid = match.group(1) target_pids.append(pid) if not target_pids: print("未找到匹配的 Central 或 Controller java 进程。") return # 杀掉进程 for pid in target_pids: try: print(f"正在 kill -9 进程 PID={pid}") subprocess.run(['kill', '-9', pid], check=True) except subprocess.CalledProcessError as e: print(f"kill -9 {pid} 失败:{e}") print("进程清理完成。") time.sleep(3) # 等待 3 秒确保进程完全退出
def start_central():
target_dir = ‘/opt/tplink/Central/lib/’
if not os.path.isdir(target_dir): print("未找到 Central 程序目录,跳过启动。") return try: os.chdir(target_dir) print(f"已进入 Central 目录:{target_dir}") except Exception as e: print(f"无法进入目录 {target_dir}:{e}") return command = [ './central-starter-port-local', '-Djava.home=/opt/tplink/EAPController/lib/', '-XX:ParallelGCThreads=2', '-Xms256m', '-Xmx300m', '-Xmn128m', '-Xss1024k', '-XX:MaxHeapFree=32m', '-XX:StackSize=1024k', '-R:ExpectedEdenSize=160m' ] try: process = subprocess.Popen(command) print(f"Central 程序已启动,PID={process.pid}") except Exception as e: print(f"启动 Central 程序失败:{e}")
def start_eap_controller():
target_dir = ‘/opt/tplink/EAPController/lib/’
if not os.path.isdir(target_dir): print("未找到 EAPController 程序目录,跳过启动。") return try: os.chdir(target_dir) print(f"已进入 EAPController 目录:{target_dir}") except Exception as e: print(f"无法进入目录 {target_dir}:{e}") return command = [ './local-starter', '-Djava.home=/opt/tplink/EAPController/lib/', '-XX:ParallelGCThreads=2', '-Xms256m', '-Xmx640m', '-Xmn128m', '-Xss1024k', '-XX:MaxHeapFree=32m', '-XX:StackSize=1024k', '-XX:+VerboseGC', '-Dcom.sun.management.jmxremote.port=1090', '-Dcom.sun.management.jmxremote.ssl=false', '-Dcom.sun.management.jmxremote.authenticate=false', '-Djava.rmi.server.hostname=192.168.0.1', '-R:ExpectedEdenSize=160m' ] try: process = subprocess.Popen(command) print(f"EAPController 程序已启动,PID={process.pid}") except Exception as e: print(f"启动 EAPController 程序失败:{e}")
if name == ‘main’:
find_and_kill_java_processes()
start_central()
start_eap_controller()
import os
import json
import subprocess
====== 函数:更新 sefDomain 字段 ======
def update_sef_domain():
config_path = ‘/etc/cloud_proc/cloud_config.cfg’
if not os.path.exists(config_path): print(f"错误:文件 {config_path} 不存在!") return False try: with open(config_path, 'r', encoding='utf-8') as f: config_data = json.load(f) except json.JSONDecodeError as e: print(f"JSON 解析失败:{e}") return False if 'cloud' in config_data and 'sefDomain' in config_data['cloud']: old_value = config_data['cloud']['sefDomain'] config_data['cloud']['sefDomain'] = 'n-device-entry-omada-beta.i.tplinkcloud.com' print(f"成功将 sefDomain 从 {old_value} 更新为 n-device-entry-omada-beta.i.tplinkcloud.com") else: print("错误:配置中未找到 cloud.sefDomain 字段!") return False try: with open(config_path, 'w', encoding='utf-8') as f: json.dump(config_data, f, indent=4) print("配置文件更新成功!") return True except Exception as e: print(f"写入文件失败:{e}") return False
====== 函数:重启服务 ======
def restart_cloud_service():
service_script = “/etc/init.d/zzzcloud_proc”
command = [service_script, “restart”]
if not os.path.exists(service_script): print(f"错误:服务脚本 {service_script} 不存在!") return False try: result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) print("服务重启成功!") print(result.stdout) return True except subprocess.CalledProcessError as e: print("服务重启失败!") print(e.stderr) return False
====== 主流程 ======
if name == ‘main’:
if update_sef_domain():
restart_cloud_service()
三个脚本和一,按顺序 更新 /etc/config/tddp 中的 macaddr 和 deviceId;
更新 /tmp/sysinfo/device-info.json 和 /tmp/sysinfo/tddp.json 文件;杀掉 Central 和 Controller 的 Java 进程;
重启 Central 和 EAPController 服务;修改 /etc/cloud_proc/cloud_config.cfg 中的 sefDomain 字段;
重启 /etc/init.d/zzzcloud_proc 服务;