package pkg import ( "regexp" "strings" "github.rackspace.com/gorax/tools/util" ) type ( Arp struct { ArpTable []*ArpEntry `json:"arp_table"` } Barp struct { ArpTable []*ArpEntry `json:"arp_table"` } ArpEntry struct { Name string `json:"name"` Address string `json:"address"` MacAddr string `json:"mac_address"` Vlan string `json:"vlan"` Expiration int `json:"expiration"` Status string `json:"status"` RouteDomain int `json:"route_domain"` } ) // f5 show net arp parser, will send the request to be parsed by nat11 func ParseARPStatus(c string) *Arp { a := new(Arp) a.Parse(c) return a } func (a *Arp) Parse(c string) { for _, s := range strings.Split(c, "\n") { r := regexp.MustCompile(`(?i)(.*?)\s+([0-9]+\..*?)\s+(\S+)\s+(\S+)\s+([0-9]+)\s+(\S+)`).FindStringSubmatch(s) if len(r) != 0 { i := strings.Split(r[1], "%") a.ArpTable = append(a.ArpTable, &ArpEntry{ Name: r[1], Address: i[0], RouteDomain: func() int { if len(i) != 2 { return 0 } return util.Atoi(i[1]) }(), MacAddr: normalizeMac(r[3]), Vlan: r[4], Expiration: util.Atoi(r[5]), Status: r[6], }) continue } r = regexp.MustCompile(`^([0-9]+\..*?)\s+(\S+)\s+(\S+)\s+([0-9]+)\s+(\S+)`).FindStringSubmatch(s) if len(r) != 0 { a.ArpTable = append(a.ArpTable, &ArpEntry{ Name: r[1], Address: r[1], MacAddr: normalizeMac(r[2]), Vlan: r[3], Expiration: util.Atoi(r[5]), Status: r[5], }) } } } // 5/8/2018 - dp - unused at this time, commented out //func ParseBARPStatus(c string) *Barp { // a := new(Barp) // a.Parse(c) // return a //} func (a *Barp) Parse(s string) { for _, v := range regexp.MustCompile(`(?i)arp\s+(.*?)\s+-\s+(.*?:.*?)\s+vlan\s+(.*?)\s+expire\s+([0-9]+)\S\s+(\S+)`).FindAllStringSubmatch(s, -1) { i := strings.Split(v[1], "%") a.ArpTable = append(a.ArpTable, &ArpEntry{ Name: v[1], Address: i[0], RouteDomain: func() int { if len(i) == 2 { return 0 } return util.Atoi(i[1]) }(), MacAddr: normalizeMac(v[2]), Vlan: v[3], Expiration: util.Atoi(v[4]), Status: v[5], }) } } func normalizeMac(mac string) string { split := strings.Split(mac, ":") for _, i := range split { if regexp.MustCompile(`(?)^[0-9a-z]$`).MatchString(i) { i = "0" + i } } return strings.Join(split, ":") }