douzui6173 2017-05-29 10:56
浏览 205

gosnmp从SNMP PDU获取八位字节串

I am trying to get the OctetString value from an SNMP PDU returned by the gosnmp package. Even the bytes would suffice.

Here is my code:

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
", 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
", 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)
    }
}

When I use the MapBPIIfindex method I get the following output:

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}}

Now, this should contain an OctetString. The uint8 bytes should decode to Fa0/1, but I am not able to do this.

When I change log.Println(p.Value) to log.Println(p.Value.([]uint8)), I get the following error:

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

How can I get this value? The documentation is not so clear on this.

  • 写回答

1条回答 默认 最新

  • dsgoj7457 2017-05-29 21:21
    关注

    this is ASCII. You need to convert your HEX output. I don't have any knowledge of GO. https://en.wikipedia.org/wiki/ASCII

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题