双击
安装snmputil和MIB Browser
getSwitchStatus.cpp:
#include <iostream>
#include <stdio.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
using namespace std;
int main() {
init_snmp("getSwitchStatus");
netsnmp_session session;
netsnmp_pdu* pdu;
netsnmp_pdu* response;
oid ifOperStatus_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 8 }; // OID for ifOperStatus (.1.3.6.1.2.1.2.2.1.8)
size_t ifOperStatus_oid_len = sizeof(ifOperStatus_oid) / sizeof(oid);
// Set up the SNMP session
snmp_sess_init(&session);
session.peername = strdup("192.168.37.1"); // Replace with the IP address or hostname of your switch
session.version = SNMP_VERSION_2c;
const char* str="COMMUNITY_STRING"; // Use SNMP v2c (you can also use v1 or v3)
session.community =const_cast<u_char*>(reinterpret_cast<const u_char*>(str)); // Replace with your community string
session.community_len = strlen(reinterpret_cast<const char*>(session.community));
// Open the SNMP session
if (!snmp_open(&session) ) {
fprintf(stderr, "Failed to open SNMP session.\n");
exit(1);
}
// Create the PDU
pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, ifOperStatus_oid, ifOperStatus_oid_len);
// Send the SNMP request
int status = snmp_synch_response(&session, pdu, &response);
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
// Process the response
for (netsnmp_variable_list* vars = response->variables; vars; vars = vars->next_variable) {
if (vars->type == ASN_INTEGER) {
int value = *vars->val.integer;
cout << "Interface Status: ";
if (value == 1) {
cout << "Up" << endl;
}
else if (value == 2) {
cout << "Down" << endl;
}
else {
cout << "Unknown" << endl;
}
}
}
}
else {
// Handle the error
if (status == STAT_SUCCESS) {
cout << "Error in packet: " << snmp_errstring(response->errstat) << endl;
}
else {
cout << "SNMP error: " << snmp_api_errstring(status) << endl;
}
}
// Clean up
if (response) {
snmp_free_pdu(response);
}
snmp_close(&session);
return 0;
}
getConfig.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct{
string ip;//IP地址
vector<int> oid;//oid
string communityStr;//社区字符串
}Config;
//得到:后面的字符串
string getLastStr(string str){
string res=str.substr(str.find(":")+1);
return res;
}
//把1.12.123变成int数组
vector<int> strToIntArray(string str){
stringstream ss(str);//
vector<int> res;
string token;
while (getline(ss,token,'.'))
{
int num=stoi(token);
res.push_back(num);
}
return res;
}
//读取config.txt文件
Config readConfigTxt(string& path){
Config res;
ifstream file(path,std::ios::binary);
if(!file){
cout<<"Failed to open file"<<endl;
return res;
}
string line;
while (getline(file,line))
{
//ip
line=getLastStr(line);
res.ip=line;
//oid
getline(file,line);
line=getLastStr(line);
res.oid=strToIntArray(line);
//communityStr
getline(file,line);
line=getLastStr(line);
res.communityStr=line;
}
}
int main(){
string path="/home/xyt/work/vsCode/getSnmp/config.txt";
Config res=readConfigTxt(path);
//ip
cout<<res.ip<<endl;
//oid
vector<int> a=res.oid;
int len=a.size();
int oidArr[len];
for (int i = 0; i < len; i++)
{
oidArr[i]=a[i];
}
//遍历oidArr
for (int i = 0; i < len; i++)
{
cout<<oidArr[i]<<" ";
}
cout<<endl;
//communityStr
cout<<res.communityStr<<endl;
}
代码还没有整合