mirror of
https://github.com/woodchen-ink/dnspod-yxip.git
synced 2025-07-18 05:42:08 +08:00
优化ping测试
This commit is contained in:
parent
b78c62bd2d
commit
94a1186125
30
main.py
30
main.py
@ -222,20 +222,32 @@ class DNSPodManager:
|
|||||||
else: # Linux/Unix系统
|
else: # Linux/Unix系统
|
||||||
ping_args = ['ping', '-c', '1', '-W', '1', ip]
|
ping_args = ['ping', '-c', '1', '-W', '1', ip]
|
||||||
|
|
||||||
result = subprocess.run(ping_args,
|
# 最多尝试3次ping
|
||||||
capture_output=True,
|
max_retries = 3
|
||||||
text=True)
|
for attempt in range(max_retries):
|
||||||
available = result.returncode == 0
|
result = subprocess.run(ping_args,
|
||||||
|
capture_output=True,
|
||||||
|
text=True)
|
||||||
|
if result.returncode == 0:
|
||||||
|
# 更新缓存
|
||||||
|
self.ip_availability_cache[ip] = {
|
||||||
|
'available': True,
|
||||||
|
'last_check': now
|
||||||
|
}
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 如果不是最后一次尝试,等待短暂时间后重试
|
||||||
|
if attempt < max_retries - 1:
|
||||||
|
time.sleep(1) # 等待1秒后重试
|
||||||
|
|
||||||
|
# 所有尝试都失败
|
||||||
|
logger.warning(f"IP {ip} ping测试失败(尝试{max_retries}次),最后一次命令输出:{result.stdout if result.stdout else result.stderr}")
|
||||||
# 更新缓存
|
# 更新缓存
|
||||||
self.ip_availability_cache[ip] = {
|
self.ip_availability_cache[ip] = {
|
||||||
'available': available,
|
'available': False,
|
||||||
'last_check': now
|
'last_check': now
|
||||||
}
|
}
|
||||||
|
return False
|
||||||
if not available:
|
|
||||||
logger.warning(f"IP {ip} ping测试失败,命令输出:{result.stdout if result.stdout else result.stderr}")
|
|
||||||
return available
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Ping测试出错 - IP: {ip}, 错误信息: {str(e)}, 命令参数: {ping_args}")
|
logger.error(f"Ping测试出错 - IP: {ip}, 错误信息: {str(e)}, 命令参数: {ping_args}")
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user