2016-03-07 66 views
0

我试图做一个程序,发送SNMP查询到网络中的一些交换机。使用代理名称,而不是IP作为SNMP4J中的地址

使用Net-snmp工具,我可以使用名称向交换机发送获取请求,并且工作正常。但SNMP4J需要IP地址CommunityTarget,所以我得到IllegalArgumentException

这是代码的相关部分:

TransportMapping transport = new DefaultUdpTransportMapping(); 
transport.listen(); 

CommunityTarget comtarget = new CommunityTarget(); 
comtarget.setCommunity(new OctetString("public")); 
comtarget.setVersion(SnmpConstants.version1); 
comtarget.setAddress(new UdpAddress("switchName")); // exception happens here 
comtarget.setRetries(2); 
comtarget.setTimeout(1000); 

我如何解决此问题?

回答

0

您可以通过使用DNS解析获得的IP地址,这样answer说:

InetAddress address = InetAddress.getByName(switchName); 
System.out.println(address.getHostAddress()); 
相关问题