-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
snmp.go
90 lines (76 loc) · 2.28 KB
/
snmp.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
package main
import (
"encoding/hex"
"fmt"
"github.com/TwiN/go-color"
"github.com/gosnmp/gosnmp"
"time"
)
func setupSNMPClient(host string, timeout int) (*gosnmp.GoSNMP, error) {
timeoutDuration := time.Duration(timeout) * time.Second
client := &gosnmp.GoSNMP{
Target: host,
Port: 161, // Default SNMP port
Community: "public", // Adjust as needed
Version: gosnmp.Version2c, // Adjust as needed
Timeout: timeoutDuration, // Set timeout
Retries: 1, // Optional: Set retries
}
err := client.Connect()
if err != nil {
if *verbose {
fmt.Println(color.Ize(color.Red, fmt.Sprintf("[Verbose] Error connecting to SNMP server: %s", err)))
}
return nil, err
}
if *verbose {
fmt.Println(color.Ize(color.Green, fmt.Sprintf("[Verbose] Successfully connected to SNMP server at %s", host)))
}
return client, nil
}
func sendSNMPRequest(client *gosnmp.GoSNMP, step Step) (string, error) {
var pdu gosnmp.SnmpPDU
switch step.InputType {
case "string":
pdu = gosnmp.SnmpPDU{Name: step.Input, Type: gosnmp.OctetString}
case "hex":
decodedInput, err := hex.DecodeString(step.Input)
if err != nil {
return "", err
}
pdu = gosnmp.SnmpPDU{Name: string(decodedInput), Type: gosnmp.OctetString}
default:
return "", fmt.Errorf("unsupported input type for SNMP: %s", step.InputType)
}
if *verbose {
fmt.Println(color.Ize(color.Blue, fmt.Sprintf("[Verbose] Sending SNMP request: OID=%s", pdu.Name)))
}
response, err := client.Get([]string{pdu.Name})
if err != nil {
if *verbose {
fmt.Println(color.Ize(color.Red, fmt.Sprintf("[Verbose] SNMP request failed: %s", err)))
}
return "", err
}
if *verbose {
fmt.Println(color.Ize(color.Green, "[Verbose] SNMP response received"))
}
var responseValue string
if len(response.Variables) > 0 {
variable := response.Variables[0]
switch variable.Type {
case gosnmp.OctetString:
responseValue = string(variable.Value.([]byte))
default:
responseValue = fmt.Sprintf("%v", variable.Value)
}
if *verbose {
fmt.Println(color.Ize(color.Cyan, fmt.Sprintf("[Verbose] SNMP response value: %s", responseValue)))
}
} else {
if *verbose {
fmt.Println(color.Ize(color.Yellow, "[Verbose] SNMP response contains no variables"))
}
}
return responseValue, nil
}