Go 编码SNMP消息并从代理读取响应
我正在尝试使用Go向客户机发送一个非常基本的SNMP Get请求。我有一条SNMP消息,用于获取OIDGo 编码SNMP消息并从代理读取响应,go,network-programming,udp,snmp,asn.1,Go,Network Programming,Udp,Snmp,Asn.1,我正在尝试使用Go向客户机发送一个非常基本的SNMP Get请求。我有一条SNMP消息,用于获取OID1.3.6.1.2.1.1,我根据以及手动将其编码到字节片中。我可以将消息写入主机,但无法读取响应(或未收到响应)。从net.Conn读取时,程序挂起。这是否意味着根本没有回应?或者我尝试发送的SNMP消息不正确 这是我的密码: package main import ( "fmt" "io/ioutil" "log" "net" "time" ) f
1.3.6.1.2.1.1
,我根据以及手动将其编码到字节片中。我可以将消息写入主机,但无法读取响应(或未收到响应)。从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))
}
我还不需要解码响应,我更关心的是获取和阅读响应。有什么建议吗?如果你可以运行Wireshark在网络上收听你的请求和预期的响应,我想这应该会为你解决问题 如果您的请求格式不正确,您应该看到Wireshark对此进行投诉。如果您的SNMP代理由于某种原因没有响应,您将无法在线路上看到响应
在某些设置中,响应可能来自/到不同的IP地址-这可能是超时的原因。另外一个原因可能是社区名称错误。除了Ilya的答案之外,如果SNMP代理具有良好的诊断功能,它可能会为您提供一个不回答的原因 如果没有,那么先找一个合适的代理进行测试,解决最初的问题