Skip to content
A MikroTik API login sentence written to the wire one word at a time, /login, =name=admin and =password=secret, each with its length byte, then a zero byte, and RouterOS answering !done

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.

mikrotik-api.py socket + struct ROUTEROS port 8728, raw TCP /login =name=admin =password=… !done /interface/bonding/print !re one per bond !re !done, or !trap on an error One word on the wire: its length, then its bytes 06 2f 6c 6f 67 69 6e /login 0b 3d 6e 61 6d 65 3d 61 64 6d 69 6e =name=admin 00 end of sentence The length byte's high bits say how many more follow, as in UTF-8
mikrotik-api.py socket + struct ROUTEROS port 8728, raw TCP /login =name=admin =password=… !done /interface/bonding/print !re one per bond !re !done, or !trap on an error One word on the wire: its length, then its bytes 06 + 6 bytes /login 0b + 11 bytes =name=admin 00 end

Lineage

If you liked this project, consider sharing it on Hacker News, Bluesky, LinkedIn, or X.

← All works