我有一个 IP 地址 jlist 和一个保存 jlist 中所选值的方法
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
s=(String)list.getSelectedValue();
}
});
我设置了按钮监听器
infoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
SNMP client = new SNMP();
client.snmpGet(ServerFrame.s,"COMMUNAUTE_SERVEUR",".1.3.6.1.2.1.1.5.0");
client.snmpGet(ServerFrame.s,"COMMUNAUTE_SERVEUR","1.3.6.1.2.1.1.1.0");
client.snmpGet(ServerFrame.s,"COMMUNAUTE_SERVEUR",".1.3.6.1.2.1.1.3.0");
}
});
此行 client.snmpGet(ServerFrame.s,"COMMUNAUTE_SERVEUR",".1.3.6.1.2.1.1.5.0");
会导致此错误:
java.lang.IllegalArgumentException: 192.168.1.64 /161
at org.snmp4j.smi.UdpAddress.(Unknown Source)
这是我在按钮监听器中调用的 snmpget
方法
public String snmpGet(String host, String community, String strOID) {
String strResponse="";
ResponseEvent response;
Snmp snmp;
try {
OctetString community1 = new OctetString(community);
host= host+"/"+"161";
Address tHost = new UdpAddress(host);
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(community1);
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(tHost);
comtarget.setRetries(2);
comtarget.setTimeout(5000);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(strOID)));
pdu.setType(PDU.GET);
snmp = new Snmp(transport);
response = snmp.get(pdu,comtarget);
if(response != null) {
if(response.getResponse().getErrorStatusText().equalsIgnoreCase("Success")) {
PDU pduresponse=response.getResponse();
strResponse=pduresponse.getVariableBindings().firstElement().toString();
if(strResponse.contains("=")) {
String strNewResponse = null;
int len = strResponse.indexOf("=");
strNewResponse=strResponse.substring(len+1, strResponse.length());
System.out.println("The SNMP response to the OID requested is: " + strNewResponse);
}
}
} else {
System.out.println("Looks like a TimeOut occured ");
}
snmp.close();
} catch(Exception e) {
e.printStackTrace();
}
//System.out.println("Response="+strResponse);
return strResponse;
}
有关此错误的任何帮助,我明白,问题似乎出在 s 变量
最佳答案
是我一个人的问题,还是 IllegalArgumentException 在您的 IP 地址中显示了额外的空格: java.lang.IllegalArgumentException:192.168.1.64/161
尝试添加 .trim() 调用以删除多余的空格:
更改
host= host+"/"+"161";
至
host= host.trim() + "/" + 161;
看看是否可以避免 IllegalArgumentException
关于java - snmpget 出现 IllegalArgumentException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18362135/