gosnmp 从 SNMP PDU 中获取八位字节串

标签 go snmp gosnmp

我正在尝试从 gosnmp 包返回的 SNMP PDU 中获取 OctetString 值。即使是字节也足够了。

这是我的代码:

package snmp_abstract

import (
    "github.com/soniah/gosnmp"
    "time"
    "log"
    "os"
    "strings"
)

type Switch struct {
    Hostname string
    Connection gosnmp.GoSNMP
}


var ConnectionParams = &gosnmp.GoSNMP{
    Target: "",
    Port: 161,
    Community: "community",
    Version: gosnmp.Version2c,
    Timeout: time.Duration(5) * time.Second,
    Logger: log.New(os.Stdout, "", 0),
}

type Mibs struct {
    VtpVlanState,
    Dot1dBasePortIfIndex string
}

var Default = &Mibs{
    VtpVlanState: "1.3.6.1.4.1.9.9.46.1.3.1.1.2",
    Dot1dBasePortIfIndex: "1.3.6.1.2.1.17.1.4.1.2",
}


func SNMPGet(conn *gosnmp.GoSNMP, host string, mib []string) {
    conn.Target = host
    log.Println(conn)
    err := conn.Connect()
    if err != nil {
        log.Printf("Unable to connect to %s\n", host)
    }
    defer conn.Conn.Close()
    res, err := conn.Get(mib)
    if err != nil {
        log.Println("GET error")
        log.Print(err)
    }
    log.Println(res)
}


func SNMPWalk(conn *gosnmp.GoSNMP, host string, mib string) []gosnmp.SnmpPDU {
    conn.Target = host
    log.Println(conn)
    err := conn.Connect()
    if err != nil {
        log.Printf("Unable to connect to %s\n", host)
    }
    defer conn.Conn.Close()
    res, err := conn.BulkWalkAll(mib)
    if err != nil {
        log.Println("GET error")
        log.Print(err)
    }
    return res
}

func (sw *Switch) Vlans() []string {

    res := SNMPWalk(&sw.Connection, sw.Hostname, Default.VtpVlanState)
    var vlans = make([]string, len(res))
    for i, vlan := range res {
        oidSlice := strings.Split(vlan.Name, ".")
        v := oidSlice[len(oidSlice)-1]
        vlans[i] = v
    }
    return vlans
}

func (sw *Switch) MapBPIIfindex(vlan string)  {
    log.Println(vlan)
    s := *sw
    s.Connection.Community += "@" + vlan
    log.Println(s.Connection.Community)
    res := SNMPWalk(&s.Connection, s.Hostname, Default.Dot1dBasePortIfIndex)
    for _, p := range res {
        log.Println(p.Name)
        log.Println(p.Value)
    }
}

当我使用 MapBPIIfindex 方法时,我得到以下输出:

OID: [.1.3.6.1.2.1.31.1.1.1.1.10001]
[decodeValue: type is OctetString]
decodeValue: value is []interface {}{[]uint8{0x46, 0x61, 0x30, 0x2f, 0x31}}

现在,这应该包含一个 OctetString。 uint8 字节应解码为 Fa0/1,但我无法执行此操作。

当我将 log.Println(p.Value) 更改为 log.Println(p.Value.([]uint8)) 时,出现以下错误:

2017/05/29 12:54:59 .1.3.6.1.2.1.17.1.4.1.2
panic: interface conversion: interface {} is nil, not []uint8

我怎样才能得到这个值?文档对此不是很清楚。

最佳答案

这是 ASCII。您需要转换 HEX 输出。 我对GO一无所知。 https://en.wikipedia.org/wiki/ASCII

关于gosnmp 从 SNMP PDU 中获取八位字节串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44240806/

相关文章:

c - 使用 net-snmp C API 设置值时出现问题

go - GoSNMP 中的对象名称而不是 OID

pointers - Go 使用指针接收者调用函数的语法

Golang http请求连续发出多个请求时导致EOF错误

go - Go Server无需重启的无缝补丁部署

arrays - 如何将字符串编码的 float 数组解码为 float 数组?

linux - 与 snmp 陷阱混淆

python - 如何在 Python 中解析 MIB 并检索没有 OID 的 TEXTUAL-CONVENTION