prod
This commit is contained in:
+119
-10
@@ -18,7 +18,7 @@ func (p *HuaweiParser) GetCommands() []string {
|
||||
return []string{
|
||||
"display version",
|
||||
"display interface",
|
||||
"display ip interface brief",
|
||||
"display interface brief",
|
||||
"display lldp neighbor",
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,15 @@ func (p *HuaweiParser) parseInterfaces(interfaceOutput, briefOutput string) []mo
|
||||
|
||||
if brief, ok := briefMap[currentInterface.Name]; ok {
|
||||
currentInterface.IP = brief.IP
|
||||
if currentInterface.Speed == "" {
|
||||
currentInterface.Speed = brief.Speed
|
||||
}
|
||||
if currentInterface.Duplex == "" {
|
||||
currentInterface.Duplex = brief.Duplex
|
||||
}
|
||||
if currentInterface.VLAN == "" {
|
||||
currentInterface.VLAN = brief.VLAN
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,11 +114,41 @@ func (p *HuaweiParser) parseInterfaces(interfaceOutput, briefOutput string) []mo
|
||||
currentInterface.Mask = matches[2]
|
||||
}
|
||||
|
||||
// 带宽
|
||||
if speedRegex := regexp.MustCompile(`(\d+)\s+(Kbps|Mbps|Gbps)`); speedRegex.MatchString(line) {
|
||||
// 带宽格式1: Speed : 1000Mbps
|
||||
if speedRegex := regexp.MustCompile(`Speed\s*:\s*(\d+)\s*(Kbps|Mbps|Gbps)`); speedRegex.MatchString(line) {
|
||||
matches := speedRegex.FindStringSubmatch(line)
|
||||
currentInterface.Speed = matches[1] + matches[2]
|
||||
}
|
||||
// 带宽格式2: BW 1000000 Kbit
|
||||
if speedRegex2 := regexp.MustCompile(`BW\s+(\d+)\s+(Kbit|Mbit|Gbit)`); speedRegex2.MatchString(line) && currentInterface.Speed == "" {
|
||||
matches := speedRegex2.FindStringSubmatch(line)
|
||||
currentInterface.Speed = matches[1] + " " + matches[2]
|
||||
}
|
||||
|
||||
// VLAN信息: Port link-type
|
||||
if linkTypeRegex := regexp.MustCompile(`Port link-type\s*:\s*(\S+)`); linkTypeRegex.MatchString(line) {
|
||||
currentInterface.VLAN = linkTypeRegex.FindStringSubmatch(line)[1]
|
||||
}
|
||||
if untaggedRegex := regexp.MustCompile(`Untagged VLAN ID\s*:\s*(\S+)`); untaggedRegex.MatchString(line) {
|
||||
untagged := untaggedRegex.FindStringSubmatch(line)[1]
|
||||
if untagged != "none" && untagged != "--" {
|
||||
if currentInterface.VLAN != "" {
|
||||
currentInterface.VLAN = currentInterface.VLAN + " " + untagged
|
||||
} else {
|
||||
currentInterface.VLAN = untagged
|
||||
}
|
||||
}
|
||||
}
|
||||
if taggedRegex := regexp.MustCompile(`Tagged VLAN ID\s*:\s*(\S+)`); taggedRegex.MatchString(line) {
|
||||
tagged := taggedRegex.FindStringSubmatch(line)[1]
|
||||
if tagged != "none" && tagged != "--" {
|
||||
if currentInterface.VLAN != "" {
|
||||
currentInterface.VLAN = currentInterface.VLAN + " (T:" + tagged + ")"
|
||||
} else {
|
||||
currentInterface.VLAN = "T:" + tagged
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,15 +163,85 @@ func (p *HuaweiParser) parseInterfaceBrief(output string) map[string]models.Inte
|
||||
interfaces := make(map[string]models.Interface)
|
||||
lines := strings.Split(output, "\n")
|
||||
|
||||
mode := 0 // 0=未开始, 1=route mode, 2=bridge mode
|
||||
for _, line := range lines {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 4 {
|
||||
iface := models.Interface{
|
||||
Name: fields[0],
|
||||
IP: fields[1],
|
||||
Status: strings.ToLower(fields[3]),
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" || strings.HasPrefix(trimmed, "---") {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(trimmed, "route mode") {
|
||||
mode = 1
|
||||
continue
|
||||
}
|
||||
if strings.Contains(trimmed, "bridge mode") {
|
||||
mode = 2
|
||||
continue
|
||||
}
|
||||
|
||||
fields := strings.Fields(trimmed)
|
||||
if len(fields) > 0 && (fields[0] == "Interface" || fields[0] == "interface") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Route mode: Interface Link Protocol Primary IP Description
|
||||
if mode == 1 && len(fields) >= 3 {
|
||||
fullName := expandH3CInterfaceName(fields[0])
|
||||
ip := ""
|
||||
if len(fields) >= 4 && fields[3] != "--" {
|
||||
ip = fields[3]
|
||||
}
|
||||
interfaces[iface.Name] = iface
|
||||
iface := models.Interface{
|
||||
Name: fullName,
|
||||
Status: strings.ToLower(fields[1]),
|
||||
IP: ip,
|
||||
}
|
||||
interfaces[fullName] = iface
|
||||
}
|
||||
|
||||
// Bridge mode: Interface Link Speed Duplex Type PVID Description
|
||||
if mode == 2 && len(fields) >= 5 {
|
||||
fullName := expandH3CInterfaceName(fields[0])
|
||||
|
||||
speed := fields[2]
|
||||
duplex := fields[3]
|
||||
switch duplex {
|
||||
case "A":
|
||||
duplex = "auto"
|
||||
case "F(a)", "F":
|
||||
duplex = "full"
|
||||
case "H":
|
||||
duplex = "half"
|
||||
}
|
||||
|
||||
vlan := ""
|
||||
if len(fields) >= 5 {
|
||||
switch fields[4] {
|
||||
case "A":
|
||||
vlan = "Access"
|
||||
case "T":
|
||||
vlan = "Trunk"
|
||||
case "H":
|
||||
vlan = "Hybrid"
|
||||
}
|
||||
}
|
||||
if len(fields) >= 6 {
|
||||
vlan = vlan + " " + fields[5]
|
||||
}
|
||||
|
||||
iface := models.Interface{
|
||||
Name: fullName,
|
||||
Status: strings.ToLower(fields[1]),
|
||||
Speed: speed,
|
||||
Duplex: duplex,
|
||||
VLAN: strings.TrimSpace(vlan),
|
||||
}
|
||||
if existing, ok := interfaces[fullName]; ok {
|
||||
if existing.IP != "" {
|
||||
iface.IP = existing.IP
|
||||
}
|
||||
}
|
||||
interfaces[fullName] = iface
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user