package asaModels import ( "net" "regexp" "strconv" "strings" ) var ( ipv4inText = regexp.MustCompile(`\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b`) ipv4Parts = regexp.MustCompile(`^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`) ) type ( Telnet struct { Enabled bool `json:"enabled"` Timeout int `json:"timeout"` Hosts []AllowedHost `json:"allowed_hosts"` } AllowedHost struct { Address net.IPNet `json:"addres"` Interface string `json:"interface"` } ) func (t *Telnet) Parse(s string) { // Timeout match := regexp.MustCompile(`telnet timeout (\S+)`).FindStringSubmatch(s) if len(match) > 0 { i, err := strconv.Atoi(match[1]) if err == nil { t.Timeout = i } } // IPv4 Hosts ips := regexp.MustCompile(`telnet ((?:[0-9]{1,3}\.){3}[0-9]{1,3}) ((?:[0-9]{1,3}\.){3}[0-9]{1,3}) (\S+)`).FindAllStringSubmatch(s, -1) for _, line := range ips { ip := net.ParseIP(line[1]) n := net.ParseIP(line[2]) t.Hosts = append(t.Hosts, AllowedHost{ Address: net.IPNet{ IP: ip, Mask: net.IPMask(n), }, Interface: line[3], }) } // IPv6 Hosts ips = regexp.MustCompile(`telnet (\S+) (\S+)\n`).FindAllStringSubmatch(s, -1) for _, ip := range ips { if strings.Contains(ip[1], "/") { _, n, err := net.ParseCIDR(ip[1]) if err == nil { t.Hosts = append(t.Hosts, AllowedHost{ Address: *n, Interface: ip[2], }) } } } if len(t.Hosts) > 0 { t.Enabled = true } }