gpt4 book ai didi

java - 如何使用 snmp4j-2.7.0 获取 snmpv3 中的系统信息

转载 作者:行者123 更新时间:2023-12-02 05:25:14 26 4
gpt4 key购买 nike

我正在尝试创建小应用程序来管理 snmp 设备,首先我想获取系统信息。

我使用 snmp4j 版本 2.7.0、java 1.8,设备使用 snmpv3。首先我想同步获取数据。我尝试用两种方式做到这一点,但我总是超时

这是我的代码

    //Normal request
public class SynchronouslySnmp
{
protected String PORT="/161";
protected String PROTOCOL="udp:";
protected SnmpData snmpData;
protected Snmp snmp;

public SynchronouslySnmp(SnmpData snmpData)
{
this.snmpData=snmpData;

}

public void start()
{
createSNMPsession();
addSnmpUser();
sendSnmp();
}


private void sendSnmp()
{
// send the PDU
ResponseEvent response;
try
{
response = snmp.send(getPDU(), getTarget());
// extract the response PDU (could be null if timed out)
PDU responsePDU = response.getResponse();
// extract the address used by the agent to send the response:
if(responsePDU == null)
{
System.out.println("ERROR: table OID [" + SnmpContants.system + "] time out" );
}
else
{
Address peerAddress = response.getPeerAddress();
System.out.println("pdu: "+responsePDU.toString());
System.out.println("Address: "+peerAddress.toString());
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
snmp.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

protected void createSNMPsession()
{
TransportMapping<? extends Address> transport;
try
{
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
//snmp.listen();
}
catch (IOException e)
{
e.printStackTrace();
}
}

protected void addSnmpUser()
{
// add user to the USM
snmp.getUSM().addUser(new OctetString(snmpData.getUsmUser()),
new UsmUser(new OctetString(snmpData.getUsmUser()),
snmpData.getAuthProtocol(),
new OctetString(snmpData.getAuthPassword()),
snmpData.getPrivProtocol(),
new OctetString(snmpData.getPrivPassword())));
}

protected Target getTarget()
{
// create the target
UserTarget target = new UserTarget();
Address targetAddress = GenericAddress.parse(PROTOCOL+snmpData.getIpAddress()+PORT);
target.setAddress(targetAddress);
target.setRetries(snmpData.getRetryTimes());
target.setTimeout(snmpData.getTimeout());
target.setVersion(snmpData.getSnmpVersion());
target.setSecurityLevel(snmpData.getSecurityLevel());
target.setSecurityName(new OctetString(snmpData.getUsmUser()));
return target;
}

protected PDU getPDU()
{
// create the PDU
PDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(SnmpConstants.system));
pdu.setType(PDU.GETBULK);
return pdu;
}
}

我尝试使用treeUtil,但遇到了与正常请求中相同的错误,以下是我的代码

    public class SynchronouslySnmpUsingTree extends SynchronouslySnmp
{
private OID tableOid;
private LinkedHashMap<String, List<VariableBinding>> resultMap;

public SynchronouslySnmpUsingTree(SnmpData snmpData)
{
super(snmpData);
resultMap = new LinkedHashMap<String, List<VariableBinding>>();
tableOid=new OID(".1.3.6.1.2.1.1");
createSNMPsession();
addSnmpUser();
sendSnmp();
}

private void sendSnmp()
{
try
{
TreeUtils treeUtils = new TreeUtils(snmp, new PDUFactory()
{
@Override
public PDU createPDU(MessageProcessingModel arg0) {
return getPDU();
}

@Override
public PDU createPDU(Target arg0) {

return getPDU();
}
});
List<TreeEvent> events = treeUtils.getSubtree(getTarget(), tableOid);
if (events == null || events.size() == 0)
{
System.out.println("Error: Unable to read table...");
return;
}
for (TreeEvent event : events)
{
if (event != null && !event.isError())
{
VariableBinding[] varBindings = event.getVariableBindings();
if (varBindings != null && varBindings.length != 0)
{
for (VariableBinding varBinding : varBindings)
{
if (varBinding != null)
{
OID oid = varBinding.getOid();
List<VariableBinding> binds = resultMap.get(oid.toString());
if( binds == null)
{
binds = new ArrayList<VariableBinding>();
resultMap.put(oid.toString(), binds);
}
binds.add(varBinding);
}
}
}
}
else
{
System.out.println("Error: table OID [" + tableOid + "] " + event.getErrorMessage());
continue;
}
}
}
catch(Throwable t)
{
System.out.println("Failed operation: getMibTableWithNext");
t.printStackTrace();
}
finally
{
try
{
snmp.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

主要功能

public static void main(String[] args){
SynchronouslySnmp synchronouslySnmp =new SynchronouslySnmp (new SnmpData());
synchronouslySnmp.start();
SynchronouslySnmpUsingTree synchronouslySnmpUsingTree = new SynchronouslySnmpUsingTree(new SnmpData());
}

程序运行结果

Error: table OID [1.3.6.1.2.1.1] time out); <---nurmal
Error: table OID [1.3.6.1.2.1.1] Request time out); <---tree

我查看https://www.snmp4j.org/html/faq.html

Why am I always getting a time-out (response == null) when sending a request?

Probably you have forgotten to call the listen() method of the TransportMapping (once) or the Snmp class before sending the request.

但我在 createSNMPsession() 方法中这样做那么我做错了什么?谢谢。 :)

最佳答案

所以,在我绞尽脑汁一整天之后,答案附在任何需要知道如何将 SNMPV3 与 SNMP4J 一起使用的人身上。

让我们从正常请求开始。问题是我将 PDU 与 GETBULK 一起使用,但我没有设置最大重复次数,因此默认值为 0,并且我得到了空 VBS

要修复它只需使用:

protected PDU getPDU() 
{
// create the PDU
PDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(SnmpConstants.system));
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(10);//or any number you wish
return pdu;
}

对于树问题,不需要定义最大重复次数,这是 getSubTree 的全部要点。所以问题是我将超时(以秒为单位)设置为 30 而不是毫秒为 30000 愚蠢的错误。但后来我得到了更大的错误“代理没有按字典顺序返回变量绑定(bind)”。我用墙来修复它:

treeUtils.setIgnoreLexicographicOrder(true);
List<TreeEvent> events = treeUtils.getSubtree(getTarget(), tableOid);
if (events == null || events.size() == 0)
{
System.out.println("Error: Unable to read table...");
return;
}
.
.
.
if( binds == null)
{
binds = new ArrayList<VariableBinding>();
binds.add(varBinding);
resultMap.put(oid.toString(), binds);
}

关于java - 如何使用 snmp4j-2.7.0 获取 snmpv3 中的系统信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237216/

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