Add SNMP v2c probing and results endpoint.
Probe sysName/sysDescr/sysObjectID for alive hosts, persist SNMP outcomes, and expose scan SNMP data via REST. Made-with: Cursor
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package scans
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/netip"
|
||||
@@ -9,14 +10,22 @@ import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gosnmp/gosnmp"
|
||||
)
|
||||
|
||||
type Runner struct {
|
||||
store Store
|
||||
cfg RunnerConfig
|
||||
}
|
||||
|
||||
func NewRunner(store Store) *Runner {
|
||||
return &Runner{store: store}
|
||||
type RunnerConfig struct {
|
||||
SNMPEnabled bool
|
||||
SNMPCommunity string
|
||||
}
|
||||
|
||||
func NewRunner(store Store, cfg RunnerConfig) *Runner {
|
||||
return &Runner{store: store, cfg: cfg}
|
||||
}
|
||||
|
||||
func (r *Runner) Start(job ScanJob) {
|
||||
@@ -87,6 +96,9 @@ func (r *Runner) run(job ScanJob) {
|
||||
})
|
||||
}
|
||||
}
|
||||
if isUp && r.cfg.SNMPEnabled {
|
||||
_ = r.store.SaveSNMPResult(job.ID, probeSNMP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS))
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
done++
|
||||
@@ -125,6 +137,58 @@ func (r *Runner) run(job ScanJob) {
|
||||
})
|
||||
}
|
||||
|
||||
func probeSNMP(ip, community string, checkedAt time.Time, timeoutMS int) SNMPResult {
|
||||
if timeoutMS <= 0 {
|
||||
timeoutMS = 700
|
||||
}
|
||||
if community == "" {
|
||||
community = "public"
|
||||
}
|
||||
|
||||
client := &gosnmp.GoSNMP{
|
||||
Target: ip,
|
||||
Port: 161,
|
||||
Version: gosnmp.Version2c,
|
||||
Community: community,
|
||||
Timeout: time.Duration(timeoutMS) * time.Millisecond,
|
||||
Retries: 1,
|
||||
}
|
||||
if err := client.Connect(); err != nil {
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}
|
||||
}
|
||||
defer client.Conn.Close()
|
||||
|
||||
oids := []string{
|
||||
".1.3.6.1.2.1.1.5.0",
|
||||
".1.3.6.1.2.1.1.1.0",
|
||||
".1.3.6.1.2.1.1.2.0",
|
||||
}
|
||||
pkt, err := client.Get(oids)
|
||||
if err != nil {
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}
|
||||
}
|
||||
|
||||
out := SNMPResult{IP: ip, Success: true, CheckedAt: checkedAt}
|
||||
for _, vb := range pkt.Variables {
|
||||
switch vb.Name {
|
||||
case ".1.3.6.1.2.1.1.5.0":
|
||||
out.SysName = snmpValueToString(vb.Value)
|
||||
case ".1.3.6.1.2.1.1.1.0":
|
||||
out.SysDescr = snmpValueToString(vb.Value)
|
||||
case ".1.3.6.1.2.1.1.2.0":
|
||||
out.SysObjectID = snmpValueToString(vb.Value)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func snmpValueToString(v any) string {
|
||||
if b, ok := v.([]byte); ok {
|
||||
return string(b)
|
||||
}
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
|
||||
func expandTargets(cidrs []string, excludeIPs []string) ([]string, error) {
|
||||
excluded := make(map[string]struct{}, len(excludeIPs))
|
||||
for _, ip := range excludeIPs {
|
||||
|
||||
Reference in New Issue
Block a user