1. 程式人生 > >[go]golang snmp測試(1)

[go]golang snmp測試(1)

golang發展挺快的,現在都已經1.9了。

現在也有了相對完備和可用的snmp庫。

目前支援snmpv3的主要有2個庫,star都很少,看起來用snmp的人不多啊。

github.com/soniah/gosnmp   293star

github.com/k-sone/snmpgo   73star

先分別測試一下

snmpv2

package main

import (
	"fmt"
	"log"

	g "github.com/soniah/gosnmp"
)

func main() {

	// Default is a pointer to a GoSNMP struct that contains sensible defaults
	// eg port 161, community public, etc
	g.Default.Target = "192.168.1.10"
	err := g.Default.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer g.Default.Conn.Close()

	oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
	result, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}

	for i, variable := range result.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case g.OctetString:
			fmt.Printf("string: %s\n", string(variable.Value.([]byte)))
		default:
			// ... or often you're just interested in numeric values.
			// ToBigInt() will return the Value as a BigInt, for plugging
			// into your calculations.
			fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))
		}
	}
}
package main

import (
    "fmt"

    "github.com/k-sone/snmpgo"
)

func main() {
    snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
        Version:   snmpgo.V2c,
        Address:   "127.0.0.1:161",
        Retries:   1,
        Community: "public",
    })
    if err != nil {
        // Failed to create snmpgo.SNMP object
        fmt.Println(err)
        return
    }

    oids, err := snmpgo.NewOids([]string{
        "1.3.6.1.2.1.1.1.0",
        "1.3.6.1.2.1.1.2.0",
        "1.3.6.1.2.1.1.3.0",
    })
    if err != nil {
        // Failed to parse Oids
        fmt.Println(err)
        return
    }

    if err = snmp.Open(); err != nil {
        // Failed to open connection
        fmt.Println(err)
        return
    }
    defer snmp.Close()

    pdu, err := snmp.GetRequest(oids)
    if err != nil {
        // Failed to request
        fmt.Println(err)
        return
    }
    if pdu.ErrorStatus() != snmpgo.NoError {
        // Received an error from the agent
        fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
    }

    // get VarBind list
    fmt.Println(pdu.VarBinds())

    // select a VarBind
    fmt.Println(pdu.VarBinds().MatchOid(oids[0]))
}

snmpv3

package main

import (
	"fmt"
	"log"
	"time"

	g "github.com/soniah/gosnmp"
)

func main() {

	// build our own GoSNMP struct, rather than using g.Default
	params := &g.GoSNMP{
		Target:        "192.168.91.20",
		Port:          161,
		Version:       g.Version3,
		Timeout:       time.Duration(30) * time.Second,
		SecurityModel: g.UserSecurityModel,
		MsgFlags:      g.AuthPriv,
		SecurityParameters: &g.UsmSecurityParameters{UserName: "user",
			AuthenticationProtocol:   g.SHA,
			AuthenticationPassphrase: "password",
			PrivacyProtocol:          g.DES,
			PrivacyPassphrase:        "password",
		},
	}
	err := params.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer params.Conn.Close()

	oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
	result, err2 := params.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}

	for i, variable := range result.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case g.OctetString:
			fmt.Printf("string: %s\n", string(variable.Value.([]byte)))
		default:
			// ... or often you're just interested in numeric values.
			// ToBigInt() will return the Value as a BigInt, for plugging
			// into your calculations.
			fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))
		}
	}
}
package main

import (
	"fmt"

	"github.com/k-sone/snmpgo"
)

func main() {
	snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
		Version:       snmpgo.V3,
		Address:       "127.0.0.1:161",
		Retries:       1,
		UserName:      "MyName",
		SecurityLevel: snmpgo.AuthPriv,
		AuthPassword:  "aaaaaaaa",
		AuthProtocol:  snmpgo.Sha,
		PrivPassword:  "bbbbbbbb",
		PrivProtocol:  snmpgo.Aes,
	})
	if err != nil {
		// Failed to create snmpgo.SNMP object
		fmt.Println(err)
		return
	}

	oids, err := snmpgo.NewOids([]string{
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.3.0",
	})
	if err != nil {
		// Failed to parse Oids
		fmt.Println(err)
		return
	}

	if err = snmp.Open(); err != nil {
		// Failed to open connection
		fmt.Println(err)
		return
	}
	defer snmp.Close()

	pdu, err := snmp.GetRequest(oids)
	if err != nil {
		// Failed to request
		fmt.Println(err)
		return
	}
	if pdu.ErrorStatus() != snmpgo.NoError {
		// Received an error from the agent
		fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
	}

	// get VarBind list
	fmt.Println(pdu.VarBinds())

	// select a VarBind
	fmt.Println(pdu.VarBinds().MatchOid(oids[0]))
}

兩個都差不多

相比而言,後者看起來的完成度要更高一些,結果值的處理也要友好一些。