-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (187 loc) · 4.77 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"net"
"os"
"sort"
"strings"
"github.com/k-sone/snmpgo"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
const (
oidSysName = "1.3.6.1.2.1.1.5"
oidIfDescr = "1.3.6.1.2.1.2.2.1.2"
oidCDPCacheEntry = "1.3.6.1.4.1.9.9.23.1.2.1.1"
oidCDPCacheDeviceid = "1.3.6.1.4.1.9.9.23.1.2.1.1.6"
oidCDPCacheDevicePort = "1.3.6.1.4.1.9.9.23.1.2.1.1.7"
oidCDPCacheAddress = "1.3.6.1.4.1.9.9.23.1.2.1.1.4"
cacheAddress = 4
cacheDeviceid = 6
cacheDevicePort = 7
)
var (
version = "0.0.1"
)
type cdpNeighbor struct {
LName string
LIP string
LIfName string
RName string
RIP string
RIfName string
}
func compactIfName(ifname string) string {
if strings.HasPrefix(ifname, "Ten") {
return strings.Replace(ifname, "TenGigabitEthernet", "Te ", -1)
}
if strings.HasPrefix(ifname, "Gig") {
return strings.Replace(ifname, "GigabitEthernet", "Gi ", -1)
}
if strings.HasPrefix(ifname, "Fas") {
return strings.Replace(ifname, "FastEthernet", "Fa ", -1)
}
return ifname
}
func prepareOids() (snmpgo.Oids, error) {
return snmpgo.NewOids([]string{
oidCDPCacheDeviceid,
oidCDPCacheDevicePort,
oidCDPCacheAddress,
oidIfDescr,
oidSysName,
})
}
func getCDPNeighbors(host, community string) (map[string]cdpNeighbor, error) {
nei := map[string]cdpNeighbor{}
snmpAddr := net.JoinHostPort(host, "161")
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c,
Address: snmpAddr,
Retries: 1,
Community: community,
})
if err != nil {
return nil, err
}
if err = snmp.Open(); err != nil {
return nil, err
}
defer snmp.Close()
cacheEntry, err := prepareOids()
if err != nil {
return nil, err
}
pdu, err := snmp.GetBulkWalk(cacheEntry, 0, 10)
if err != nil {
return nil, err
}
if pdu.ErrorStatus() != snmpgo.NoError {
fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex())
}
oid, err := snmpgo.NewOid(oidCDPCacheEntry)
if err != nil {
return nil, err
}
cacheEntrys := pdu.VarBinds().MatchBaseOids(oid)
sysName := pdu.VarBinds().MatchBaseOids(snmpgo.MustNewOid(oidSysName))[0]
for _, val := range cacheEntrys {
ifindex := fmt.Sprintf("%d", val.Oid.Value[14])
if _, ok := nei[ifindex]; !ok {
oid, err = snmpgo.NewOid(oidIfDescr + "." + ifindex)
if err != nil {
return nil, err
}
ifnames := pdu.VarBinds().MatchBaseOids(oid)
rifname := ifnames.MatchOid(oid).Variable.String()
nei[ifindex] = cdpNeighbor{
RIfName: compactIfName(rifname),
}
}
cdp := nei[ifindex]
cdp.RName = sysName.Variable.String()
cdp.RIP = host
switch val.Oid.Value[13] {
case cacheAddress:
cdp.LIP = val.Variable.String()
var ip, ip1, ip2, ip3 int
fmt.Sscanf(val.Variable.String(), "%x:%x:%x:%x", &ip, &ip1, &ip2, &ip3)
cdp.LIP = fmt.Sprintf("%d.%d.%d.%d", ip, ip1, ip2, ip3)
case cacheDeviceid:
cdp.LName = val.Variable.String()
case cacheDevicePort:
cdp.LIfName = compactIfName(val.Variable.String())
}
nei[ifindex] = cdp
}
return nei, nil
}
func printTable(neigbors map[string]cdpNeighbor) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "IP", "Local IF", "--", "Remote IF", "Remote IP", "Remote Name"})
table.SetBorder(false)
var data [][]string
var keys []string
for k := range neigbors {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := neigbors[k]
data = append(data, []string{v.RName, v.RIP, v.RIfName, "->", v.LIfName, v.LIP, v.LName})
}
table.SetAlignment(4)
table.AppendBulk(data)
table.Render()
}
func neighbors(c *cli.Context) error {
community := c.String("community")
host := c.String("host")
if host == "" {
if err := cli.ShowCommandHelp(c, "neigbors"); err != nil {
return err
}
return cli.NewExitError("Host address required", 1)
}
res, err := getCDPNeighbors(host, community)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
printTable(res)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "gocdp"
app.Usage = "show CDP by snmp"
app.Version = version
app.Authors = []cli.Author{
cli.Author{
Name: "hdhog",
Email: "[email protected]",
},
}
app.Commands = []cli.Command{
{
Name: "neigbors",
Aliases: []string{"n", "nei"},
Usage: "Show CDP neigbors over snmp",
Flags: []cli.Flag{
cli.StringFlag{
Name: "community, c",
Value: "public",
Usage: "community string",
},
cli.StringFlag{
Name: "host ,s",
Usage: "host address",
},
},
Action: neighbors,
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}