dongshao2967 2017-06-13 08:50
浏览 247
已采纳

在golang中捕获ping命令的结果

I have result of ping command in two ways->

1->5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.067/0.078/0.087/0.007 ms

2->5 packets transmitted, 5 received, 0% packet loss, time 801ms
rtt min/avg/max/stddev = 0.019/0.034/0.044/0.010 ms, ipg/ewma 200.318/0.038 ms

these are results of ping command in two different platforms. Now From this I want value of avg/stddev in both the cases. I have written a regex-

var latencyPattern = regexp.MustCompile("(round-trip|rtt) .* = (.*)/(.*)/(.*)/(.*) *ms,")
if matches := latencyPattern.FindStringSubmatch(strOutput); len(matches) >= 5{
    latency, _ = strconv.ParseFloat(strings.TrimSpace(matches[3]), 64)
    jitter, _ = strconv.ParseFloat(strings.TrimSpace(matches[5]), 64)
}

Now this pattern is working fine for 2nd result of ping (giving me 0.034 and 0.01 as result). But for result 1, it is not able to find the pattern (i.e. to give 0.078 and 0.007). How can I change the regex to work for both?

  • 写回答

2条回答 默认 最新

  • doulu1325 2017-06-13 10:32
    关注

    You need to be more precise when defining the regex. .* is too greedy and matches across commas and other stuff.

    I suggest using

    var latencyPattern = regexp.MustCompile(`(round-trip|rtt)\s+\S+\s*=\s*([0-9.]+)/([0-9.]+)/([0-9.]+)/([0-9.]+)\s*ms`)
    

    See the regex demo.

    Go lang demo:

    package main
    
    import (
        "fmt"
        "regexp"
        "strings"
        "strconv"
    )
    
    func main() {
        strOutput := `1->5 packets transmitted, 5 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 0.067/0.078/0.087/0.007 ms 
            2->5 packets transmitted, 5 received, 0% packet loss, time 801ms
    rtt min/avg/max/stddev = 0.019/0.034/0.044/0.010 ms, ipg/ewma 200.318/0.038 ms`
        latencyPattern := regexp.MustCompile(`(round-trip|rtt)\s+\S+\s*=\s*([0-9.]+)/([0-9.]+)/([0-9.]+)/([0-9.]+)\s*ms`)
        matches := latencyPattern.FindAllStringSubmatch(strOutput, -1)
        for _, item := range matches {
            latency, _ := strconv.ParseFloat(strings.TrimSpace(item[3]), 64)
                jitter, _ := strconv.ParseFloat(strings.TrimSpace(item[5]), 64)
                fmt.Printf("AVG = %.3f, STDDEV = %.3f
    ", latency, jitter)
    
            }
    }
    

    Result:

    AVG = 0.078, STDDEV = 0.007
    AVG = 0.034, STDDEV = 0.010
    

    Pattern details:

    • (round-trip|rtt) - round-trip or rtt substrings
    • \s+ - 1+ whitespaces
    • \S+ - 1+ non-whitespace symbols
    • \s*=\s* - a = enclosed with 0+ whitespaces
    • ([0-9.]+) - Group 1: the first number
    • / - a /
    • ([0-9.]+) - Group 2: the second number
    • / - a /
    • ([0-9.]+) - Group 3: the third number
    • / - a /
    • ([0-9.]+) - Group 4: the fourth number
    • \s* - 0+ whitespaces
    • ms - a substring ms
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格