package pkg import ( "fmt" "regexp" "strings" ) type IRule struct { Name string `json:"name"` Contents []string `json:"contents"` AppService string `json:"app_service"` Partition string `json:"partition"` } // Parse : Parse a raw 'list ltm rule all-properties' command to an IRule structure func (i *IRule) Parse(raw string) { /* ltm rule SNAT-REVERSE { app-service none partition Common when CLIENT_ACCEPTED { #log local0. "[IP::local_addr] & [IP::client_addr]" if { [IP::addr [IP::local_addr] equals 8.8.8.8] and [IP::addr [IP::client_addr] equals 192.168.44.46] } { log local0. "[IP::remote_addr] & [IP::client_addr]" snat 2.3.5.6 } } } */ for _, line := range strings.Split(raw, "\n") { if regexp.MustCompile(`list ltm rule \S+ all-properties`).MatchString(line) { continue // Skip the command } if regexp.MustCompile(`ltm rule \S`).MatchString(line) { continue // Skip the first line } if line == " }" { continue // Skip the last line } if regexp.MustCompile(`netsec.nsi@\(\S+\).*\(tmos\)`).MatchString(line) { continue // Skip the line that has the command line in it } part := regexp.MustCompile(`partition (\S+)`).FindStringSubmatch(line) as := regexp.MustCompile(`app-service (\S+)`).FindStringSubmatch(line) switch { case len(as) > 0: i.AppService = as[1] case len(part) > 0: i.Partition = part[1] default: i.Contents = append(i.Contents, line) } } } // PopulateIRules : populates the f5.ltm.irules entrye from a list ltm rule command func (f5 *F5) PopulateIRules() error { res, err := f5.sshClient.PostCommand("list ltm rule", defaultPrompt, defaultTimeout) if err != nil { return err } f5.LTM.IRules = make(map[string]IRule) for _, match := range regexp.MustCompile(`ltm rule (\S+)`).FindAllStringSubmatch(*res.Response, -1) { cmd := fmt.Sprintf("list ltm rule %s all-properties", match[1]) specific, err := f5.sshClient.PostCommand(cmd, defaultPrompt, defaultTimeout) if err != nil { return err } newRule := IRule{ Name: match[1], } newRule.Parse(*specific.Response) f5.LTM.IRules[match[1]] = newRule } return nil }