refine: not use ping if def interface

This commit is contained in:
lit
2025-06-24 17:13:31 +08:00
parent 5f7aff7d1b
commit 4f2e9a1adf
+25 -16
View File
@@ -26,28 +26,37 @@ def append_interface(command, pre, interface):
if not interface_def: if not interface_def:
command += [pre, interface] command += [pre, interface]
def check_connectivity_ping(host, interface): def check_connectivity(interface):
command = ['ping'] if not interface_def:
append_interface(command, '-I', interface) command = ['ping']
command += ['-c', '1', host] # -n 1 on windows, -c 1 on linux append_interface(command, '-I', interface)
try: host = "223.5.5.5"
result = subprocess.check_call(command, timeout=2) command += ['-c', '1', host] # -n 1 on windows, -c 1 on linux
return result == 0 try:
except subprocess.TimeoutExpired: result = subprocess.check_call(command, timeout=2)
print(f"接口 {interface}Ping to {host} timed out.") return result == 0
return False except subprocess.TimeoutExpired:
except subprocess.CalledProcessError: print(f"接口 {interface}Ping to {host} timed out.")
return False return False
except Exception as e: except subprocess.CalledProcessError:
print(f"接口 {interface}Error during ping: {e}") return False
return False except Exception as e:
print(f"接口 {interface}Error during ping: {e}")
return False
else:
try:
request_get_text("http://connect.rom.miui.com/generate_204")
return True
except:
return False
device = 0 device = 0
if interface_def: if interface_def:
warnings.warn("no interface given by -i or --interface, use default route") warnings.warn("no interface given by -i or --interface, use default route")
# 检测网络联通性 # 检测网络联通性
if check_connectivity_ping("223.5.5.5", interface): if check_connectivity(interface):
exit() exit()
with open(file, 'r') as f: with open(file, 'r') as f: