package switchy import ( "regexp" "strconv" "strings" ) var ( // Pulls info from show vlan vlanLineRE = regexp.MustCompile(`(\d+)\s+(\S+)\s+(\S+)\s+(.*)`) // matches "VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2" to jump out of parsing loop endVlanRE = regexp.MustCompile(`VLAN\s+Type\s+SAID`) ) // LoadSwitch runs all the base gather methods on a new SwitchClient object func (sw *SwitchClient) LoadSwitch() error { // Gather basic switch info if err := sw.SwitchModelInfo(); err != nil { return err } // Initialize the SSH Client if err := sw.InitSSHClient(); err != nil { return err } // Gather port names if err := sw.PortNameGather(); err != nil { return err } // Gather neighbors if err := sw.UnipedeNeighborData(0); err != nil { return err } // Gather detail on neighbors if err := sw.UnipedeNeighborInfo(0); err != nil { return err } err := sw.GetVlans() if err != nil { return err } return nil } // ShowVlanParse takes the output of a 'show vlan' command and parses it in to the SwitchClient.vlans object func (sw *SwitchClient) ShowVlanParse(s *string) error { split := strings.Split(*s, "\r\n") for _, line := range split { if endVlanRE.MatchString(line) { // Jump out if we get passed the vlan lines return nil } info := vlanLineRE.FindStringSubmatch(line) // 1 = num // 2 = name // 3 = status // 4 = ports if len(info) > 0 { i, err := strconv.Atoi(info[1]) if err != nil { continue } ports := new([]string) for _, p := range strings.Split(info[4], ",") { if p != "" { *ports = append(*ports, strings.TrimSpace(p)) } } sw.Vlans = append(sw.Vlans, &SwitchVlan{ Vlan: i, Name: &info[2], Ports: func() *[]string { if ports != nil { return ports } return nil }(), }) } } return nil }