package asaModels import "regexp" var OBJECTRE = regexp.MustCompile(`(?i)object\s+(network)\s+(\S+)\s*(host|subnet)\s+(\d+\.\d+\.\d+\.\d+(?:\s+\d+\.\d+\.\d+\.\d+)?)(?:\s*description\s+"(.*)"\n)?`) type ( Objects struct { NetworkObjects []*NetworkObject `json:"network_objects"` } NetworkObject struct { Name string `json:"name"` Description string `json:"description"` Type string `json:"type"` NetworkType string `json:"network_type"` Network *string `json:"network"` Subnet *string `json:"subnet"` } ) //Parse will regex match ASA command output for 'show objects' and parse each match // match[i][0] = full match (single object config) // match[i][1] = object.Type (will always be network until we work different stuff in) // match[i][2] = object.Name // match[i][3] = object.NetworkType (host or subnet) // match[i][4] = string to be filtered into host IP or network netmask (*object.Network and *object.Subnet) // match[i][5] = object.Description (if any) func (o *Objects) Parse(s string) { for _, v := range OBJECTRE.FindAllStringSubmatch(s, -1) { if len(v) != 6 { continue } switch v[1] { case "network": o.objectParse(&v) } } } func (o *Objects) objectParse(s *[]string) { switch (*s)[3] { case "host": o.NetworkObjects = append(o.NetworkObjects, &NetworkObject{ Type: (*s)[1], Name: (*s)[2], NetworkType: (*s)[3], Network: &(*s)[4], Description: (*s)[5], }) case "subnet": r := KEYVALRE.FindStringSubmatch((*s)[4]) if len(r) == 3 { o.NetworkObjects = append(o.NetworkObjects, &NetworkObject{ Type: (*s)[1], Name: (*s)[2], NetworkType: (*s)[3], Network: &r[1], Subnet: &r[2], Description: (*s)[5], }) } } } func (o *Objects) FindHost(h string) (matches []*NetworkObject) { return FindObjectHost(h, o.NetworkObjects) } func FindObjectHost(h string, o []*NetworkObject) (matches []*NetworkObject) { for _, obj := range o { if obj.NetworkType == "host" { if *obj.Network == h { matches = append(matches, obj) } } } return } func (o *Objects) FindObject(name string) *NetworkObject { return FindObject(name, o.NetworkObjects) } func FindObject(name string, o []*NetworkObject) *NetworkObject { for _, obj := range o { if obj.Name == name { return obj } } return nil }