package asaModels import ( "errors" "fmt" "regexp" "strconv" ) type SSHConfig struct { StrictKey bool `json:"stricthostkeycheck"` Timeout int `json:"timeout"` Version int `json:"version"` KeyExchange string `json:"key-exchange"` Scopy bool `json:"scopy"` SSHHosts []SSHHost `json:"ssh_hosts"` } type SSHHost struct { Network string `json:"network"` Netmask string `json:"netmask"` IfName string `json:"interface"` } func (s SSHHost) String() string { return fmt.Sprintf("%s %s %s", s.Network, s.Netmask, s.IfName) } func (s *SSHConfig) Parse(c string) error { s.StrictKey = regexp.MustCompile(`(?i)(ssh\s+stricthostkeycheck)`).MatchString(c) s.Scopy = regexp.MustCompile(`(?i)ssh\s+([0-9].*?)\s+(\S+)\s+(\S+)`).MatchString(c) match := regexp.MustCompile(`(?i)ssh\s+timeout\s+([0-9]+)`).FindStringSubmatch(c) if len(match) > 0 { i, err := strconv.Atoi(match[1]) if err != nil { return err } s.Timeout = i } match = regexp.MustCompile(`(?i)ssh\s+version\s+([0-9]+)`).FindStringSubmatch(c) if len(match) > 0 { i, err := strconv.Atoi(match[1]) if err != nil { return err } s.Version = i } match = regexp.MustCompile(`(?i)ssh\s+key-exchange\s+(.*?)\r`).FindStringSubmatch(c) if len(match) > 0 { s.KeyExchange = match[1] } for _, v := range regexp.MustCompile(`(?i)ssh\s+(\S+)\s+([0-9]{1,3}\..*?)\s+(\S+)`).FindAllStringSubmatch(c, -1) { s.SSHHosts = append(s.SSHHosts, SSHHost{ Network: v[1], Netmask: v[2], IfName: v[3], }) } return nil } func (s SSHHost) Valid() error { if s.Network == "" { return errors.New("Network cannot be empty.") } if s.Netmask == "" { return errors.New("Netmask cannot be empty.") } if s.IfName == "" { return errors.New("interface name cannot be empty") } return nil }