June 27, 2022


The Blog of a Chronic Content Creator

Automate a Telnet Session with One Command

The Telnet protocol might not be the newest thing on the block, however there is still some legacy network hardware that will only accept Telnet connections and not newer more secure SSH connections. Recently I bought an APC MasterSwitch unit, that allows me to remotely power on and power off my hardware, and I ran into a problem of automation. I wanted to be able to remotely reboot the server every night a little after 3 AM, but there was no schedule capability inside the software that shipped with the decade old unit. The MasterSwitch, however did come with a built-in telnet server that one can remotely access and power on and off each individual outlet.

The problem with automating Telnet, is that the prompts are not always uniform between different Telnet servers as they are with SSH servers. So one needs to know the exact keystrokes used in order to accomplish the task they need to do. However if you know all of the keystrokes you can create a command that will type them directly to the telnet client on an *nix computer. Here is my crontab command that navigates through the menus of the MasterSwitch unit and reboots the selected outlet at 3:16 in the morning:

16 3 * * * { echo "USERNAME"; echo "PASSWORD"; echo "1"; echo "2"; echo "1"; echo "6"; echo "YES"; sleep 1; } | telnet SERVERNAME

You’ll see that this command reads sequential to the actual keys that I put in when accessing the unit, first my username, then my password, then the menu commands which use numbers to drill down to the actual outlet, and then confirm that you would like to reboot it. The sleep 1 command at the end spaces them out so that they don’t overload the client.

There you have it, a quick and dirty way to automate a telnet session!

%d bloggers like this: