gpt4 book ai didi

java - 如何使用 snmp4j 代理创建自己的 snmp 模拟器?

转载 作者:行者123 更新时间:2023-11-30 09:54:54 25 4
gpt4 key购买 nike

根据标题,我想创建一个 snmp 设备代理模拟器。现在,在我手中,我有一个文本文件,其中包含一堆 oid 及其值(通过 snmp walk)。

我想将所有这些作为静态 oid 添加到模拟器中。我确实创建了一个简单的函数来测试它是否注册了这个 oid(我认为它是正确的)。但到目前为止,我无法得到我想要的结果。

我复制并粘贴了 TestAgent 代码。

公共(public)类 AgentUtil 扩展 BaseAgent {

static {
LogFactory.setLogFactory(new Log4jLogFactory());
}

private String address;
public static boolean STOP=false;
private Hashtable oid_values = new Hashtable();

public AgentUtil(String address,Hashtable oid_values) throws IOException {

super(new File("conf.agent"), new File("bootCounter.agent"),
new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
this.address = address;
this.oid_values = oid_values;
agent.setWorkerPool(ThreadPool.create("RequestPool", 4));
}

@Override
protected void registerManagedObjects() {

}

public void registerManagedObject(ManagedObject mo) {
try {
server.register(mo, null);

} catch (DuplicateRegistrationException ex) {
throw new RuntimeException(ex);
}
}


public void registerCustomMO() {

// String key = "";

try {
// Iterator iterator = oid_values.keySet().iterator();
// while(iterator.hasNext()) {
// key = (String) iterator.next();
server.register(createManagedObject(),null);
// }

} catch (DuplicateRegistrationException e) {
e.printStackTrace();
}
}

private ManagedObject createManagedObject() {


VariableBinding vb = new VariableBinding(new OID("0"),new OctetString("WinsXP"));
StaticMOGroup group = new StaticMOGroup(new OID("1.2.3.4.5.6.7"),new VariableBinding[]{vb});
return group;
}

public void unregisterManagedObject(MOGroup moGroup) {
moGroup.unregisterMOs(server, getContext(moGroup));
}

/*
* Empty implementation
*/
@Override
protected void addNotificationTargets(SnmpTargetMIB targetMIB,
SnmpNotificationMIB notificationMIB) {

targetMIB.addDefaultTDomains();
}

/**
* Minimal View based Access Control
*
* http://www.faqs.org/rfcs/rfc2575.html
*/
@Override
protected void addViews(VacmMIB vacm) {

vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString(
"cpublic"), new OctetString("v1v2group"),
StorageType.nonVolatile);

vacm.addAccess(new OctetString("v1v2group"), new OctetString("public"),
SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
new OctetString("fullWriteView"), new OctetString(
"fullNotifyView"), StorageType.nonVolatile);

vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);
}

/**
* User based Security Model, only applicable to
* SNMP v.3
*
*/
protected void addUsmUser(USM usm) {
}

protected void initTransportMappings() throws IOException {
transportMappings = new TransportMapping[1];
Address addr = GenericAddress.parse(address);
TransportMapping tm = TransportMappings.getInstance()
.createTransportMapping(addr);
transportMappings[0] = tm;
}

/**
* Start method invokes some initialization methods needed to
* start the agent
* @throws IOException
*/
public void start() throws IOException {

init();
loadConfig(ImportModes.REPLACE_CREATE);
addShutdownHook();
getServer().addContext(new OctetString("public"));
finishInit();
registerCustomMO();
run();
sendColdStartNotification();

}


protected void unregisterManagedObjects() {
// here we should unregister those objects previously registered...
}

/**
* The table of community strings configured in the SNMP
* engine's Local Configuration Datastore (LCD).
*
* We only configure one, "public".
*/
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] {
new OctetString("public"), // community name
new OctetString("cpublic"), // security name
getAgent().getContextEngineID(), // local engine ID
new OctetString("public"), // default context name
new OctetString(), // transport tag
new Integer32(StorageType.nonVolatile), // storage type
new Integer32(RowStatus.active) // row status
};
MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow(
new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}

public void runAgent() throws IOException, InterruptedException {
while(!STOP) {
System.out.println("Agent running...");
Thread.sleep(5000);
}
}

}

这是我的 snmp 测试仪打印输出

[Client] Target address : udp:127.0.0.1/162 [Client] Send PDU : GET[requestID=1075357385, errorStatus=Success(0), errorIndex=0, VBS[1.2.3.4.5.6.7 = Null]] [Client] RESPONSE from 127.0.0.1/162 : noSuchObject

这是代理端的一些打印输出

WARN : org.snmp4j.agent.io.MOServerPersistence - Context 'public' is no longer supported by agent INFO : org.snmp4j.agent.DefaultMOServer - Registered MO StaticMOGroup[root=1.2.3.4.5.6.7,vbs={}] in default context with scope org.snmp4j.agent.DefaultMOScope[lowerBound=1.2.3.4.5.6.7,lowerIncluded=true,upperBound=1.2.3.4.5.6.8,upperIncluded=false] INFO : org.snmp4j.agent.mo.snmp.NotificationOriginatorImpl - Notification 1.3.6.1.6.3.1.1.5.1 reported with [] for context
Agent running... Agent running... Agent running... Agent running... Agent running... Agent running...

我做得对吗?哦,顺便说一句,我正在使用 snmp4j-Agent 库。如果您需要更多信息,请告诉我。

问候,西蒙

最佳答案

作为旁注,有一个具有非常相似功能的 pythonic 工具:

http://snmpsim.sf.net

关于java - 如何使用 snmp4j 代理创建自己的 snmp 模拟器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com