forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (59 loc) · 1.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2012 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
// This program demonstrates BulkWalk.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/vhdsih/gosnmp"
)
func main() {
flag.Usage = func() {
fmt.Printf("Usage:\n")
fmt.Printf(" %s [-community=<community>] host [oid]\n", filepath.Base(os.Args[0]))
fmt.Printf(" host - the host to walk/scan\n")
fmt.Printf(" oid - the MIB/Oid defining a subtree of values\n\n")
flag.PrintDefaults()
}
var community string
flag.StringVar(&community, "community", "public", "the community string for device")
flag.Parse()
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
target := flag.Args()[0]
var oid string
if len(flag.Args()) > 1 {
oid = flag.Args()[1]
}
gosnmp.Default.Target = target
gosnmp.Default.Community = community
gosnmp.Default.Timeout = time.Duration(10 * time.Second) // Timeout better suited to walking
err := gosnmp.Default.Connect()
if err != nil {
fmt.Printf("Connect err: %v\n", err)
os.Exit(1)
}
defer gosnmp.Default.Conn.Close()
err = gosnmp.Default.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.Printf("STRING: %s\n", string(b))
default:
fmt.Printf("TYPE %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
}
return nil
}