import subprocessimport osimport timedef is_connected(): # ping一个内网地址 internal_ip = '10.10.10.11' internal_ping = subprocess.call(['ping', '-n', '1', '-w', '500', internal_ip], stdout=subprocess.PIPE) if internal_ping == 0: # 内网连接正常,检查软件是否已经关闭 drive_process = "cloud-drive-ui.exe" try: output = subprocess.check_output(["tasklist", "/FI", f"IMAGENAME eq {drive_process}"]) if drive_process in str(output): # 软件正在运行,需要关闭 os.system("taskkill /f /im cloud-drive-ui.exe") return True except subprocess.CalledProcessError: # 异常处理 pass # ping一个公共网址 external_ip = 'www.baidu.com' external_ping = subprocess.call(['ping', '-n', '1', '-w', '500', external_ip], stdout=subprocess.PIPE) if external_ping == 0: # 外网连接正常,检查软件是否已经启动 drive_process = "cloud-drive-ui.exe" try: output = subprocess.check_output(["tasklist", "/FI", f"IMAGENAME eq {drive_process}"]) if drive_process in str(output): # 软件已经启动,不需要重复运行 return True else: # 软件没有启动,需要运行 drive_path = r"C:\Program Files (x86)\Synology\SynologyDrive\bin\launcher.exe" os.startfile(drive_path) return True except subprocess.CalledProcessError: # 异常处理 pass return False# 持续运行程序while True: if not is_connected(): # 如果网络连接中断,关闭 Synology Drive 软件 pass # 等待一段时间后再次检查网络连接状态 time.sleep(10)
(图片来源网络,侵删)
0 评论