【发布时间】:2013-11-02 01:23:35
【问题描述】:
我正在编写一个通过 UDP 端口 161 连接到网络服务器的 snmp 客户端。它将发送一个 ASN 格式的消息并接收来自服务器的响应。我已经尝试了所有我能想到的方法,但我似乎无法让它正常工作。
程序应该在cmd中接收并解析出以下命令返回的计数器字段:
snmpget -d -v 2c -c public 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); 他通过相同十六进制代码的代码运行良好。
我唯一能想到的是我没有正确格式化、发送或收听,但是在翻阅文档和示例之后,我什么也想不通。有没有人指点一下?
【问题讨论】:
-
如果您可以访问教授的示例,请尝试运行它以及您自己的示例,并使用 Wireshark 或 tcpdump 之类的工具来查看在这两种情况下实际发送的内容.你可以看到你的程序发送了什么不同的东西,如果有任何东西被发送出去。
-
我必须通过 linux 服务器上的 putty 远程运行教授的示例,但我不知道有什么方法可以在上面安装wireshark。我对传出的数据包进行了捕获,一切看起来都非常正确(目的地很好,端口是 161,消息体很好,等等),但是我没有从 Wireshark 中的服务器得到任何响应。我不知道发生了什么。
-
你可以在Linux服务器上试试tcpdump。它不是用户友好的,但它仍然非常简单。快速谷歌或查看手册页会告诉你如何使用它。
-
显然,将我的 HEX 字符串转换为 byte[] 时出现问题,但我的数据包至少已被运送到正确的地方。编辑 - 我目前正在将我的十六进制字符串转换为字节 [],然后再次转换回字符串并将其与原始十六进制字符串进行比较并且它匹配,所以我回到了第 1 格。