SNMP OID 的使用方法

SNMP OID的使用方法

**Get:**支持单个或者多个oid,获取结果为固定oid的一对一值,例如:品牌/型号/转发功能/

[root@es-9 ~]# snmpwalk -v 2c -c public123 192.168.40.2  .1.3.6.1.2.1.1.2.0
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.2011.2.23.426
ObjectIdentifier .1.3.6.1.4.1.2011.2.23.426 ObjectIdentifier

**GetBulk:**支持单个或者多个oid,获取结果为某个OID下面的所有子数的值,例如: 序列号

[root@es-9 ~]# snmpwalk -v 2c -c public123 192.168.40.2 .1.3.6.1.2.1.47.1.1.1.1.11
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67108867 = STRING: "21980106012SHB602060"
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67108869 = ""
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67108873 = STRING: "21980106012SHB602060"
SNMPv2-SMI::mib-2.47.1.1.1.1.11.67125260 = ""
OctetString  21980106012SHB602060 OctetString

WalkAll和BulkWalkAll:

使用方法一样,BulkWalkAll,只是使用了更为高效的GetBulk操作

传入参数单个OID,获取单个OID下面所有的同类信息的值,获取设备端口数量

[root@es-9 ~]# snmpwalk -v 2c -c public123 192.168.40.2 .1.3.6.1.2.1.2.2.1.2
IF-MIB::ifDescr.1 = STRING: InLoopBack0
IF-MIB::ifDescr.2 = STRING: NULL0
IF-MIB::ifDescr.3 = STRING: Console9/0/0
IF-MIB::ifDescr.4 = STRING: Vlanif1
IF-MIB::ifDescr.5 = STRING: GigabitEthernet0/0/1
IF-MIB::ifDescr.6 = STRING: GigabitEthernet0/0/2
IF-MIB::ifDescr.7 = STRING: GigabitEthernet0/0/3
OctetString  Vlanif20

**Walk:**暂未用到,需要WalkFunc这个参数,retrieves a subtree of values using GETNEXT.

**BulkWalk:**暂未用到、需要WalkFunc这个参数, retrieves a subtree of values using GETBULK

用例:获取设备端口数量

err2 := s.client.Walk(".1.3.6.1.2.1.2.2.1.2",printValue)
if err2 != nil {
	fmt.Println("GetModeV2 snmp conn switch v3, info: %v", err2)
}
	
func printValue(pdu g.SnmpPDU) error {
	fmt.Printf("%s = ", pdu.Name)

	switch pdu.Type {
	case g.OctetString:
		b := pdu.Value.([]byte)
		fmt.Printf("STRING: %s\n", string(b))
	default:
		fmt.Printf("TYPE %d: %d\n", pdu.Type, g.ToBigInt(pdu.Value))
	}
	return nil
}

其他方法:

GetNext:暂未用到

测试代码

package main

import (
	"fmt"
	g "github.com/soniah/gosnmp"
	"strconv"
	"strings"
	"time"
)

func main() {
	oids := []string{".1.3.6.1.2.1.1.2.0"}
	s := NewSnmpClientV2("192.168.40.2", "public123", uint16(161))

	err1 := s.client.Connect()
	if err1 != nil {
		fmt.Println("GetModeV2 snmp connect failure,err: %v", err1.Error())
	}
	defer s.client.Conn.Close()

	res, err2 := s.client.Get(oids)
	if err2 != nil {
		fmt.Println("GetModeV2 snmp conn switch v3, info: %v", err2)
	} else {
		for _, v := range res.Variables {
			switch v.Type {
			case g.ObjectIdentifier:
				fmt.Println(v.Value.(string), "ObjectIdentifier")
			case g.OctetString:
				tempStr := strings.Split(string(v.Value.([]uint8)), "\r")
				fmt.Println(tempStr[0], "OctetString")
			case g.Integer:
				temp := v.Value.(int)
				fmt.Println(strconv.Itoa(temp), "g.Integer")
			}
		}
	}
}

func NewSnmpClientV2(target, community string, port uint16) *SnmpClient {
	return &SnmpClient{
		g.GoSNMP{
			Target:         target,
			Community:      community,
			Port:           port,
			Version:        g.Version2c,
			Timeout:        time.Duration(2) * time.Second,
			Retries:        1,
			MaxOids:        1,
			MaxRepetitions: 2,
			Logger:         nil,
		},
	}
}

type SnmpClient struct {
	client g.GoSNMP
}

你可能感兴趣的:(Go开发)