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
sh
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")
WshShell.Run "PATH-TO-PuTTY HOSTNAME -l USERNAME -pw PASSWORD"
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 192.168.1.1, your window title will be 192.168.1.1 – 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!
Comments
13 responses to “Automated PuTTY Session to a Cisco Router via VBScript”
Hi.
How can I run scripts (.vbs files) using putty? Thanks!
I have scripts that can I smoothly run on secureCRT but I don’t know how to run them using putty.
Worked for me to bring up disabled ports in remote locations. Good Script! Thank you!!
in this line below
WshShell.Run “PATH-TO-PuTTY HOSTNAME -l USERNAME -pw PASSWORD”
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!