{"id":25962801,"url":"https://github.com/soniah/gosnmp","last_synced_at":"2025-03-04T20:01:58.761Z","repository":{"id":4429238,"uuid":"5567243","full_name":"gosnmp/gosnmp","owner":"gosnmp","description":"An SNMP library written in Go","archived":false,"fork":false,"pushed_at":"2024-08-16T09:55:07.000Z","size":938,"stargazers_count":1130,"open_issues_count":45,"forks_count":351,"subscribers_count":48,"default_branch":"master","last_synced_at":"2024-10-29T15:04:51.046Z","etag":null,"topics":["go","golang","snmp","snmp-agent","snmpv2","snmpv2-trap","snmpv3"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gosnmp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.md","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-08-27T05:59:24.000Z","updated_at":"2024-10-27T16:31:42.000Z","dependencies_parsed_at":"2023-10-16T23:57:21.358Z","dependency_job_id":"ff8a5a4f-6556-43e5-bb55-113451e23032","html_url":"https://github.com/gosnmp/gosnmp","commit_stats":{"total_commits":541,"total_committers":101,"mean_commits":5.356435643564357,"dds":0.6210720887245841,"last_synced_commit":"448fcd8a01ec358c87ca8aea78ba8b73ae123590"},"previous_names":["soniah/gosnmp"],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosnmp%2Fgosnmp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosnmp%2Fgosnmp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosnmp%2Fgosnmp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosnmp%2Fgosnmp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gosnmp","download_url":"https://codeload.github.com/gosnmp/gosnmp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241913770,"owners_count":20041460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","snmp","snmp-agent","snmpv2","snmpv2-trap","snmpv3"],"created_at":"2025-03-04T20:01:27.113Z","updated_at":"2025-03-04T20:01:58.723Z","avatar_url":"https://github.com/gosnmp.png","language":"Go","funding_links":[],"categories":["Networking","网络","Misc","Network Analysis","网络相关库","\u003cspan id=\"网络-networking\"\u003e网络 Networking\u003c/span\u003e"],"sub_categories":["Transliteration","音译","交流","Uncategorized","Advanced Console UIs","Strings","暂未分类这些库被放在这里是因为其他类别似乎都不适合。","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","暂未分类"],"readme":"gosnmp\n======\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/avelino/awesome-go#networking)\n\n[![Build Status](https://circleci.com/gh/gosnmp/gosnmp.svg?style=shield)](https://circleci.com/gh/gosnmp/gosnmp/tree/master)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/gosnmp/gosnmp)](https://pkg.go.dev/github.com/gosnmp/gosnmp)\n\nGoSNMP is an SNMP client library fully written in Go. It provides Get,\nGetNext, GetBulk, Walk, BulkWalk, Set and Traps. It supports IPv4 and\nIPv6, using __SNMPv1__, __SNMPv2c__ or __SNMPv3__. Builds are tested against\nlinux/amd64 and linux/386.\n\n# About\n\n**gosnmp** was started by [Andreas Louca](https://github.com/alouca), then\ncompletely rewritten by [Sonia Hamilton](https://github.com/soniah)\n(2012-2020), and now ownership has been transferred to the community at\n[gosnmp/gosnmp](https://github.com/gosnmp/gosnmp).\n\nFor support and help, join us in the #snmp channel of\n[Gophers Slack](https://invite.slack.golangbridge.org/).\n\n# Overview\n\nGoSNMP has the following SNMP functions:\n\n* **Get** (single or multiple OIDs)\n* **GetNext**\n* **GetBulk** (SNMPv2c and SNMPv3 only)\n* **Walk** - retrieves a subtree of values using GETNEXT.\n* **BulkWalk** - retrieves a subtree of values using GETBULK (SNMPv2c and\n  SNMPv3 only).\n* **BulkWalkAll** - similar to BulkWalk but returns a filled array of all values rather than using a callback function to stream results.\n* **Set** - supports Integers and OctetStrings.\n* **SendTrap** - send SNMP TRAPs.\n* **Listen** - act as an NMS for receiving TRAPs.\n\nGoSNMP has the following **helper** functions:\n\n* **ToBigInt** - treat returned values as `*big.Int`\n* **Partition** - facilitates dividing up large slices of OIDs\n\n**gosnmp/gosnmp** has completely diverged from **alouca/gosnmp**, your code\nwill require modification in these (and other) locations:\n\n* the **Get** function has a different method signature\n* the **NewGoSNMP** function has been removed, use **Connect** instead\n  (see Usage below). `Connect` uses the `GoSNMP` struct;\n  `gosnmp.Default` is provided for you to build on.\n* GoSNMP no longer relies on **alouca/gologger** - you can use your\n  logger if it conforms to the `gosnmp.LoggerInterface` interface; otherwise\n  debugging will disabled.\n\n```go\ntype LoggerInterface interface {\n    Print(v ...interface{})\n    Printf(format string, v ...interface{})\n}\n```\nTo enable logging, you must call gosnmp.NewLogger() function, and pass a pointer to your logging interface, for example with standard *log.Logger:\n\n```go\ngosnmp.Default.Logger = gosnmp.NewLogger(log.New(os.Stdout, \"\", 0))\n```\nor\n```go\ng := \u0026gosnmp.GoSNMP{\n    ...\n    Logger:    gosnmp.NewLogger(log.New(os.Stdout, \"\", 0)),\n}\n\n```\nYou can completely remove the logging code from your application using the golang build tag \"gosnmp_nodebug\", for example:\n```\ngo build -tags gosnmp_nodebug\n```\nThis will completely disable the logging of the gosnmp library, even if the logger interface is specified in the code. This provides a small performance improvement.\n\n# Installation\n\n```shell\ngo get github.com/gosnmp/gosnmp\n```\n\n# Documentation\n\nhttps://pkg.go.dev/github.com/gosnmp/gosnmp\n\n# Usage\n\nHere is `examples/example/main.go`, demonstrating how to use GoSNMP:\n\n```go\n// Default is a pointer to a GoSNMP struct that contains sensible defaults\n// eg port 161, community public, etc\ng.Default.Target = \"192.168.1.10\"\nerr := g.Default.Connect()\nif err != nil {\n    log.Fatalf(\"Connect() err: %v\", err)\n}\ndefer g.Default.Conn.Close()\n\noids := []string{\"1.3.6.1.2.1.1.4.0\", \"1.3.6.1.2.1.1.7.0\"}\nresult, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS\nif err2 != nil {\n    log.Fatalf(\"Get() err: %v\", err2)\n}\n\nfor i, variable := range result.Variables {\n    fmt.Printf(\"%d: oid: %s \", i, variable.Name)\n\n    // the Value of each variable returned by Get() implements\n    // interface{}. You could do a type switch...\n    switch variable.Type {\n    case g.OctetString:\n        bytes := variable.Value.([]byte)\n        fmt.Printf(\"string: %s\\n\", string(bytes))\n    default:\n        // ... or often you're just interested in numeric values.\n        // ToBigInt() will return the Value as a BigInt, for plugging\n        // into your calculations.\n        fmt.Printf(\"number: %d\\n\", g.ToBigInt(variable.Value))\n    }\n}\n```\n\nRunning this example gives the following output (from my printer):\n\n```shell\n% go run example.go\n0: oid: 1.3.6.1.2.1.1.4.0 string: Administrator\n1: oid: 1.3.6.1.2.1.1.7.0 number: 104\n```\n\n* `examples/example2.go` is similar to `example.go`, however it uses a\n  custom `\u0026GoSNMP` rather than `g.Default`\n* `examples/walkexample.go` demonstrates using `BulkWalk`\n* `examples/example3.go` demonstrates `SNMPv3`\n* `examples/trapserver.go` demonstrates writing an SNMP v2c trap server\n\n# MIB Parser\n\nI don't have any plans to write a mib parser. Others have suggested\nhttps://github.com/sleepinggenius2/gosmi\n\n# Contributions\n\nContributions are welcome, especially ones that have packet captures (see\nbelow).\n\nIf you've never contributed to a Go project before, here is an example workflow.\n\n1. [fork this repo on the GitHub webpage](https://github.com/gosnmp/gosnmp/fork)\n1. `go get github.com/gosnmp/gosnmp`\n1. `cd $GOPATH/src/github.com/gosnmp/gosnmp`\n1. `git remote rename origin upstream`\n1. `git remote add origin git@github.com:\u003cyour-github-username\u003e/gosnmp.git`\n1. `git checkout -b development`\n1. `git push -u origin development` (setup where you push to, check it works)\n\n# Packet Captures\n\nCreate your packet captures in the following way:\n\nExpected output, obtained via an **snmp** command. For example:\n\n```shell\n% snmpget -On -v2c -c public 203.50.251.17 1.3.6.1.2.1.1.7.0 \\\n  1.3.6.1.2.1.2.2.1.2.6 1.3.6.1.2.1.2.2.1.5.3\n.1.3.6.1.2.1.1.7.0 = INTEGER: 78\n.1.3.6.1.2.1.2.2.1.2.6 = STRING: GigabitEthernet0\n.1.3.6.1.2.1.2.2.1.5.3 = Gauge32: 4294967295\n```\n\nA packet capture, obtained while running the snmpget. For example:\n\n```shell\nsudo tcpdump -s 0 -i eth0 -w foo.pcap host 203.50.251.17 and port 161\n```\n\n# Bugs\n\nRane's document [SNMP: Simple? Network Management\nProtocol](https://www.ranecommercial.com/legacy/note161.html) was useful when learning the SNMP\nprotocol.\n\nPlease create an [issue](https://github.com/gosnmp/gosnmp/issues) on\nGithub with packet captures (upload capture to Google Drive, Dropbox, or\nsimilar) containing samples of missing BER types, or of any other bugs\nyou find. If possible, please include 2 or 3 examples of the\nmissing/faulty BER type.\n\nThe following BER types have been implemented:\n\n* 0x00 UnknownType\n* 0x01 Boolean\n* 0x02 Integer\n* 0x03 BitString\n* 0x04 OctetString\n* 0x05 Null\n* 0x06 ObjectIdentifier\n* 0x07 ObjectDescription\n* 0x40 IPAddress (IPv4 \u0026 IPv6)\n* 0x41 Counter32\n* 0x42 Gauge32\n* 0x43 TimeTicks\n* 0x44 Opaque (Float \u0026 Double)\n* 0x45 NsapAddress\n* 0x46 Counter64\n* 0x47 Uinteger32\n* 0x78 OpaqueFloat\n* 0x79 OpaqueDouble\n* 0x80 NoSuchObject\n* 0x81 NoSuchInstance\n* 0x82 EndOfMibView\n\n# Running the Tests\n\nLocal testing in Docker\n```shell\ndocker build -t gosnmp/gosnmp:latest .\ndocker run -it gosnmp/gosnmp:latest\n```\n\nor\n\n```shell\nexport GOSNMP_TARGET=1.2.3.4\nexport GOSNMP_PORT=161\nexport GOSNMP_TARGET_IPV4=1.2.3.4\nexport GOSNMP_PORT_IPV4=161\nexport GOSNMP_TARGET_IPV6='0:0:0:0:0:ffff:102:304'\nexport GOSNMP_PORT_IPV6=161\ngo test -v -tags all        # for example\ngo test -v -tags helper     # for example\n```\n\nTests are grouped as follows:\n\n* Unit tests (validating data packing and marshalling):\n   * `marshal_test.go`\n   * `misc_test.go`\n* Public API consistency tests:\n   * `gosnmp_api_test.go`\n* End-to-end integration tests:\n   * `generic_e2e_test.go`\n\nThe generic end-to-end integration test `generic_e2e_test.go` should\nwork against any SNMP MIB-2 compliant host (e.g. a router, NAS box, printer).\n\nMocks were generated using:\n\n`mockgen -source=interface.go -destination=mocks/gosnmp_mock.go -package=mocks`\n\nHowever they're currently removed, as they were breaking linting.\n\nTo profile cpu usage:\n\n```shell\ngo test -cpuprofile cpu.out\ngo test -c\ngo tool pprof gosnmp.test cpu.out\n```\n\nTo profile memory usage:\n\n```shell\ngo test -memprofile mem.out\ngo test -c\ngo tool pprof gosnmp.test mem.out\n```\n\nTo check test coverage:\n\n```shell\ngo get github.com/axw/gocov/gocov\ngo get github.com/matm/gocov-html\ngocov test github.com/gosnmp/gosnmp | gocov-html \u003e gosnmp.html \u0026\u0026 firefox gosnmp.html \u0026\n```\n\nTo measure the performance of password hash caching:\n\nPassword hash caching can be disabled during benchmark tests by using the golang build tag \"gosnmp_nopwdcache\", so:\n```\ngo build -tags gosnmp_nopwdcache -bench=Benchmark.*Hash\n```\nwill benchmark the code without password hash caching. Removing the tag will run the benchmark with caching enabled (default behavior of package).\n\n\n# License\n\nParts of the code are taken from the Golang project (specifically some\nfunctions for unmarshaling BER responses), which are under the same terms\nand conditions as the Go language. The rest of the code is under a BSD\nlicense.\n\nSee the LICENSE file for more details.\n\nThe remaining code is Copyright 2012 the GoSNMP Authors - see\nAUTHORS.md for a list of authors.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoniah%2Fgosnmp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoniah%2Fgosnmp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoniah%2Fgosnmp/lists"}