From 334159d3d7b1fbab04a5af9ecb4a4d48978dea08 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Sun, 18 Jan 2026 02:51:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(ping):=20=E4=BF=AE=E5=A4=8D=20TTL=20expired?= =?UTF-8?q?=20=E5=AF=BC=E8=87=B4=E4=B8=BB=E6=9C=BA=E8=AF=AF=E5=88=A4?= =?UTF-8?q?=E4=B8=BA=E5=AD=98=E6=B4=BB=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 ExecCommandPing 中增加错误关键词检测,当 ping 输出包含 TTL expired、Destination unreachable 等错误信息时,不再将 目标主机标记为存活。 Fixes #454 --- core/icmp.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/core/icmp.go b/core/icmp.go index ce885cb..bd61823 100644 --- a/core/icmp.go +++ b/core/icmp.go @@ -21,6 +21,22 @@ import ( // pingForbiddenChars 命令注入防护 - 禁止的字符 var pingForbiddenChars = []string{";", "&", "|", "`", "$", "\\", "'", "%", "\"", "\n"} +// pingErrorKeywords ping 失败的关键词(跨平台) +var pingErrorKeywords = []string{ + // Windows + "TTL expired", + "Destination host unreachable", + "Destination net unreachable", + "Request timed out", + "General failure", + "transmit failed", + // Linux/macOS + "Time to live exceeded", + "100% packet loss", + "Network is unreachable", + "No route to host", +} + // CheckLive 检测主机存活状态 func CheckLive(hostslist []string, Ping bool, config *common.Config, state *common.State) []string { // 创建局部WaitGroup @@ -391,6 +407,17 @@ func RunPing(hostslist []string, chanHosts chan string, livewg *sync.WaitGroup) wg.Wait() } +// containsPingError 检查 ping 输出是否包含错误关键词 +func containsPingError(output string) bool { + outputLower := strings.ToLower(output) + for _, keyword := range pingErrorKeywords { + if strings.Contains(outputLower, strings.ToLower(keyword)) { + return true + } + } + return false +} + // ExecCommandPing 执行系统Ping命令检测主机存活 func ExecCommandPing(ip string) bool { // 过滤黑名单字符(命令注入防护) @@ -426,7 +453,7 @@ func ExecCommandPing(ip string) bool { // 分析输出结果 output := outinfo.String() - return strings.Contains(output, "true") && strings.Count(output, ip) > 2 + return strings.Contains(output, "true") && strings.Count(output, ip) > 2 && !containsPingError(output) } // makemsg 构造ICMP echo请求消息