0

I'm trying to make a program that sends SNMP queries to some switches in the network.

Using the Net-snmp tools, I can send get requests to the switch using its name, and it works fine. But SNMP4J requires an IP address in CommunityTarget, so I get an IllegalArgumentException.

This is the relevant part of the code:

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);

How can I work around this?

dluga
  • 67
  • 1
  • 8

1 Answers1

0

You can get the IP address by using DNS resolution, like this answer says:

InetAddress address = InetAddress.getByName(switchName); 
System.out.println(address.getHostAddress());
Community
  • 1
  • 1
dluga
  • 67
  • 1
  • 8