【问题标题】:Encoding SNMP Message and Reading Response From Agent编码 SNMP 消息并从代理读取响应
【发布时间】:2018-07-19 19:00:06
【问题描述】:

我正在尝试使用 Go 向客户端发送一个非常基本的 SNMP Get 请求。我有一条用于获取 OID 1.3.6.1.2.1.1 的 SNMP 消息,我根据 this postsoniah/gosnmp 手动将其编码为字节片。我能够将消息写入主机,但无法读取响应(或未收到响应)。从net.Conn 读取时程序挂起。这是否意味着根本没有回应?或者我尝试发送的 SNMP 消息不正确?

这是我的代码:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net"
    "time"
)

func main() {
    conn, err := net.DialTimeout("udp", "host:161", 5*time.Second)
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    fmt.Println(conn.RemoteAddr())

    var snmpMsg []byte

    // Type: Sequence, Length: 37
    msg := []byte{0x30, 0x25}
    snmpMsg = append(snmpMsg, msg...)

    // Type: Integer, Len: 1, Val: 1 (SNMP Version 2c)
    version := []byte{0x02, 0x01, 0x1}
    snmpMsg = append(snmpMsg, version...)

    // Type: Octet String, Len: 7, Val: "private"
    community := []byte{0x04, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65}
    snmpMsg = append(snmpMsg, community...)

    // Type: GetRequest, Len: 23
    pdu := []byte{0xa0, 0x17}
    snmpMsg = append(snmpMsg, pdu...)

    // Type: Integer, Len: 1, Val: 1 (Is the RequestID value supposed to be something specific?)
    requestID := []byte{0x02, 0x01, 0x01}
    snmpMsg = append(snmpMsg, requestID...)

    // Type: Integer, Len: 1, Val: 0
    errStatus := []byte{0x02, 0x01, 0x00}
    snmpMsg = append(snmpMsg, errStatus...)

    // Type: Integer, Len: 1, Val: 0
    errIndex := []byte{0x02, 0x01, 0x00}
    snmpMsg = append(snmpMsg, errIndex...)

    // Type: Sequence, Len: 12
    varBindList := []byte{0x30, 0x0c}
    snmpMsg = append(snmpMsg, varBindList...)

    // Type: Sequence, Len: 10
    varBind := []byte{0x30, 0x0a}
    snmpMsg = append(snmpMsg, varBind...)

    // Type: Object Identifier, Len: 6, Val: 1.3.6.1.2.1.1
    oid := []byte{0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01}
    snmpMsg = append(snmpMsg, oid...)

    // Type: Null, Len: 0
    value := []byte{0x05, 0x00}
    snmpMsg = append(snmpMsg, value...)

    n, err := conn.Write(snmpMsg)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Message size: %d, Bytes written: %d\n", len(snmpMsg), n)

    // Program hangs here; does the same with `conn.Read()`.
    b, err := ioutil.ReadAll(conn)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(b))
}

我还不需要解码响应,我更关心的是获取和读取响应。有什么建议吗?

【问题讨论】:

    标签: go network-programming udp snmp asn.1


    【解决方案1】:

    如果您可以运行 Wireshark 在网络上侦听您的请求和预期的响应,我认为这应该会为您解决问题。

    如果您的请求格式不正确,您应该会看到 Wireshark 对此进行投诉。如果您的 SNMP 代理由于某种原因没有响应 - 您将不会在线上看到响应。

    在某些设置中,响应可能来自/来自不同的 IP 地址 - 这可能是超时的原因。错误的社区名称可能是另一个原因。

    【讨论】:

      【解决方案2】:

      除了 Ilya 的回答之外,如果 SNMP 代理有良好的诊断,它可能会给你一个它没有回答的原因。

      如果没有,请先使用体面的代理进行测试,以解决最初的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 2017-02-15
        • 1970-01-01
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多