我正在编写一个通过 UDP 端口 161 连接到网络服务器的 snmp 客户端。它将发送一个 ASN 格式的消息并接收来自服务器的响应。我已经尝试了我能想到的一切,但我似乎无法让它正常工作。
该程序应该在 cmd 中接收并解析从以下命令返回的计数器字段:
snmpget -d -v 2c -c 公共 129.130.10.43 ip.ipInReceives.0
//命令结果:
{
向 UDP 发送 43 个字节:[129.130.10.43]:161->[0.0.0.0]
0000: 30 29 02 01 01 04 06 70 75 62 6C 69 63 A0 1C 02 0).....公共...
0016: 04 28 6A DF 1F 02 01 00 02 01 00 30 0E 30 0C 06 .(j........0.0..
0032:08 2B 06 01 02 01 04 03 00 05 00 .+......
从 UDP 接收到 47 个字节:[129.130.10.43]:161->[0.0.0.0]
0000: 30 2D 02 01 01 04 06 70 75 62 6C 69 63 A2 20 02 0-.....公开。.
0016: 04 28 6A DF 1F 02 01 00 02 01 00 30 12 30 10 06 .(j........0.0..
0032: 08 2B 06 01 02 01 04 03 00 41 04 1B 49 0C 95 .+........A..I..
IP-MIB::ipInReceives.0 = Counter32: 457772181
}
//我的asn消息格式说明:
保存在 byte[] 数组中并传递给 udp 服务器的十六进制值:
30 29 02 01 01 04 06 70 75 62 6C 69 63 A0 1C 02
04 XX XX XX XX 02 01 00 02 01 00 30 0E 30 0C 06
08 2B 06 01 02 01 04 03 00 05 00
XX 表示请求 ID XX 将为每个请求更改(可能是随机的)以创建唯一的请求 ID
代码:
public static void snmpMessage() throws SocketException
{
DatagramSocket socket = null;
byte[] serverResponse = new byte[1024];
InetAddress addy = null;
byte[] hex = hexStringToByteArray("302902010004067075626C6963A01C0204121533EA020100020100300E300C06082B060102010403000500"); //note: this hex string is the exact string from the example program, so I know the string is correct
addy = InetAddress.getByName("129.130.10.48"); //server IP address
socket= new DatagramSocket();
//method that creates unique snmp asn message replacing the XX's with appropriate non-negative hex values
hex = messageGenerator();
//Data reproduction from byte[] array
System.out.println("byte array integer format:\n" + Arrays.toString(hex)); //correctly prints out integer values
System.out.println("\nbyte array length: " + hex.length); //Correctly prints length of sending array (43)
System.out.println("\nserver name: " + addy.getCanonicalHostName()); //correctly prints server name
String hex2 = toHex(hex);
System.out.println("\nbyte array in hex format: \n" + hex2); //correctly reproduces original hex string from byte[] array
//at this point, I can only assume that my data is stored correctly
//send byte array to server
DatagramPacket sendPacket = new DatagramPacket(hex, hex.length, addy, 161);
socket.send(sendPacket);
//get server's response
DatagramPacket receivePacket = new DatagramPacket(serverResponse, serverResponse.length);
System.out.println("\nWaiting for server response...\n"); //prints out
socket.receive(receivePacket);
System.out.println("Response received"); //does not print
}
我的教授发布了一个示例,他只是将指针传递给 char 字符串: sendto (sockfd, (char *) s, 43, 0, (struct sockaddr *) &serv_addr, servlen); 他通过相同十六进制代码的代码运行良好。
我唯一能想到的就是我没有正确格式化、发送或收听,但是在翻阅文档和示例之后,我什么也想不通。有没有人有一些指示?