MikroTik Native API Client
MikroTik's binary router API, in 137 lines of standard-library Python.
2026
My homelab's 10G backbone is a MikroTik CRS317 switch, and I wanted to change its bonds, DHCP and firewall rules from scripts instead of typing into SSH. RouterOS has an API on port 8728 that speaks its own binary protocol over raw TCP: sentences made of words, each word led by its length, each sentence ended by a zero byte.
I wrote the protocol myself instead of using a library. The Python ones are heavy, and I only understand a byte-level format once I've built it. The length encoding turned out to be the trick UTF-8 uses, where the first byte's high bits say how many more bytes follow. The client came out at 137 lines with no dependencies, small enough to call from a cron job.
How it's made
Python · Binary Protocol · MikroTik · Networking
It uses only socket and struct: it logs in, sends each command as a sentence, and reads !re replies until !done, or !trap for an error. Any RouterOS command works from the command line with no code changes. How it works, byte by byte.
Lineage
If you liked this project, consider sharing it on Hacker News, Bluesky, LinkedIn, or X.
← All works