Reboot an OpenWrt Router from Windows with a Batch File (Plink / PuTTY)
Readers of my blog sometimes message me with questions. They are all different: some about system administration, some about contests, markets and investing, and so on. As a rule I try to give some advice.
Not long ago I got this question: a reader had read my article Automatic router reboot on OpenWrt and wanted to reboot the router not with cron, but with a .bat file that a program on Windows would run.
I had never needed this, so I asked for details and started thinking about options.
OpenWrt is flexible — a convenient Linux with a web interface — so there are many ways to reboot it from Windows.
My idea: connect to the router, for example over SSH, and run the reboot command.
The rest is technique, so I started trying things.
Plink: one line in a .bat file
My favorite SSH client, like many people’s, is PuTTY. I tried it; there are options, but for this job it turns out that Plink — plink.exe from the PuTTY package — is a better fit.
Plink lets you send a command to the router literally with one line in a .bat or .cmd file. Here is an example:
plink -ssh -batch 10.0.0.1 -l root -pw password reboot
where:
-ssh— the protocol;10.0.0.1— the IP address of the OpenWrt router (or any other Linux machine with SSH on the default port);-l root— the user name (better root, or a user with root rights, so the reboot goes through);-pw password— the password.
They say keeping a password in plain text is not good practice. For a router I think it’s not a big deal, but you can use key-based login instead — Plink supports it too:
plink -ssh -batch -i C:\keys\router.ppk [email protected] reboot
The same with PuTTY
PuTTY itself can also do this, with a command file:
putty -m reboot.txt -P 22 10.0.0.1 -l root -pw password
The parameters are very similar to Plink’s. You only need to create a file with the given name (reboot.txt) and write reboot in it — this is the command PuTTY sends when it connects to the server.
At the first connection you must click OK when the SSH client asks about the server key; after that connections are silent.
So we have 2 ways to connect to the router and send the reboot command. Then create a .cmd file and put it, for example, on the desktop, or create a reboot task in Windows Task Scheduler.
You can also write a script that pings some website and, if it becomes unreachable, sends the router to reboot. Your imagination is the limit
although of course all these things are better done inside the router itself — but that is a matter of taste, I just suggested options.
Get Plink from the official PuTTY download page (it is in the same package as PuTTY).
