【问题标题】:gosnmp get octetstring from SNMP PDUgosnmp 从 SNMP PDU 获取八位字节串
【发布时间】:2017-05-29 10:56:51
【问题描述】:

我正在尝试从 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

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

【问题讨论】:

  • 如果您将值 {0x46, 0x61, 0x30, 0x2f, 0x31} 确定为 ASCII 字符,它会解码为“Fa0/1”。您可能只需要进行类型转换即可将其转换为 String。

标签: go snmp gosnmp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    相关资源
    最近更新 更多