#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <unordered_map>
#include <sstream>
#include <unistd.h> // For usleep// 假设存在的 LOG_INFO 和 LOG_WARNING 函数
#define LOG_INFO(...) std::cout << __VA_ARGS__ << std::endl
#define LOG_WARNING(...) std::cerr << __VA_ARGS__ << std::endlclass SwitchMonitor {
public:void lldpNeighborInfoHandle();private:void writeExchangeSerialData(const std::string& command) {std::cout << "Sending command: " << command << std::endl;}std::string readExchangeSerialData() {return R"(GigabitEthernet0/0/20 has 1 neighbor(s):Neighbor index :1Chassis type :MAC addressChassis ID :1001-0114-0101Port ID type :Interface namePort ID :hhhhh1System name :HYExpired time :3sGigabitEthernet0/0/21 has 2 neighbor(s):Neighbor index :1Chassis type :MAC addressChassis ID :1004-0114-0102Port ID type :Interface namePort ID :CM300-PID-0001System name :HhXSExpired time :3sNeighbor index :2Chassis type :MAC addressChassis ID :1004-0113-0103Port ID type :Interface namePort ID :CM300-PID-0001System name :HYXSExpired time :3sGigabitEthernet0/0/22 has 1 neighbor(s):Neighbor index :1Chassis type :MAC addressChassis ID :3cc7-867d-efffPort ID type :Interface namePort ID :GigabitEthernet0/0/2Port description :GigabitEthernet0/0/2System name :FutureMatrixSystem description :hhhhhhhFutureMatrix Versatile Routing Platform SoftwareVRP (R) software, Version 5.170 (S5735 V200R022C00SPC500)System capabilities supported :bridge routerSystem capabilities enabled :bridge routerManagement address type :ipv4Management address value :192.168.1.253Expired time :95sPort VLAN ID(PVID) :1VLAN name of VLAN 1:VLAN 0001Auto-negotiation supported :YesAuto-negotiation enabled :YesOperMau :speed(100)/duplex(Full)Power port class :PDPSE power supported :NoPSE power enabled :NoPSE pairs control ability :NoPower pairs :UnknownPort power classification :UnknownLink aggregation supported:YesLink aggregation enabled :NoAggregation port ID :0Maximum frame Size :10232GigabitEthernet0/0/23 has 1 neighbor(s):Neighbor index :1Chassis type :MAC addressChassis ID :1001-0114-0104Port ID type :Interface namePort ID :S5730System name :XJExpired time :3sGigabitEthernet0/0/24 has 2 neighbor(s):Neighbor index :1Chassis type :MAC addressChassis ID :1004-0114-0105Port ID type :Interface namePort ID :PID-0005System name :xj2Expired time :3sNeighbor index :2Chassis type :MAC addressChassis ID :1004-0113-0106Port ID type :Interface namePort ID :PID-0006System name :xj3Expired time :3s)";}struct MacAddressInfo {std::string deviceType;std::string identifier;std::string board;std::string slot;std::string interface;};MacAddressInfo parseMacAddress(const std::string& mac) {return {"DeviceType", "Identifier", "Board", "Slot", "Interface"};}std::vector<std::unordered_map<std::string, std::string>> contents_;
};void SwitchMonitor::lldpNeighborInfoHandle() {std::string responseConnectInfo;writeExchangeSerialData("display lldp neighbor\n");int totalCount = 0;while (true) {usleep(10 * 1000); // 10 毫秒responseConnectInfo = readExchangeSerialData();if (responseConnectInfo.empty()) {break; // 如果没有回复,退出循环} else {std::regex pattern(R"(GigabitEthernet0/0/(\d+) has (\d+) neighbor\(s\):([\s\S]*?)(?=GigabitEthernet0/0/\d+|\Z|$))");std::smatch matches;std::string::const_iterator it(responseConnectInfo.cbegin());while (std::regex_search(it, responseConnectInfo.cend(), matches, pattern)) {std::string portToFind = matches[1].str();int neighborCount = std::stoi(matches[2].str());std::string neighborsDetails = matches[3].str();LOG_INFO("############ GigabitEthernet0/0/" << portToFind << " with " << neighborCount << " neighbor(s)" << " neighborsDetails: " << neighborsDetails);if (neighborCount == 1) {std::regex singleNeighborPattern(R"(Neighbor index\s*:(\d+)[\s\S]*?Chassis ID\s*:\s*(\S+))");std::smatch singleNeighborMatches;if (std::regex_search(neighborsDetails, singleNeighborMatches, singleNeighborPattern)) {std::string macToFindCurrent = singleNeighborMatches[2].str();LOG_INFO("Single Neighbor Chassis ID: " << macToFindCurrent);MacAddressInfo foundInfo = parseMacAddress(macToFindCurrent);if (foundInfo.deviceType.empty()) {LOG_WARNING("Protocol MAC device, parse MAC failed, MAC: " << macToFindCurrent);} else {//xxxxx 处理信息}}} else if (neighborCount > 1) {std::regex neighborPattern(R"(Neighbor index\s*:(\d+)[\s\S]*?Chassis ID\s*:\s*(\S+))");std::smatch neighborMatches;std::string::const_iterator nIt(neighborsDetails.cbegin());while (std::regex_search(nIt, neighborsDetails.cend(), neighborMatches, neighborPattern)) {std::string macToFindCurrent = neighborMatches[2].str();LOG_INFO("multy Neighbor Chassis ID: " << macToFindCurrent);MacAddressInfo foundInfo = parseMacAddress(macToFindCurrent);if (foundInfo.deviceType.empty()) {LOG_WARNING("Protocol MAC device, parse MAC failed, MAC: " << macToFindCurrent);} else {//xxxxx 处理信息}// 更新迭代器到下一个邻居nIt = neighborMatches.suffix().first;}}totalCount++;LOG_INFO("--------------------------------------------------------------------------------------- totalCount" << totalCount);// 移动迭代器到当前匹配的末尾以搜索下一个接口it = matches.suffix().first;}usleep(10);}}
}int main() {SwitchMonitor monitor;monitor.lldpNeighborInfoHandle();LOG_INFO("$$$$$$$$$$$$$$$$$$$$ end");return 0;
}
经过了70w次近1小时的测试,,没有问题。