package asaModels import ( "regexp" "strconv" "strings" ) type ( Clock struct { Timezone string `json:"timezone"` Offset int `json:"offset"` SummerTime ClockSummerTime `json:"summer_time"` NTP NTP `json:"ntp"` } ClockSummerTime struct { Name string `json:"name"` Type string `json:"type"` Details *SummerTime `json:"details,omitempty"` // Only a pointer so it omitempty will work instead of sending an empty object } SummerTime struct { StartFirstWeek bool `json:"start_first_week,omitempty"` // First week of the month StartLastWeek bool `json:"start_last_week,omitempty"` // Last week of the month // Only for absolute StartDate int `json:"start_date,omitempty"` StartYear int `json:"start_year,omitempty"` EndDate int `json:"end_date,omitempty"` EndYear int `json:"end_year,omitempty"` // Shared between recurring and absolute StartWeek int `json:"start_week,omitempty"` // Week number to start StartDay string `json:"start_weekday,omitempty"` // Weekday to start StartMonth string `json:"start_month,omitempty"` // Month to start StartTime string `json:"start_time,omitempty"` // Time it starts, form of "00:00" EndWeek int `json:"end_week,omitempty"` // Week number to end EndDay string `json:"end_weekday,omitempty"` // Weekday to end EndMonth string `json:"end_month,omitempty"` // Month to end EndTime string `json:"end_time,omitempty"` // Time to end, form of "00:00" Offset int `json:"offset,omitempty"` // Offset to add in minutes } NTP struct { Authenticate bool `json:"authenticate_servers"` AuthenticationKeys []NTPAuthKey `json:"authentication_keys,omitempty"` Servers []NTPServer `json:"servers"` } NTPAuthKey struct { Number int `json:"number"` MD5 bool `json:"md5"` Key string `json:"key"` } NTPServer struct { Hostname string `json:"hostname"` Key int `json:"key,omitempty"` Prefer bool `json:"prefer"` Interface string `json:"source"` } ) var ( regexClockSTRecurDetail = regexp.MustCompile(`clock summer-time (\S+) recurring (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+)(.*)`) // Regex for clock summer-time date parsing regexClockSTDateDetail = regexp.MustCompile(`clock summer-time (\S+) date (\S+) (\d+) (\d+) (\S+) (\S+) (\d+) (\d+) (\S+)(.*)`) // Regex for clock summer-time recurring simple regexRecurringSimple = regexp.MustCompile(`clock summer-time (\S+) recurring`) regexNTPServer = regexp.MustCompile(`ntp server (\S+) source (\S+)(.*)`) ) func (c *Clock) Parse(s string) { // Sample simple: // clock summer-time cdt recurring // Sample detailed recurring: // clock summer-time cdt recurring 2 Sun Mar 2:00 2 Sun Nov 2:00 // 1 2 3 4 5 6 7 8 9 // Sample absolute: // clock summer-time cdt date Mar 13 2017 2:00 Nov 6 2017 2:00 // 1 2 3 4 5 6 7 8 9 m := regexp.MustCompile(`clock timezone (\S+) (\S+)`).FindStringSubmatch(s) if len(m) > 0 { c.Timezone = m[1] i, _ := strconv.Atoi(m[2]) c.Offset = i } else { // Line doesn't exist, ASA's default to UTC in this case c.Timezone = "UTC" c.Offset = 0 return } // clock summer-time cdt recurring 1 Sun Mar 2:00 1 Sun Nov 2:00 m = regexClockSTRecurDetail.FindStringSubmatch(s) if len(m) > 0 { // Detailed recurring c.SummerTime.Name = m[1] c.SummerTime.Type = "recurring" sw, _ := strconv.Atoi(m[2]) ew, _ := strconv.Atoi(m[6]) c.SummerTime.Details = &SummerTime{ StartWeek: sw, StartDay: m[3], StartMonth: m[4], StartTime: m[5], EndWeek: ew, EndDay: m[7], EndMonth: m[8], EndTime: m[9], } if m[10] != "" { of, _ := strconv.Atoi(strings.TrimSpace(m[10])) c.SummerTime.Details.Offset = of } return } m = regexClockSTDateDetail.FindStringSubmatch(s) if len(m) > 0 { // Detailed Absolute Date c.SummerTime.Name = m[1] c.SummerTime.Type = "absolute" sd, _ := strconv.Atoi(m[3]) ed, _ := strconv.Atoi(m[7]) sy, _ := strconv.Atoi(m[4]) ey, _ := strconv.Atoi(m[8]) c.SummerTime.Details = &SummerTime{ StartDate: sd, StartMonth: m[2], StartTime: m[5], StartYear: sy, EndDate: ed, EndMonth: m[6], EndYear: ey, EndTime: m[9], } if m[10] != "" { of, _ := strconv.Atoi(strings.TrimSpace(m[10])) c.SummerTime.Details.Offset = of } return } m = regexRecurringSimple.FindStringSubmatch(s) if len(m) > 0 { // Simple recurring c.SummerTime.Name = m[1] c.SummerTime.Type = "recurring" } } func (n *NTP) Parse(s string) { m := regexNTPServer.FindAllStringSubmatch(s, -1) for _, server := range m { var newServer NTPServer newServer.Hostname = server[1] newServer.Interface = server[2] if strings.Contains(server[0], "prefer") { newServer.Prefer = true } n.Servers = append(n.Servers, newServer) } }