Scripting cheap switches that only have a web page
Before the VLAN cutover, three small switches had to be set up for it: a 16-port TP-Link in the basement and two Netgear “Plus” models upstairs and downstairs. None of them has an API or SSH. They have a web page, and the agent drove that web page with curl and a cookie jar, which works better than I’d expected and has some odd corners.
The Netgears are plain HTTP forms. The login posts the password in plain text (on this firmware), and every form carries a hidden hash field that has to be read from the page and sent back. There’s one admin session at a time, so the script has to log out at the end; if it doesn’t, the next login returns a page without the form for about five minutes. The VLAN membership page was the tricky one. On the 5-port model, simply loading that page resets the selected VLAN to 1, so fetching the hash between selecting a VLAN and applying a change turns the apply into another select. And even done in the right order, it takes two POSTs: the first one with a new VLAN ID only selects that VLAN, whatever membership you sent with it, and the second one applies. The 8-port model differs in small ways (a different cookie name, a hidden field naming the VLAN currently shown, and a read-back that can come back empty if requests follow each other too quickly). The agent ended up reading every value back after every change, which is the only reason we know any of this.
They also only ask for DHCP at boot. If there’s no answer, they fall back to a fixed factory address and stay there, invisible to the DHCP server. Two of the switches I’d been calling “unmanaged” were in fact these, sitting on that factory address the whole time. The agent found them by giving my desktop a temporary address in that range and scanning it. Both now have static addresses.
The TP-Link had a different problem: its password had been set at some point, and it wasn’t written down anywhere. Its firmware forces a new password on the first login, so the factory one doesn’t work once it’s been set up. The agent tried one guess and stopped (no brute-forcing my own switch), and I factory-reset it. After a reset the factory login works but every page answers with an error code until you set a new password through a slightly different form. After that, every setting is a plain GET request, including port mirroring, and each page carries its current state as a JavaScript object at the top, which is handy for reading back.
All three now run in 802.1Q mode and the logic lives in one small library. Replacement switches with a real controller are on order, which tells you how much I enjoyed this.