• Stars
    star
    16
  • Rank 1,269,358 (Top 26 %)
  • Language
    Crystal
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SNMP implementation for crystal lang

SNMP Support for Crystal Lang

CI

NOTE:: I consider the project ready to use. Usage won't change significantly between now and v1.0.0

Usage

This library can be used to build either an SNMP Agent or Client application. The examples below indicate how to use it as a client.

SNMP v2c

# Connect to server
socket = UDPSocket.new
socket.connect("demo.snmplabs.com", 161)
socket.sync = false

# Make request
session = SNMP::Session.new
socket.write_bytes session.get("1.3.6.1.2.1.1.4.0")
socket.flush

# Process response
response = session.parse(socket.read_bytes(ASN1::BER))
response.value.get_string # "SNMP Laboratories, [email protected]"

SNMP v3

# Connect to server
socket = UDPSocket.new
socket.connect("demo.snmplabs.com", 161)
socket.sync = false

# Setup session
session = SNMP::V3::Session.new("usr-md5-aes", "authkey1", "privkey1", priv_protocol: SNMP::V3::Security::PrivacyProtocol::AES)

# This is required to get the engine ID, boot and tick times
# You can read about it here: https://www.snmpsharpnet.com/?page_id=28
if session.must_revalidate?
  socket.write_bytes session.engine_validation_probe
  socket.flush
  session.validate socket.read_bytes(ASN1::BER)
end

# Make the request
# NOTE:: with SNMPv3 you need to prepare the message for transmission
unencrypted_message = session.get("1.3.6.1.2.1.1.4.0")
socket.write_bytes session.prepare(unencrypted_message)
socket.flush

# Process response
response = session.parse(socket.read_bytes(ASN1::BER))
response.value.get_string # "SNMP Laboratories, [email protected]"

Setting values

NOTE:: set currently supports:

  • Strings
  • Integers
  • Boolean
  • Nil

More crystal classes will be added over time (such as Float and Socket::IPAddress etc)

# Setting a string
session.set("1.3.6.1.2.1.1.3.0", "some string value")

# Setting an integer
session.set("1.3.6.1.2.1.1.3.0", 34)

For more complex or currently unsupported types you can build a custom ASN1.BER.

ber = ASN1::BER.new
ber.tag_class = ASN1::BER::TagClass::Application
ber.tag_number = 12
ber.payload = Bytes[1,2,3,4,5]

session.set("1.3.6.1.2.1.1.3.0", ber)

Extracting response values

The response value is always an ASN1::BER

response = session.parse(socket.read_bytes(ASN1::BER))
response.value

You can extract common data types using helper methods:

  • .get_string
  • .get_object_id for SNMP OIDs such as 1.3.6.1.2.1
  • .get_hexstring for a hex representation of the payload bytes
  • .get_bytes for the raw byte data
  • .get_boolean
  • .get_integer returning an Int64

Notes on IO

Writing to Sockets

When writing SNMP messages to the socket, be aware that you should be buffering the write.

session = SNMP::V3::Session.new
message = session.engine_validation_probe

# Ensure sync is false so the message is buffered
socket.sync = false
socket.write_bytes message

# This requires you to call `flush`
socket.flush

This is because the call to to_io on message involves multiple writes to the IO as the message is progressively constructed. However you don't want each write to be sending packets as this will result in a lot of overhead and most SNMP servers will not accept fragmented messages.

Reading from sockets

Whilst you'll probably be OK reading data like socket.read_bytes(ASN1::BER) you should probably be buffering requests based on SNMP PDU Max Size (defaulting to 65507 bytes) and throwing away any buffered data that can't be read after buffering or a short timeout.

More Repositories

1

spider-gazelle

A Rails esque web framework with a focus on speed and extensibility for crystal lang
Crystal
175
star
2

tasker

Scheduled tasks for crystal lang
Crystal
54
star
3

bindata

BinData - Parsing Binary Data in Crystal Lang
Crystal
48
star
4

ssh2.cr

libssh2 binding for Crystal language
Crystal
42
star
5

promise

Type aware promises for crystal lang
Crystal
40
star
6

action-controller

A rails-esque controller framework for crystal lang
Crystal
39
star
7

active-model

A rails-esque model framework for crystal lang
Crystal
28
star
8

rethinkdb-orm

RethinkDB ORM for Crystal lang
Crystal
24
star
9

crystal-mqtt

Crystal lang implementation of the MQTT protocol, a lightweight protocol for publish/subscribe messaging
Crystal
19
star
10

ffmpeg

ffmpeg crystal bindings
Crystal
18
star
11

qr-code

a QR Code implementation written in crystal lang
Crystal
17
star
12

crystal-ldap

a Crystal lang LDAP client
Crystal
16
star
13

priority-queue

Priority Queue and Heap implementation for Crystal Lang
Crystal
13
star
14

bisect

Library for maintaining sorted Arrays
Crystal
12
star
15

json-schema

Describe crystal-lang JSON serializable types with JSON Schema
Crystal
12
star
16

pinger

Microlibrary to perform ping requests with Crystal Lang
Crystal
11
star
17

telnet.cr

Telnet protocol helper for crystal lang
Crystal
11
star
18

pars

Parser combinator library for crystal-lang
Crystal
11
star
19

inactive-support

Utilities for crystal-lang
Crystal
10
star
20

pg-orm

Postgres ORM for Crystal Lang
Crystal
9
star
21

mdns

Crystal Lang mDNS and DNS-SD Support
Crystal
8
star
22

tensorflow_lite

tensorflow lite bindings for crystal lang
Crystal
8
star
23

ed25519

Ed25519 high-performance public-key signature system for crystal lang
Crystal
7
star
24

crystal-gpt

ChatGPT plugin template that allows you to focus on writing actions, automatically generating the required metadata
Crystal
7
star
25

secure-remote-password

Crystal implementation of the Secure Remote Password protocol (SRP-6a)
Crystal
6
star
26

simple_retry

a tool for retrying code blocks
Crystal
6
star
27

connect-proxy

crystal lang connect / HTTP proxy implementation
Crystal
6
star
28

crystal-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALLยทE, Whisper API Client for Crystal
Crystal
6
star
29

crunits

Physical quantity and units of measure conversion and math for crystal lang
Crystal
6
star
30

secrets-env

Extension to the crystal lang ENV module to support reading secrets
Crystal
6
star
31

v4l2.cr

crystal lang video for linux device helpers / bindings
Crystal
5
star
32

log_helper

Extension for Crystal Log to aid logging key-value data
Crystal
5
star
33

readers-writer

A simple readers writer lock for crystal lang
Crystal
5
star
34

cmac

Crystal implementation of the Cipher-based Message Authentication Code (CMAC)
Crystal
5
star
35

guide

Spider Gazelle Documentation
Python
4
star
36

ntlm

NTLM authentication for crystal lang
Crystal
4
star
37

worker_pool

a basic fiber pool implementation for crystal lang
Crystal
4
star
38

digest-auth

HTTP digest auth for crystal lang
Crystal
4
star
39

matter

A complete Crystal implementation of the Matter protocol specification (https://buildwithmatter.com). Includes full support for controller, device, commissioning, secure communications, device types, and cluster definitions.
Crystal
4
star
40

eventbus

Listen for Postgres database change events and publish them to event listeners
Crystal
3
star
41

knx

KNX protocol support for crystal lang
Crystal
3
star
42

tokenizer

Simplified binary stream tokenization for crystal lang
Crystal
2
star
43

crystal-dtls

DTLS support for crystal lang
Crystal
2
star
44

upload-signer

Provide API for generating pre-signed URLs for file uploads to cloud storage
Crystal
2
star
45

stomp

crystal lang implementation of the STOMP protocol
Crystal
1
star
46

tlv

Matter TLV encoder/decoder
Crystal
1
star
47

gpio.cr

crystal lang bindings for linux gpiod
Crystal
1
star
48

stumpy_resize

resizes stumpy canvas images in pure crystal
Crystal
1
star
49

HKDF

HMAC-based Extract-and-Expand Key Derivation Function (HKDF) for crystal lang
Crystal
1
star
50

SPAKE2_plus

a crystal lang implementation of SPAKE2+, a Password Authenticated Key Exchange (PAKE) protocol
Crystal
1
star
51

panopticon

Distributed tracing for services built in crystal-lang
Crystal
1
star
52

tflite_image

image classification and feature detection with tflite and crystal lang
Crystal
1
star
53

tflite_pipeline

video processing AI pipeline leveraging tflite_image
Crystal
1
star
54

link-header

Crystal Lang HTTP Link Header Parser
Crystal
1
star