captain-zhou
11/13/2018 - 3:02 AM

RegPingMsg

regex example

package main

import (
	"fmt"
	"regexp"
	"strconv"
)

func RegPingMsg(pingMsg string) int {
    exp := ".*([0-9]+) packet.* received"
    reg := regexp.MustCompile(exp)
    matches := reg.FindAllStringSubmatch(pingMsg, -1)
    if len(matches) == 1 {
    	n, _ := strconv.Atoi(matches[0][1])
    	if n >= 1 {
    		fmt.Println("ping success")
    		return 0
    	}
    }
    fmt.Println("ping fail")
    return 0
}

func main() {
     RegPingMsg("ping -a 10.200.200.254 -c 3 -m 100 -vpn-instance vpn \u001b[1D8 10.42.255.4\r\n  PING 10.42.255.4: 56  data bytes, press CTRL_C to break\r\n    Reply from 10.42.255.4: bytes=56 Sequence=1 ttl=62 time=4 ms\r\n    Reply from 10.42.255.4: bytes=56 Sequence=2 ttl=62 time=5 ms\r\n    Reply from 10.42.255.4: bytes=56 Sequence=3 ttl=62 time=4 ms\r\n\r\n  --- 10.42.255.4 ping statistics ---\r\n    3 packet(s) transmitted\r\n    3 packet(s) received\r\n    0.00% packet loss\r\n    round-trip min/avg/max = 4/4/5 ms\r\n \r\n<LTAS-002-HB06-A203-C01.E01>")
     RegPingMsg("ping -a 10.200.200.254 -c 3 -m 100 -vpn-instance vpn \u001b[1D8 10.42.255.4\r\n  PING 10.42.255.4: 56  data bytes, press CTRL_C to break\r\n    Reply from 10.42.255.4: bytes=56 Sequence=1 ttl=62 time=4 ms\r\n    Reply from 10.42.255.4: bytes=56 Sequence=2 ttl=62 time=5 ms\r\n    Reply from 10.42.255.4: bytes=56 Sequence=3 ttl=62 time=4 ms\r\n\r\n  --- 10.42.255.4 ping statistics ---\r\n    3 packet(s) transmitted\r\n    0 packet(s) received\r\n    0.00% packet loss\r\n    round-trip min/avg/max = 4/4/5 ms\r\n \r\n<LTAS-002-HB06-A203-C01.E01>")
}