Last active
September 13, 2019 11:12
-
-
Save kevsersrca/be08e046fdc87031191d1ba84ed78bfd to your computer and use it in GitHub Desktop.
Golang Snmp Trials 🐰
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"time" | |
"gopkg.in/errgo.v1" | |
"github.com/posteo/go-agentx" | |
"github.com/posteo/go-agentx/pdu" | |
"github.com/posteo/go-agentx/value" | |
) | |
func main() { | |
client := &agentx.Client{ | |
Net: "tcp", | |
Address: "localhost:161", | |
Timeout: 1 * time.Minute, | |
ReconnectInterval: 1 * time.Second, | |
} | |
if err := client.Open(); err != nil { | |
log.Fatalf(errgo.Details(err)) | |
} | |
session, err := client.Session() | |
if err != nil { | |
log.Fatalf(errgo.Details(err)) | |
} | |
listHandler := &agentx.ListHandler{} | |
item := listHandler.Add("1.3.6.1.4.1.45995.3.1") | |
item.Type = pdu.VariableTypeInteger | |
item.Value = int32(-123) | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.2") | |
item.Type = pdu.VariableTypeOctetString | |
item.Value = "echo test" | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.3") | |
item.Type = pdu.VariableTypeNull | |
item.Value = nil | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.4") | |
item.Type = pdu.VariableTypeObjectIdentifier | |
item.Value = "1.3.6.1.4.1.45995.1.5" | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.5") | |
item.Type = pdu.VariableTypeIPAddress | |
item.Value = net.IP{10, 10, 10, 10} | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.6") | |
item.Type = pdu.VariableTypeCounter32 | |
item.Value = uint32(123) | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.7") | |
item.Type = pdu.VariableTypeGauge32 | |
item.Value = uint32(123) | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.8") | |
item.Type = pdu.VariableTypeTimeTicks | |
item.Value = 123 * time.Second | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.9") | |
item.Type = pdu.VariableTypeOpaque | |
item.Value = []byte{1, 2, 3} | |
item = listHandler.Add("1.3.6.1.4.1.45995.3.10") | |
item.Type = pdu.VariableTypeCounter64 | |
item.Value = uint64(12345678901234567890) | |
session.Handler = listHandler | |
if err := session.Register(127, value.MustParseOID("1.3.6.1.4.1.45995.3")); err != nil { | |
log.Fatalf(errgo.Details(err)) | |
} | |
for { | |
time.Sleep(100 * time.Millisecond) | |
} | |
} |
Author
kevsersrca
commented
Mar 1, 2018
package main
import (
"fmt"
"log"
"os"
"sync"
"time"
"github.com/soniah/gosnmp"
)
var wg sync.WaitGroup
const (
devClientCount string = ".1.3.6.1.4.1.29671.1.1.4.1.5"
devName = ".1.3.6.1.4.1.29671.1.1.4.1.2"
)
func main() {
client := &gosnmp.GoSNMP{
Target: "159.253.41.211",
Port: 161,
Community: "public",
Version: gosnmp.Version2c,
Timeout: time.Duration(2) * time.Second,
}
err := client.Connect()
if err != nil {
log.Fatalf("Connect() err: %v", err)
}
fmt.Println("connected")
defer client.Conn.Close()
wg.Add(1)
go walk(devClientCount, client)
wg.Add(1)
go walk(devClientCount, client)
wg.Wait()
}
func walk(oid string, client *gosnmp.GoSNMP) {
defer wg.Done()
err := client.BulkWalk(oid, printValue)
if err != nil {
fmt.Printf("Walk Error: %v\n", err)
os.Exit(1)
}
}
func printValue(pdu gosnmp.SnmpPDU) error {
fmt.Printf("%s = ", pdu.Name)
switch pdu.Type {
case gosnmp.OctetString:
b := pdu.Value.([]byte)
fmt.Println("STRING: %s\n", string(b))
default:
fmt.Println("INTEGER %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
}
return nil
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment