Network toolbox: SNMP, NTP, LDAP and IP addresses

Jennifer

Five bundled modules for the jobs a network admin scripts every week: addresses and subnets, domain names, interface counters, clocks and directories.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 21

Five modules, one toolbox

ModuleWhat it doesNeeds
ipnetaddresses, CIDR blocks, subnet maths, scopesnothing: pure Jennifer
idnainternational domain names to xn-- and backnothing: pure Jennifer
snmpv1 / v2c manager, and a small agentUDP (net)
ntpone SNTP query: server time, offset, delayUDP (net)
ldapv3 client with StartTLS / LDAPS, and a small serverTCP and TLS (net)
import "ipnet.j" as ipnet;   # bundled: no jvc, no vendor/
use io;

def a as ipnet.Address init ipnet.parseAddress("10.0.0.1");
io.printf("%v\n", ipnet.isPrivate($a));   # true
2 / 21

ipnet: addresses and networks

def net as ipnet.Network init ipnet.parse("198.51.100.77/26");
def a as ipnet.Address init ipnet.parseAddress("198.51.100.70");
io.printf("%s contains %s: %v\n", ipnet.networkString($net),
    ipnet.toString($a), ipnet.contains($net, $a));
def v6 as ipnet.Address init ipnet.parseAddress("2001:DB8:0:0::0:01");
io.printf("%s is IPv%d\n", ipnet.toString($v6), ipnet.version($v6));
def m as ipnet.Address init ipnet.parseAddress("::ffff:198.51.100.99");
io.printf("mapped: %s is IPv%d, inside: %v\n", ipnet.toString($m),
    ipnet.version($m), ipnet.contains($net, $m));
198.51.100.64/26 contains 198.51.100.70: true
2001:db8::1 is IPv6
mapped: 198.51.100.99 is IPv4, inside: true
3 / 21

ipnet: subnet arithmetic

def n as ipnet.Network init ipnet.parse("198.51.100.64/26");
io.printf("mask %s, %d addresses, last host %s\n",
    ipnet.toString(ipnet.netmask($n)), ipnet.hostCount($n),
    ipnet.toString(ipnet.lastUsable($n)));
def subs as list of string;
for (def s in ipnet.split(ipnet.parse("2001:db8:10::/48"), 49)) {
    $subs[] = ipnet.networkString($s);
}
io.printf("split: %a\n", $subs);
mask 255.255.255.192, 64 addresses, last host 198.51.100.126
split: [2001:db8:10::/49, 2001:db8:10:8000::/49]
4 / 21

ipnet: what kind of address is it?

for (def s in ["10.20.30.40", "fd00::1", "fe80::1", "::ffff:192.168.1.1",
    "100.64.0.1", "192.0.2.1", "ff02::1"]) {
    def a as ipnet.Address init ipnet.parseAddress($s);
    io.printf("%s|pad=20 %v\n", $s, ipnet.scope($a));
}
10.20.30.40          ipnet.Scope.Private
fd00::1              ipnet.Scope.Private
fe80::1              ipnet.Scope.LinkLocal
::ffff:192.168.1.1   ipnet.Scope.Private
100.64.0.1           ipnet.Scope.Reserved
192.0.2.1            ipnet.Scope.Reserved
ff02::1              ipnet.Scope.Multicast
5 / 21

idna: international domain names

io.printf("%s\n", idna.toAscii("Bücher.Example"));
io.printf("%s\n", idna.toAscii("straße.example.org"));
io.printf("%s\n", idna.toUnicode("xn--mnchen-3ya.example"));
def long as string init "über-" + strings.replicate("x", 60);
try {
    io.printf("%s\n", idna.toAscii($long + ".example"));
} catch (e) {
    io.printf("[%s] %s\n", $e.kind, $e.message);
}
xn--bcher-kva.example
xn--strae-oqa.example.org
münchen.example
[idna] idna: label too long for a 63-octet A-label
6 / 21

idna: why spoofing matters

func beyondLatin(host as string) {
    for (def ch in strings.chars(idna.toUnicode($host))) {
        if (convert.toCodepoint($ch) > 0x024F) { return true; }
    }
    return false;
}
def fake as string init convert.fromCodepoint(0x0430) + "pple.example";
io.printf("%v %s %v\n", $fake == "apple.example", idna.toAscii($fake),
    beyondLatin(idna.toAscii($fake)));
io.printf("%v\n", beyondLatin("xn--mnchen-3ya.example"));
false xn--pple-43d.example true
false
7 / 21

SNMP: get and walk

def c as snmp.Client init snmp.clientWith("127.0.0.1:16161", "public",
    snmp.VERSION2C, 2000, 1);
def v as list of snmp.Varbind init snmp.get($c, ["1.3.6.1.2.1.1.3.0",
    "1.3.6.1.2.1.31.1.1.1.6.2"]);
io.printf("up %d ticks, %s [%s] %d\n", $v[0].number, $v[1].oid,
    $v[1].type, $v[1].number);
for (def vb in snmp.walk($c, "1.3.6.1.2.1.2.2.1.2")) {
    io.printf("%s = %s\n", $vb.oid, $vb.value);
}
up 93657 ticks, 1.3.6.1.2.1.31.1.1.1.6.2 [counter64] 19062460
1.3.6.1.2.1.2.2.1.2.1 = lo
1.3.6.1.2.1.2.2.1.2.2 = eth0
8 / 21

SNMP: bandwidth from two samples

func delta32(prev as int, cur as int) {
    if ($cur >= $prev) {
        return $cur - $prev;
    }
    return $cur + WRAP32 - $prev;
}
def a as list of snmp.Varbind init sample($c, "2");
time.sleep(time.fromSeconds(5));
def b as list of snmp.Varbind init sample($c, "2");
def secs as float init ($b[0].number - $a[0].number) / 100.0;
def bytesIn as int init delta32($a[1].number, $b[1].number);
3171075 octets in 5.00 s: 5.07 Mbit/s
after a wrap: 1000
9 / 21

SNMP: a tiny agent for your own values

func snapshot() {
    return snmp.agent("lab-ro-7f3a", snmp.VERSION2C, [snmp.varbind(
        BASE + ".1.0", "gauge32", "", len(fs.list("spool")))]);
}
while (true) { # lint-disable: L105
    def stop as channel of bool init channel.make(1);
    def t as task of null init spawn {
        snmp.serveOn(snapshot(), $sock, $stop);
    };
    time.sleep(time.fromSeconds(10));
    channel.send($stop, true);
    task.wait($t);
}
10 / 21

NTP: ask a server for the time

import "ntp.j" as ntp;
use io;
use time;

def r as ntp.Result init ntp.query("pool.ntp.org");
io.printf("server time %s\n", time.iso($r.serverTime));
io.printf("offset %d ms, delay %d ms\n", time.milliseconds($r.offset),
    time.milliseconds($r.delay));
server time 2026-11-19T12:23:08.037108196Z
offset 126 ms, delay 282 ms
11 / 21

NTP: a drift check that warns

func checkNtp(server as string) {
    def r as ntp.Result init ntp.queryWith($server, 2000);
    def ms as int init time.milliseconds($r.offset);
    def rtt as int init time.milliseconds($r.delay);
    def level as string init "ok  ";
    if (math.abs($ms) >= WARN_MS) { $level = "WARN"; }
    if (math.abs($ms) >= FAIL_MS) { $level = "FAIL"; }
    return ["{$level} ntp {$server}: offset {$ms} ms, rtt {$rtt} ms"];
}
ok   ntp 127.0.0.1:10123: offset 0 ms, rtt 3 ms
12 / 21

LDAP: never bind in plain text

def s as ldap.Conn init ldap.connect(HOST + ":10389",
    transport.Security.Starttls);
def o as net.TLSOptions;
$o.caCert = fs.readBytes("lab-ca.pem");
def t as ldap.Conn init ldap.Conn{timeoutMs: 5000,
    handle: net.connectTLS(HOST + ":10636", $o, 5000)};
def p as ldap.Conn init ldap.connect(HOST + ":10389", PLAIN);
def codes as list of int;
for (def c in [$s, $t, $p]) {
    $codes[] = ldap.bind($c, $dn, $pw).code;
}
io.printf("StartTLS, LDAPS, plain: %a\n", $codes);
StartTLS, LDAPS, plain: [0, 0, 13]
13 / 21

LDAP: users and groups

def who as string init "a*";
def f as asn1.Value init ldap.parseFilter("(uid="
    + ldap.escapeFilterValue($who) + ")");
def hits as list of ldap.Entry init ldap.search($c, BASE,
    ldap.SCOPE_SUB, $f, ["cn"]);
io.printf("%s: %d hits\n", ldap.escapeFilterValue($who), len($hits));
io.printf("groups: %a\n", dirauth.groupsOf($c, BASE, $alice));
a\2a: 0 hits
groups: [netops, helpdesk]
14 / 21

LDAP: check a password with bind

export func passwordOk(addr as string, sec as transport.Security,
    dn as string, password as string) {
    if ($dn == "" or $password == "") {
        return false;
    }
    def c as ldap.Conn init ldap.connect($addr, $sec);
    defer ldap.close($c);
    return ldap.bind($c, $dn, $password).code == ldap.SUCCESS;
}
'alice-pw-2026': true
'guess': false
'': false
eve: ''
15 / 21

LDAP: large result sets

def hits as list of ldap.Entry init ldap.searchPaged($c, BASE,
    ldap.SCOPE_ONE, $f, [], 20);
io.printf("paged by 20: %d entries\n", len($hits));
try {
    $hits = ldap.search($c, BASE, ldap.SCOPE_ONE, $f, []);
} catch (e) {
    io.printf("%s\n", $e.message);
}
paged by 20: 43 entries
ldap: search failed: code 4 ()
16 / 21

LDAP: a directory server for your tests

func testLoginAndGroups() {
    def l as net.Listener init ldap.listen("127.0.0.1:0");
    defer net.close($l);
    spawn { ldap.serveOn(fixture(), $l); };
    def a as string init net.address($l);
    def c as ldap.Conn init ldap.connect($a, PLAIN);
    def dn as string init dirauth.userDn($c, BASE, "alice");
    testing.assertTrue(dirauth.passwordOk($a, PLAIN, $dn, "s3cret"));
    testing.assertFalse(dirauth.passwordOk($a, PLAIN, $dn, "guess"));
    testing.assertEqual(dirauth.groupsOf($c, BASE, $dn), ["netops"]);
}
PASS testLoginAndGroups (59 ms)
17 / 21

Errors, timeouts and limits

What happenedkindReal message
no UDP answer, or a wrong communitytimeoutsnmp: no response from 127.0.0.1:16161 after 2 attempt(s)
CA not trustednetnet.startTLS: tls: failed to verify certificate: ...
the agent refuses a SETsnmpagent returned error-status 6 (noAccess) at index 1
no such search baseldapldap: search failed: code 32 ()
a malformed addressipnetIPv4 octet out of range 0..255: 192.0.2.256
18 / 21

Project: a network health report

func inMgmt(ip as string) {
    def a as ipnet.Address init ipnet.parseAddress($ip);
    for (def n in MGMT) {
        if (ipnet.contains(ipnet.parse($n), $a)) {
            return true;
        }
    }
    return false;
}
19 / 21

The report

ok   ntp 127.0.0.1:10123: offset 0 ms, rtt 3 ms
ok   snmp 127.0.0.1:16161 if 2: up true, in 6.02, out 6.02 Mbit/s
FAIL snmp 127.0.0.1:16171 (timeout): snmp: no response from ...
WARN snmp 203.0.113.9:161: not a management address, skipped
ok   ldap localhost:10389: 43 people, netops 2
SNMP_COMMUNITY=... LDAP_BIND_DN=cn=reader,dc=example,dc=org \
  LDAP_PW=... SSL_CERT_FILE=lab-ca.pem jennifer run report.j
echo $?    # 1: one FAIL, one WARN
20 / 21

Take-aways and read more

Based on Jennifer 0.27.0, November 2026.

21 / 21