| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package device
- import (
- "bufio"
- "fmt"
- "network-topology-discovery/pkg/models"
- "regexp"
- "strings"
- )
- // ASAParser ASA防火墙解析器
- type ASAParser struct {
- BaseParser
- }
- // GetCommands 获取ASA设备命令列表
- func (p *ASAParser) GetCommands() []string {
- return []string{
- "show version",
- "show interface",
- "show ip",
- "show inventory",
- }
- }
- // Parse 解析ASA设备输出
- func (p *ASAParser) Parse(device *models.Device, outputs []string) error {
- if len(outputs) < 4 {
- return fmt.Errorf("insufficient command outputs")
- }
- p.parseVersion(device, outputs[0])
- device.Interfaces = p.parseInterfaces(outputs[1])
- return nil
- }
- func (p *ASAParser) parseVersion(device *models.Device, output string) {
- hostnameRegex := regexp.MustCompile(`^(\S+)\s*>`)
- lines := strings.Split(output, "\n")
- for _, line := range lines {
- if matches := hostnameRegex.FindStringSubmatch(line); len(matches) > 1 {
- device.Hostname = matches[1]
- break
- }
- }
- if strings.Contains(output, "ASA Version") {
- lines := strings.Split(output, "\n")
- for _, line := range lines {
- if strings.Contains(line, "ASA Version") {
- device.OSVersion = strings.TrimSpace(line)
- break
- }
- }
- }
- uptimeRegex := regexp.MustCompile(`up\s+\d+\s+hours?\s+\d+\s+mins?`)
- if matches := uptimeRegex.FindString(output); matches != "" {
- device.Uptime = matches
- }
- }
- func (p *ASAParser) parseInterfaces(output string) []models.Interface {
- var interfaces []models.Interface
- scanner := bufio.NewScanner(strings.NewReader(output))
- var currentInterface *models.Interface
- for scanner.Scan() {
- line := scanner.Text()
- if nameRegex := regexp.MustCompile(`^Interface\s+(\S+)\s+"(\S+)"`); nameRegex.MatchString(line) {
- if currentInterface != nil {
- interfaces = append(interfaces, *currentInterface)
- }
- matches := nameRegex.FindStringSubmatch(line)
- currentInterface = &models.Interface{
- Name: matches[1],
- Description: matches[2],
- }
- }
- if currentInterface != nil {
- if statusRegex := regexp.MustCompile(`\S+\s+is\s+(up|down)`); statusRegex.MatchString(line) {
- matches := statusRegex.FindStringSubmatch(line)
- currentInterface.Status = matches[1]
- }
- if macRegex := regexp.MustCompile(`Hardware is\s+\S+,\s+address is\s+(\S+)`); macRegex.MatchString(line) {
- matches := macRegex.FindStringSubmatch(line)
- currentInterface.MAC = matches[1]
- }
- if ipRegex := regexp.MustCompile(`IP address\s+(\d+\.\d+\.\d+\.\d+),\s+subnet mask\s+(\d+\.\d+\.\d+\.\d+)`); ipRegex.MatchString(line) {
- matches := ipRegex.FindStringSubmatch(line)
- currentInterface.IP = matches[1]
- currentInterface.Mask = matches[2]
- }
- if speedRegex := regexp.MustCompile(`(\d+)\s+(Mbps|Gbps)`); speedRegex.MatchString(line) {
- matches := speedRegex.FindStringSubmatch(line)
- currentInterface.Speed = matches[1] + " " + matches[2]
- }
- }
- }
- if currentInterface != nil {
- interfaces = append(interfaces, *currentInterface)
- }
- return interfaces
- }
|