-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
90 lines (76 loc) · 1.71 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2019 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package main
import (
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
)
var (
community = flag.String("c", "", "SNMP community string")
dev = flag.String("d", "", "Device hostname/IP")
dumpfile = flag.String("o", "", "Name of file to write SNMP dump to")
oids = oidFlags{}
polls = flag.Int("n", 2, "Number of polls to perform")
)
type oidFlags []string
func (o *oidFlags) String() string {
return strings.Join(*o, ", ")
}
func (o *oidFlags) Set(v string) error {
*o = append(*o, v)
return nil
}
func init() {
flag.Var(&oids, "oid", "OID to walk - may be repeated to specify multiple")
}
func snmpWalkCmd(oid string) (string, []string) {
cmd := []string{"-O", "ne", "-Cc"}
if *community != "" {
cmd = append(cmd, "-c", *community)
}
cmd = append(cmd, *dev, oid)
return "snmpbulkwalk", cmd
}
func snmpWalk(f io.Writer) {
for _, o := range oids {
c, args := snmpWalkCmd(o)
cmd := exec.Command(c, args...)
cmd.Stdout = f
fmt.Printf("Walking OID '%s'...\n", o)
if err := cmd.Run(); err != nil {
log.Fatalf("Walk command failed: %v", err)
}
}
}
func main() {
flag.Parse()
if *dev == "" {
fmt.Println("-d must be specified")
os.Exit(1)
}
if *dumpfile == "" {
fmt.Println("-o must be specified")
os.Exit(1)
}
if len(oids) == 0 {
oids = []string{"."}
}
f, err := os.Create(*dumpfile)
if err != nil {
log.Fatalf("Failed to open dumpfile: %v", err)
}
defer f.Close()
gf := gzip.NewWriter(f)
gf.Header.Name = *dumpfile + ".gz"
defer gf.Close()
for i := 0; i < *polls; i++ {
snmpWalk(gf)
}
}