This is a creation in Article, where the information may have evolved or changed.
SONIAH/GOSNMP is a pure Go language-written SNMP package, based on ALOUCA/GOSNMP development. Currently provides getrequest, GetNext, Getbulk, walk, and setrequest functions, supports IPV4 and IPV6, and supports SNMPV2C and SNMPv3.
This article describes the Setrequest and walk operations of SONIAH/GOSNMP.
Setrequest
The Snoiah/gosnmp GoSNMP.Set()
method has an obvious error:
func (x *GoSNMP) Set ( PDUs []SNMPPDU] (Result *snmppacket, err error) {if pdus[ 0 ]. Type! = Integer | | Pdus[0 ]. Type! = octetstring {return nil , FMT. Errorf ( "ERR:GOSNMP currently only supports SNMP sets for integers and octetstrings" )} //build up Snmppacket packetout: = X.mksnmppacket (Setrequest, 0 , 0 ) return x.send (PDUs, packetout)}
if
The two conditions of the statement should be "with", that is, if the type of the PDU is neither and Integer
not an OctetString
error (SONIAH/GOSNMP currently only supports integer and octetstring two types), it ||
should be replaced &&
.
Set_request.go
PackageMainImport("FMT" "Log" "Time"G"Github.com/soniah/gosnmp")funcMain () {varPDUs []gs. SNMPPDU PDU: = Gs. SNMPPDU {Name:"1.3.6.1.2.1.1.5.0", Type:gs. OctetString, Value:"New_name",} PDUs =Append(PDUs, PDU) G.default.target ="172.18.0.2"G.default.community ="Community"ERR: = Gs. Default.connect ()ifErr! =Nil{FMT. Printf ("Connect () Err:%v\n", err)}deferG.default.conn.close () result, err: = G.default.set (PDUs)ifErr! =Nil{FMT. Printf ("Set () Err:%v", err)} forI, V: =RangeResult. Variables {fmt. Printf ("%d. OID:%s", I, V.name)SwitchV.type { CaseG.octetstring:fmt. Printf ("string:%s\n",string(V.value. ([]byte)))default: FMT. Printf ("Number:%d\n", G.tobigint (V.value))}}}
Walk
Walk is not a primitive of SNMP, it is a shortcut tool built with operations such as GetNext or getbulk, and is commonly used to get a subtrees tree management object at once, or to traverse the entire MIB tree (note that some device manufacturers are not able to obtain a complete MIB for third parties. The private MIB is deliberately set to discontinuous, and the walk operation terminates at a discontinuous point. Strongly despise these manufacturers).
Walk.go
PackageMainImport("FMT"G"Github.com/soniah/gosnmp")funcMain () {g.default.target ="172.18.0.2"G.default.community ="Community"ERR: =g.default.connect ()ifErr! =Nil{FMT. Printf ("Connect () Err:%v", err)}deferG.default.conn.close () OID: =". 1.3.6.1.2.1.2.2.1.2" //Define callback functions that are used to process the returned SNMPPDU after each getnext operation is completed //function type: Type Walkfunc func (dataunit SNMPPDU) Errorfn: =func(v G.SNMPPDU) error {FMT. Printf ("OID:%s, Value:", V.name)Switchv.type{ CaseG.octetstring:fmt. Printf ("%s\n",string(V.value. ([]byte)))default: FMT. Printf ("%d\n", G.tobigint (V.value))}return NilErr = G.default.walk (OID, FN)ifErr! =Nil{FMT. Printf ("Walk () Err:%v", err)}}
GoSNMP.Wall()
method is implemented using the GetNext operation, where each OID needs to be executed once GetNext()
, is inefficient, can be improved using methods, and BulkWall()
BulkWalk()
returns multiple OIDs at once using the Getbulk operation.
The source code in walk and Bulkwalk use the same function, there is only one difference:
Maxreps: = x.maxrepetitionsifMaxreps <=0{maxreps = Defaultmaxrepetitions}getfn: =func(OIDstring) (Result *snmppacket, err error) {SwitchGetrequesttype { CaseGetbulkrequest:returnX.getbulk ([]string{OID},uint8(X.nonrepeaters),uint8(maxreps)) CaseGetnextrequest:returnX.getnext ([]string{OID})default:return NilFmt. Errorf ("Unsupported request type:%d", Getrequesttype)}}
BulkWalk()
As with Walk()
the use of the same, just replace the Walk.go code g.Default.Walk(oid)
, the g.Default.BulkWalk(oid)
same effect, just use more efficient getbulk operation.
Walkall
GoSNMP.Walk()
The method uses the callback function to process the returned data, and the WalkAll()
method provides a general way to store the PDU returned by each operation in an array, which is returned to the caller for processing when the traversal operation is completed.
Walk_all.go
PackageMainImport("FMT"G"Github.com/soniah/gosnmp")funcMain () {g.default.target ="172.18.0.2"G.default.community ="Community"ERR: =g.default.connect ()ifErr! =Nil{FMT. Printf ("Connect () Err:%v", err)}deferG.default.conn.close () OID: =". 1.3.6.1.2.1.2.2.1.2"result, err: = G.default.walkall (OID)ifErr! =Nil{FMT. Printf ("Walk () Err:%v", err)} for_, V: =RangeResult {FMT. Printf ("OID:%s, Value:", V.name)Switchv.type{ CaseG.octetstring:fmt. Printf ("%s\n",string(V.value. ([]byte)))default: FMT. Printf ("%d\n", G.tobigint (V.value))}}}
BulkWalkAll()
and WalkAll()
using the same method, just replace the Walk_all.go code with g.Default.WalkAll(oid)
the g.Default.BulkWalkAll(oid)
same effect, just use a more efficient getbulk operation.