Automated PuTTY Session to a Cisco Router via VBScript

Because of my recent communication issues with a cable telecommunications provider who just can’t seem to get things right, I’ve had to log into my router a number of times to reset the line. And in case you’re wondering, it’s because I use a Cisco router at home, rather than the standard Linksys fare. It provides a better connection – provided the line itself is working. Unfortunately, if the line goes down, it doesn’t do quite as good as job as recovering, which is odd, since it’s a lot more expensive.

So it means that I need to log into the router and shutdown the line and reset it anytime the line goes down – which over the last week and a half has been several times a day. This isn’t a difficult process, but it is a pain. Either I have to load up a serial session or a telnet one, and then type four commands. Like I said, it’s not hard, but it is tedious. In fact, it’s really tedious. So I decided to come up with a solution.

First, what needs to be done.

conf term
int e0
no sh

See? Not hard – just enter configuration mode for the ethernet line, shut it down and restart. This tells it to go out and grab a new DHCP address, which gets everything going again. At least until the next time the connection dies. But it can get really annoying when you have to do it over and over (and over) again. So I decided to see if I could come up with a way of automating the process. Enter PuTTY.

This free client handles all sorts of SSH connections, and it just so happens that I can make one to my router. The only problem is automating the process. For that, you need to add a Windows Script. Think of it like an advanced batch file. You see, you can use Plink (a PuTTY tool) to run commands, but only individual commands. If you want to run a whole set of them (or four, as the case may be), you’re out of luck. Though I didn’t really want to use Windows Script, it worked really well for this purpose. Here’s the script I used, courtesy of Experts Exchange.

set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 2000
WshShell.AppActivate "HOSTNAME - PuTTY"
WshShell.SendKeys "conf term{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "int e0{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "sh{ENTER}"
WScript.Sleep 10000
WshShell.SendKeys "no sh{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "exit{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "exit{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "exit{ENTER}"

It looks a lot more complex than it is.

First, you declare the object. Then you have to set up your session. Tell it where to find PuTTY (use the full path, it’s easier) and connect to HOSTNAME using USERNAME with PASSWORD. This method does store the password in clear text, so you’ll want to be careful if you store the script on your desktop or somewhere easy to find, since anyone can then find out your password. PuTTY is able to use Pageant for public key authenticaion, but I could not get it to work with my router. If you figure it out, let me know!

Next up you see the first WScript.Sleep command. This sets a sleep for 2000 milliseconds, or 2 seconds. It just gives PuTTY time to make the connection before sending the next command. Adjust as needed.

The WshShell.AppActivate instruction tells the script to activate a window with a particular title. In this case, with HOSTNAME – PuTTY. This is the format used by PuTTY. So if your hostname is an IP address of, your window title will be – PuTTY. That is what you would have here. Just replace HOSTNAME in both places with the same value and you should be fine.

Once activated, the true magic happens. First, we enter configuration mode, then open the interface that we need. In my case, it is int e0. If for some reason you have a different interface on your router, replace it here. Then the interface is shut down and then restarted in Cisco’s lovely IOS language, by reversing the command. In between each command is a sleep cycle. After the shut down is a long one (10 seconds), because on my router, that process takes a while. Again, you may need to adjust the delay. Also notice the {ENTER}. This actually sends an ENTER keystroke. Without it, the process will sit, waiting for the ENTER key to be pressed.

After the interface is brought back up, three exit commands are sent, which ends the session. That’s it.

I usually only run this manually, but it could easily be scheduled to reconnect nightly, weekly, monthly or whatever. Go wild!

12 Replies to “Automated PuTTY Session to a Cisco Router via VBScript”

  1. Hi Chad, your example is great, but besided similar goal I also wanted to schedule the script execution. By the time such script runs there is no user logged to the workstation, thus it is not working. Do you know if it is possible to run something like this without user logged on with usage of PLINK? I am only able to execute 1 command from a command file with PLINK, which is of no use right now. Thank for any input.

  2. Unfortunately, I don’t use PLINK, so I can’t say. But I do recall running into some examples that used PLINK, so I’d suggest looking for some by running some searches and seeing what you can find. As I recall, they were actually running command lines on the remote router, but that’s not possible in my case. It might be in yours. Best of luck!

  3. Hi Pascal –

    I actually just keep it on the desktop and click it (double-click, technically) when I need to run it. Not very high-tech, but I usually have a pretty stable connection, so I don’t need to run it often. If I needed it more frequently, I would probably schedule it to run daily or something with the scheduler.

  4. Ah, sorry about that! To run it automatically, just give it an extension of .vbs. I think you can run scripts in other ways as well, but you’d have to execute it via a command line instead of clicking (similar to how Perl works).

  5. Fine !

    I managed in fact a wsf file, but this one fails to invoke putty.
    Here is the faulty line:
    WshShell.Run “C:Program FilesPuTTYputty.exe”
    Then I have the mesage:
    “The system cannot find the file specified.”
    That’s strange, because I just took the string from PuTTY’s shortcut properties.
    I am really confused to worry you with so basic questions.

  6. Hi Pascal –

    My copy of PuTTY is not in the Program Files directory, so it looks something like this:

    WshShell.Run “C:PuTTYPuTTY.exe…”

    If you have a space in there, you will actually need an extra set of quotes, I believe. Try this:

    WshShell.Run “”C:Program FilesPuTTYPuTTY.exe”…”

    This is because you need your entire command in quotes, but you also need to put the spaces of “Program Files” in quotes so that it runs correctly. Make sense? Also, do not forget your host name, user name, etc!

  7. in this line below

    WshShell.Run “PATH-TO-PuTTY HOSTNAME -l “

    couldn’t you use putty to call your saved profile which stores your credentials? this would eliminate the need to store clear text passwords. it’s possible that putty has evolved much more since you originally made this post. Thanks by the way!

Leave a Reply

Your email address will not be published. Required fields are marked *