【问题标题】:problems with UDP snmp client JavaUDP snmp客户端Java的问题
【发布时间】: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 格。

标签: java snmp udpclient


【解决方案1】:

如果您将问题范围缩小到十六进制字符串到字节数组的转换,那就去吧:

在 Java 中将十六进制字符串转换为字节数组

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

【讨论】:

  • 实际上,我试过了(注意我对那个方法的函数调用),但它是不正确的。我的一些数据字节被该方法错误地转换(尽管我不太确定为什么,此时我并不在乎)。我最终只使用了 'DatatypeConverter.parseHexBinary(str);'获取十六进制字符串的 byte[] 表示。
【解决方案2】:

我想通了。我用来将十六进制字符串转换为 byte[] 的方法不太正确。我改用内置转换器“DatatypeConverter.parseHexBinary(str);”而且效果很好。

【讨论】:

  • 当我在服务器上运行它时,我得到了某种响应,但我似乎无法解压缩它以了解它。这台机器上似乎没有安装 tcpdump,所以我很难弄清楚数据包中实际发送的是什么。这是我得到的输出:'java.net.DatagramPacket@(7 or 8 char length hex string)' 其中十六进制字符串似乎是随机的,无论我的消息是否唯一或 0)public*square*3 *square*00 + PuTTy 我可以使用 receivedPacket.getData().toString() 提取某种数字,但它似乎不正确。
猜你喜欢
  • 2011-09-30
  • 1970-01-01
  • 2020-01-19
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多