|
| 1 | +# High-level client usage — one `SNMP::Client` per fiber. |
| 2 | +# In your project: require "snmp" |
| 3 | +require "../src/snmp" |
| 4 | + |
| 5 | +# ---- v2c ---- |
| 6 | +client = SNMP::Client.new("localhost", community: "public") |
| 7 | + |
| 8 | +# Single get: the shortcut accessors read the first varbind |
| 9 | +message = client.get("1.3.6.1.2.1.1.4.0") |
| 10 | +puts message.value.get_string |
| 11 | + |
| 12 | +# Multi-varbind get: one round-trip for several OIDs |
| 13 | +message = client.get(["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.5.0"]) |
| 14 | +message.varbinds.each { |varbind| puts "#{varbind.oid} => #{varbind.value.get_string}" } |
| 15 | + |
| 16 | +# Walk a subtree (GetNext based); the block form streams |
| 17 | +client.walk("1.3.6.1.2.1.1.9.1.3") do |msg| |
| 18 | + puts "#{msg.oid} => #{msg.value.get_string}" |
| 19 | +end |
| 20 | + |
| 21 | +# Bulk walk (GetBulk based, fewer round-trips); yields each VarBind |
| 22 | +client.bulk_walk("1.3.6.1.2.1.2.2.1.2", max_repetitions: 25) do |varbind| |
| 23 | + puts "#{varbind.oid} => #{varbind.value.get_string}" |
| 24 | +end |
| 25 | + |
| 26 | +# Set values — Crystal primitives or typed SNMP values |
| 27 | +client.set("1.3.6.1.2.1.1.6.0", "server room") |
| 28 | +client.set("1.3.6.1.2.1.1.3.0", SNMP::TimeTicks.new(12_345_u32)) |
| 29 | + |
| 30 | +# Multi-varbind set: one SetRequest for several assignments |
| 31 | +client.set({ |
| 32 | + "1.3.6.1.2.1.1.5.0" => "hostname", |
| 33 | + "1.3.6.1.2.1.1.6.0" => "the location", |
| 34 | +}) |
| 35 | + |
| 36 | +# Send notifications (community sessions) |
| 37 | +client.send_trap_v2("1.3.6.1.4.1.8072.2.3.0.1", uptime: 12_345) |
| 38 | +response = client.send_inform("1.3.6.1.4.1.8072.2.3.0.1") |
| 39 | +puts response.request # Response (the inform acknowledgement) |
| 40 | + |
| 41 | +# Release the reused UDP socket when done (reconnects transparently if reused) |
| 42 | +client.close |
| 43 | + |
| 44 | +# ---- v3 ---- |
| 45 | +# The client drives engine discovery, HMAC verification, the RFC 3414 time |
| 46 | +# window, and auto-resyncs/retries once on a usmStats Report. |
| 47 | +security = SNMP::V3::Security.new( |
| 48 | + "usr-sha-aes", |
| 49 | + auth_protocol: SNMP::V3::Security::AuthProtocol::SHA256, |
| 50 | + auth_password: "authkey1", |
| 51 | + priv_protocol: SNMP::V3::Security::PrivacyProtocol::AES256, |
| 52 | + priv_password: "privkey1" |
| 53 | +) |
| 54 | +v3_client = SNMP::Client.new("localhost", SNMP::V3::Session.new(security)) |
| 55 | +puts v3_client.get("1.3.6.1.2.1.1.4.0").value.get_string |
0 commit comments