package com.wgcloud.util;
|
|
import com.wgcloud.entity.vo.OspfInterface;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.stereotype.Component;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 解析ospf接口信息工具类
|
*/
|
@Component
|
public class OspfInterfaceUtil {
|
|
public List<OspfInterface> analysisStr(String str){
|
List<OspfInterface> ospfInterfaceList = new ArrayList<>();
|
String s1 = parseOspfInterface(str);
|
String[] lines = s1.split("\n");
|
for (String line : lines) {
|
if(StringUtils.isNotBlank(line.trim())){
|
String[] parts = line.trim().split("\\s+");
|
if (parts.length >= 6) {
|
OspfInterface entity = new OspfInterface(
|
parts[0],
|
parts[1],
|
parts[2],
|
Integer.parseInt(parts[3]),
|
Integer.parseInt(parts[4]),
|
parts[5],
|
parts[6]
|
);
|
ospfInterfaceList.add(entity);
|
} else {
|
System.out.println("搜索结果不符合要求,无法解析");
|
}
|
}
|
}
|
System.out.println(ospfInterfaceList);
|
return ospfInterfaceList;
|
}
|
|
private String parseOspfInterface(String s) {
|
// 找到 "Router ID" 所在行的位置
|
int routerIdLineStart = s.indexOf("Cost Pri");
|
if (routerIdLineStart == -1) {
|
System.out.println("未找到包含\"Cost Pri\"的行");
|
return "";
|
}
|
// 找到 "Router ID" 所在行的下一行的起始位置
|
int nextLineStart = s.indexOf("\n", routerIdLineStart) + 1;
|
|
// 找到最后的 "<H3C>" 所在行的位置
|
int h3cLineStart = s.lastIndexOf("<H3C>");
|
if (h3cLineStart == -1) {
|
System.out.println("未找到最后的\"<H3C>\"");
|
return "";
|
}
|
// 找到最后的 "<H3C>" 所在行的上一行的结束位置
|
int prevLineEnd = s.lastIndexOf("\n", h3cLineStart - 1) + 1;
|
|
// 截取从 Router ID 的下一行到最后的 <H3C> 上一行的内容
|
String result = s.substring(nextLineStart, prevLineEnd);
|
System.out.println(result);
|
return result;
|
}
|
}
|