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!

6 Replies to “Automate a Telnet Session with One Command”

  1. HI, I tried using your above commands with little modification to automate login in server, but its giving connection closed error

    What I modified is as below:

    { echo “USERNAME”; echo “PASSWORD”;} | telnet HOST I.P.

  2. Hello, thanks for the reply.
    I tried the below and I could log into the server successfully but the issue here is, the session comes out of the server session after few secs.

    { echo “user_name”;sleep 5;echo “password”;sleep 5;} | telnet host_aix_ip

    Thanks in Advance

    1. Not sure what you mean – the session will likely close – the script isn’t designed to open the connection and stay open, it’s designed to automate some task then close the connection.

  3. Hi, thanks, was looking a way to automate my unix login from my local shell….anyways thanks for the help!

Leave a Reply