func get(agent string, oids snmpgo.Oids) { // create a SNMP Object for each the agent snmp, err := snmpgo.NewSNMP(snmpgo.Arguments{ Version: snmpgo.V2c, Address: agent, Retries: 1, Community: "public", }) if err != nil { fmt.Printf("[%s] : construct error - %s\n", agent, err) return } if err = snmp.Open(); err != nil { fmt.Printf("[%s] : open error - %s\n", agent, err) return } defer snmp.Close() pdu, err := snmp.GetRequest(oids) if err != nil { fmt.Printf("[%s] : get error - %s\n", agent, err) return } if pdu.ErrorStatus() != snmpgo.NoError { fmt.Printf("[%s] : error status - %s at %d\n", agent, pdu.ErrorStatus(), pdu.ErrorIndex()) } fmt.Printf("[%s] : %s\n", agent, pdu.VariableBindings()) }
func main() { snmpArgs, cmdArgs := parseArgs() if l := len(cmdArgs); l < 3 { usage("required AGENT and UPTIME and TRAP-OID", 2) } else if l%3 != 0 { usage(fmt.Sprintf("%s: missing TYPE/VALUE for variable", cmdArgs[l/3*3]), 2) } VariableBindings, err := buildVarBinds(cmdArgs) if err != nil { usage(err.Error(), 2) } snmp, err := snmpgo.NewSNMP(*snmpArgs) if err != nil { usage(err.Error(), 2) } if err = snmp.Open(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer snmp.Close() if inform { err = snmp.InformRequest(VariableBindings) } else { err = snmp.V2Trap(VariableBindings) } if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
func main() { snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V3, Address: "127.0.0.1:161", Retries: 1, UserName: "******", SecurityLevel: snmpgo.AuthPriv, AuthPassword: "******", AuthProtocol: snmpgo.Sha, PrivPassword: "******", PrivProtocol: snmpgo.Aes, }) if err != nil { // Failed to create snmpgo.SNMP object fmt.Println(err) return } oids, err := snmpgo.NewOids([]string{ "1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0", "1.3.6.1.2.1.1.3.0", }) if err != nil { // Failed to parse Oids fmt.Println(err) return } if err = snmp.Open(); err != nil { // Failed to open connection fmt.Println(err) return } defer snmp.Close() pdu, err := snmp.GetRequest(oids) if err != nil { // Failed to request fmt.Println(err) return } if pdu.ErrorStatus() != snmpgo.NoError { // Received an error from the agent fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex()) } // get VarBind list fmt.Println(pdu.VarBinds()) // select a VarBind fmt.Println(pdu.VarBinds().MatchOid(oids[0])) }
func TestMessageProcessingV1(t *testing.T) { snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V2c, Community: "public", }) mp := snmpgo.NewMessageProcessing(snmpgo.V2c) pdu := snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetRequest) msg, err := mp.PrepareOutgoingMessage(snmp, pdu) if err != nil { t.Errorf("PrepareOutgoingMessage() - has error %v", err) } if len(msg.PduBytes()) == 0 { t.Error("PrepareOutgoingMessage() - pdu bytes") } if pdu.RequestId() == 0 { t.Error("PrepareOutgoingMessage() - request id") } requestId := pdu.RequestId() _, err = mp.PrepareDataElements(snmp, msg, []byte{0x00, 0x00}) if err == nil { t.Error("PrepareDataElements() - message unmarshal error") } b, _ := msg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - pdu type check") } pdu = snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetResponse) rmsg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V1, pdu)) b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - version check") } pdu.SetRequestId(requestId) pduBytes, _ := pdu.Marshal() rmsg = snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) rmsg.Community = []byte("public") rmsg.SetPduBytes(pduBytes) b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err != nil { t.Errorf("PrepareDataElements() - has error %v", err) } }
func main() { // `snmpgo.SNMP.Open` function execute the EngineID Discovery when you use V3. // Specify the Agent's EngineID to `snmpgo.SNMPArguments.SecurityEngineId` parameter, // if you want to suppress this behavior. snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V3, Address: "127.0.0.1:162", Retries: 1, UserName: "******", SecurityLevel: snmpgo.AuthPriv, AuthPassword: "******", AuthProtocol: snmpgo.Sha, PrivPassword: "******", PrivProtocol: snmpgo.Aes, SecurityEngineId: "8000000004736e6d70676f", }) if err != nil { // Failed to create snmpgo.SNMP object fmt.Println(err) return } // Build VarBind list var varBinds snmpgo.VarBinds varBinds = append(varBinds, snmpgo.NewVarBind(snmpgo.OidSysUpTime, snmpgo.NewTimeTicks(1000))) oid, _ := snmpgo.NewOid("1.3.6.1.6.3.1.1.5.3") varBinds = append(varBinds, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid)) oid, _ = snmpgo.NewOid("1.3.6.1.2.1.2.2.1.1.2") varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewInteger(2))) oid, _ = snmpgo.NewOid("1.3.6.1.2.1.31.1.1.1.1.2") varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewOctetString([]byte("eth0")))) if err = snmp.Open(); err != nil { // Failed to open connection fmt.Println(err) return } defer snmp.Close() if err = snmp.V2Trap(varBinds); err != nil { // Failed to request fmt.Println(err) return } }
func main() { snmp, err := snmpgo.NewSNMP(snmpgo.Arguments{ Version: snmpgo.V2c, Address: "127.0.0.1:161", Retries: 1, Community: "public", }) if err != nil { // Failed to create snmpgo.SNMP object fmt.Println(err) return } oids, err := snmpgo.NewOids([]string{ "1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0", "1.3.6.1.2.1.1.3.0", }) if err != nil { // Failed to parse Oids fmt.Println(err) return } if err = snmp.Open(); err != nil { // Failed to open connection fmt.Println(err) return } defer snmp.Close() pdu, err := snmp.GetRequest(oids) if err != nil { // Failed to request fmt.Println(err) return } if pdu.ErrorStatus() != snmpgo.NoError { // Received an error from the agent fmt.Println(pdu.ErrorStatus(), pdu.ErrorIndex()) } // get VariableBinding list fmt.Println(pdu.VariableBindings()) // select a VariableBinding fmt.Println(pdu.VariableBindings().MatchOid(oids[0])) }
func main() { snmp, err := snmpgo.NewSNMP(snmpgo.Arguments{ Version: snmpgo.V2c, Address: "127.0.0.1:162", Retries: 1, Community: "public", }) if err != nil { // Failed to create snmpgo.SNMP object fmt.Println(err) return } // Build VariableBinding list var VariableBindings snmpgo.VariableBindings VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSysUpTime, snmpgo.NewTimeTicks(1000))) oid, _ := snmpgo.ParseOidFromString("1.3.6.1.6.3.1.1.5.3") VariableBindings = append(VariableBindings, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid)) oid, _ = snmpgo.ParseOidFromString("1.3.6.1.2.1.2.2.1.1.2") VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, snmpgo.NewInteger(2))) oid, _ = snmpgo.ParseOidFromString("1.3.6.1.2.1.31.1.1.1.1.2") VariableBindings = append(VariableBindings, snmpgo.NewVarBind(oid, snmpgo.NewOctetString([]byte("eth0")))) if err = snmp.Open(); err != nil { // Failed to open connection fmt.Println(err) return } defer snmp.Close() if err = snmp.V2Trap(VariableBindings); err != nil { // Failed to request fmt.Println(err) return } }
func main() { snmpArgs, cmdArgs := parseArgs() if len(cmdArgs) < 2 { usage("required AGENT and OID", 2) } oids, err := snmpgo.NewOids(cmdArgs[1:]) if err != nil { usage(err.Error(), 2) } snmp, err := snmpgo.NewSNMP(*snmpArgs) if err != nil { usage(err.Error(), 2) } if err = snmp.Open(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer snmp.Close() pdu, err := snmp.GetBulkWalk(oids, nonRepeaters, maxRepetitions) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if pdu.ErrorStatus() != snmpgo.NoError { fmt.Fprintln(os.Stderr, pdu.ErrorStatus(), pdu.ErrorIndex()) os.Exit(1) } for _, val := range pdu.VariableBindings() { fmt.Printf("%s = %s: %s\n", val.Oid, val.Variable.Type(), val.Variable) } }
func TestSNMP(t *testing.T) { snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V3, UserName: "******", SecurityLevel: snmpgo.AuthPriv, AuthPassword: "******", AuthProtocol: snmpgo.Md5, PrivPassword: "******", PrivProtocol: snmpgo.Des, }) pdu := snmpgo.NewPdu(snmpgo.V3, snmpgo.Report) err := snmpgo.SnmpCheckPdu(snmp, pdu) if err != nil { t.Errorf("checkPdu() - has error %v", err) } oids, _ := snmpgo.NewOids([]string{"1.3.6.1.6.3.11.2.1.1.0"}) pdu = snmpgo.NewPduWithOids(snmpgo.V3, snmpgo.Report, oids) err = snmpgo.SnmpCheckPdu(snmp, pdu) if err == nil { t.Error("checkPdu() - report oid") } }
func TestCommunity(t *testing.T) { expCom := "public" snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V2c, Community: expCom, }) sec := snmpgo.NewCommunity() pdu := snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetRequest) smsg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) err := sec.GenerateRequestMessage(snmp, smsg) if err != nil { t.Errorf("GenerateRequestMessage() - has error %v", err) } if !bytes.Equal(smsg.Community, []byte(expCom)) { t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]", expCom, smsg.Community) } if len(smsg.PduBytes()) == 0 { t.Error("GenerateRequestMessage() - pdu marshal") } pdu = snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetResponse) rmsg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - community check") } rmsg.Community = []byte(expCom) rmsg.SetPduBytes(smsg.PduBytes()) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err != nil { t.Errorf("ProcessIncomingMessage() - has error %v", err) } }
func TestMessageProcessingV3(t *testing.T) { snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V3, UserName: "******", SecurityLevel: snmpgo.AuthPriv, AuthPassword: "******", AuthProtocol: snmpgo.Md5, PrivPassword: "******", PrivProtocol: snmpgo.Des, }) mp := snmpgo.NewMessageProcessing(snmpgo.V3) usm := snmpgo.ToUsm(mp.Security()) usm.AuthKey = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} usm.PrivKey = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} pdu := snmpgo.NewPdu(snmpgo.V3, snmpgo.GetRequest) msg, err := mp.PrepareOutgoingMessage(snmp, pdu) if err != nil { t.Errorf("PrepareOutgoingMessage() - has error %v", err) } if len(msg.PduBytes()) == 0 { t.Error("PrepareOutgoingMessage() - pdu bytes") } if pdu.RequestId() == 0 { t.Error("PrepareOutgoingMessage() - request id") } msgv3 := snmpgo.ToMessageV3(msg) if msgv3.MessageId == 0 { t.Error("PrepareOutgoingMessage() - message id") } if !msgv3.Reportable() || !msgv3.Authentication() || !msgv3.Privacy() { t.Error("PrepareOutgoingMessage() - security flag") } msgv3.SetAuthentication(false) msgv3.SetPrivacy(false) msgv3.AuthEngineId = []byte{0, 0, 0, 0, 0} requestId := pdu.RequestId() messageId := msgv3.MessageId _, err = mp.PrepareDataElements(snmp, msg, []byte{0x00, 0x00}) if err == nil { t.Error("PrepareDataElements() - message unmarshal error") } b, _ := msg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - pdu type check") } pdu = snmpgo.NewPdu(snmpgo.V3, snmpgo.GetResponse) rmsg := snmpgo.ToMessageV3(snmpgo.NewMessage(snmpgo.V3, pdu)) b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - message id check") } rmsg = snmpgo.ToMessageV3(snmpgo.NewMessage(snmpgo.V3, pdu)) rmsg.AuthEngineId = []byte{0, 0, 0, 0, 0} rmsg.MessageId = messageId b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - security model check") } pdu.(*snmpgo.ScopedPdu).ContextEngineId = rmsg.AuthEngineId pduBytes, _ := pdu.Marshal() rmsg.SetPduBytes(pduBytes) rmsg.SecurityModel = 3 b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - request id check") } pdu.SetRequestId(requestId) pduBytes, _ = pdu.Marshal() rmsg.SetPduBytes(pduBytes) b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err != nil { t.Errorf("PrepareDataElements() - has error %v", err) } }
func main() { snmpArgs, cmdArgs := parseArgs() if l := len(cmdArgs); l < 1 { usage("required AGENT", 2) } else { if l > 1 { if num, err := strconv.Atoi(cmdArgs[1]); err != nil || num < 1 { usage(fmt.Sprintf("Illegal delay, value `%s`", cmdArgs[1]), 2) } else { delay = time.Duration(num) * time.Second } } if l > 2 { if num, err := strconv.Atoi(cmdArgs[2]); err != nil || num < 1 { usage(fmt.Sprintf("Illegal iterations, value `%s`", cmdArgs[2]), 2) } else { iterations = num } } } snmp, err := snmpgo.NewSNMP(*snmpArgs) if err != nil { usage(err.Error(), 2) } if err = snmp.Open(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer snmp.Close() info, err := getIfInfo(snmp) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if !noHeader { printHeader(info) } start := time.Now() for i := 0; ; i++ { if err := getIfTraffic(snmp, info); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } printTraffic(info) if iterations > 0 && i >= iterations { break } start = start.Add(delay) if d := start.Sub(info.Time); d > 0 { time.Sleep(d) } } }
func TestUsm(t *testing.T) { expUser := []byte("myUser") expEngId := []byte{0x80, 0x00, 0x00, 0x00, 0x01} expCtxId := []byte{0x80, 0x00, 0x00, 0x00, 0x05} expCtxName := "myName" snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V3, UserName: string(expUser), SecurityLevel: snmpgo.AuthPriv, AuthPassword: "******", AuthProtocol: snmpgo.Md5, PrivPassword: "******", PrivProtocol: snmpgo.Des, ContextEngineId: hex.EncodeToString(expCtxId), ContextName: expCtxName, }) sec := snmpgo.NewUsm() pdu := snmpgo.NewPdu(snmpgo.V3, snmpgo.GetRequest) spdu := pdu.(*snmpgo.ScopedPdu) smsg := snmpgo.ToMessageV3(snmpgo.NewMessage(snmpgo.V3, pdu)) smsg.SetAuthentication(false) smsg.SetPrivacy(false) // Discovery err := sec.GenerateRequestMessage(snmp, smsg) if err != nil { t.Errorf("GenerateRequestMessage() - has error %v", err) } if !bytes.Equal(spdu.ContextEngineId, expCtxId) { t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]", expCtxId, spdu.ContextEngineId) } if string(spdu.ContextName) != expCtxName { t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]", expCtxName, string(spdu.ContextName)) } if len(smsg.PduBytes()) == 0 { t.Error("GenerateRequestMessage() - pdu marshal") } pdu = snmpgo.NewPdu(snmpgo.V3, snmpgo.Report) rmsg := snmpgo.ToMessageV3(snmpgo.NewMessage(snmpgo.V3, pdu)) rmsg.SetPduBytes(smsg.PduBytes()) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - engineId check") } rmsg.AuthEngineId = expEngId rmsg.AuthEngineBoots = -1 err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - boots check") } rmsg.AuthEngineBoots = 1 rmsg.AuthEngineTime = -1 err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - time check") } rmsg.AuthEngineTime = 1 err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err != nil { t.Errorf("ProcessIncomingMessage() - has error %v", err) } if !bytes.Equal(sec.AuthEngineId, expEngId) { t.Errorf("ProcessIncomingMessage() - expected [%s], actual [%s]", sec.AuthEngineId, expEngId) } if len(sec.AuthKey) == 0 { t.Error("ProcessIncomingMessage() - authKey") } if len(sec.PrivKey) == 0 { t.Error("ProcessIncomingMessage() - privKey") } // Synchronize smsg.SetAuthentication(true) smsg.SetPrivacy(true) err = sec.GenerateRequestMessage(snmp, smsg) if err != nil { t.Errorf("GenerateRequestMessage() - has error %v", err) } if !bytes.Equal(smsg.UserName, expUser) { t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]", expUser, smsg.UserName) } if !bytes.Equal(smsg.AuthEngineId, expEngId) { t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]", expEngId, smsg.AuthEngineId) } if len(smsg.PrivParameter) == 0 { t.Error("GenerateRequestMessage() - privParameter") } if len(smsg.AuthParameter) == 0 { t.Error("GenerateRequestMessage() - authParameter") } pdu = snmpgo.NewPdu(snmpgo.V3, snmpgo.Report) rmsg = snmpgo.ToMessageV3(snmpgo.NewMessage(snmpgo.V3, pdu)) rmsg.SetAuthentication(true) rmsg.SetPrivacy(true) rmsg.SetPduBytes(smsg.PduBytes()) rmsg.AuthEngineId = []byte("foobar") rmsg.AuthEngineBoots = smsg.AuthEngineBoots rmsg.AuthEngineTime = smsg.AuthEngineTime rmsg.PrivParameter = smsg.PrivParameter rmsg.AuthParameter = smsg.AuthParameter err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - userName check") } rmsg.UserName = expUser err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - authEngine check") } rmsg.AuthEngineId = expEngId err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err != nil { t.Errorf("ProcessIncomingMessage() - has error %v", err) } if sec.AuthEngineBoots != rmsg.AuthEngineBoots { t.Error("ProcessIncomingMessage() - engineBoots") } if sec.AuthEngineTime != rmsg.AuthEngineTime { t.Error("ProcessIncomingMessage() - engineTime") } // Request sec.AuthEngineBoots = 1 sec.AuthEngineTime = 1 err = sec.GenerateRequestMessage(snmp, smsg) if err != nil { t.Errorf("GenerateRequestMessage() - has error %v", err) } if smsg.AuthEngineBoots != sec.AuthEngineBoots { t.Errorf("GenerateRequestMessage() - expected [%d], actual [%d]", sec.AuthEngineBoots, smsg.AuthEngineBoots) } if smsg.AuthEngineTime != sec.AuthEngineTime { t.Errorf("GenerateRequestMessage() - expected [%d], actual [%d]", sec.AuthEngineTime, smsg.AuthEngineTime) } pdu = snmpgo.NewPdu(snmpgo.V3, snmpgo.GetResponse) spdu = pdu.(*snmpgo.ScopedPdu) rmsg = snmpgo.ToMessageV3(snmpgo.NewMessage(snmpgo.V3, pdu)) rmsg.AuthEngineId = expEngId rmsg.AuthEngineBoots = smsg.AuthEngineBoots rmsg.AuthEngineTime = smsg.AuthEngineTime rmsg.UserName = expUser // set PduBytes with GetResponse b, _ := spdu.Marshal() rmsg.SetPduBytes(b) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - contextEngineId check") } // set PduBytes with ContextEngineId spdu.ContextEngineId = expCtxId b, _ = spdu.Marshal() rmsg.SetPduBytes(b) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - contextName check") } // set PduBytes with ContextName spdu.ContextName = []byte(expCtxName) b, _ = spdu.Marshal() rmsg.SetPduBytes(b) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - response authenticate check") } }