Lua - 使用 string.find 来查找句子?

Lua - Using string.find to find a sentence?

根据之前的 Stack Overflow 响应,(here) 我正在尝试检查 io.popen 命令的响应中是否返回了特定的 sentence/string 我是 运行宁..

local function DevicePing(ip)
    local handler = io.popen("ping -c 3 " ..ip.. " 2>&1")
    local response = handler:read("*a")
    print(response)
    if string.find(response, "0% packet loss") then
        print ("Pings were all successfull.")
    else
        print ("Pings were all unsuccessfull.")
    end
end

DevicePing("192.168.1.180")

但是每次我运行它都找不到请求的string/sentence;请参阅下面的打印输出..

PING 192.168.1.180 (192.168.1.180): 56 data bytes
64 bytes from 192.168.1.180: seq=0 ttl=64 time=2.983 ms
64 bytes from 192.168.1.180: seq=1 ttl=64 time=1.620 ms
64 bytes from 192.168.1.180: seq=2 ttl=64 time=2.465 ms

--- 192.168.1.180 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 1.620/2.356/2.983 ms
     
Pings were all unsuccessfull.     

没有看到“0% 数据包丢失”并表示成功,我做错了什么?

% 是 Lua 模式中的转义字符。 使用 "0%% packet loss" 表示文字 %.

但是,此模式也将匹配 100% packet loss。我建议 ", 0%% packet loss".

此外,它在报告中显示小数的 macOS 中不起作用:0.0% packet loss