Automated Telnet

There was a recent post in the ScriptingAnswers.com forums about a Telnet COM object. I’m assuming the poster was looking for a way to automate a telnet session. Well there isnt’ a free and easy way. What you need is some way to pass a string of commands to the Telnet session.  Well, when all else fails, the SendKeys method is a last resort.

I won’t belabor the point about Telnet security or the benefits of using SSH or something else. You do what you have to do. Even though my demo script uses the XP Telnet client, you could probably adapt it for any remote console client.

Here’s the demo script in it’s entirety.  I’ll go through a few key sections below: 

‘TelnetDemo.vbs

Dim objShell
Dim objNetwork

Set objNetwork=CreateObject(“WScript.Network”)

strTitle=”Telnet Demo”
strDefaultServer=”Server01″
strDefaultUser=objNetwork.UserDomain & “\” & objNetwork.UserName
strDefaultPassword=”P@ssw0rd”

strComputer=InputBox(“What server or device do you want to connect to?”,_
strTitle,strDefaultServer)
If Len(strComputer)=0 Then WScript.quit

strUsername=InputBox(“What credential do you want to use”,strTitle,_
strDefaultUser)
If Len (strUsername)=0 Then WScript.Quit

strPassword=InputBox(“What password do you want to use?”,strTitle,_
strDefaultPassword)
If Len (strPassword)=0 Then WScript.Quit

Set objShell=CreateObject(“wscript.shell”)
‘Start Telnet
objShell.Run “Telnet ” & strComputer
‘Give app a chance to get started
WScript.Sleep 5000
objShell.AppActivate “Telnet ” & strComputer

‘Send login credentials
objShell.SendKeys strUsername & “~”
WScript.Sleep 2000
objShell.SendKeys strPassword & “~”
WScript.Sleep 2000

‘Send commands
objShell.SendKeys “net localgroup administrators”
WScript.Sleep 200
objShell.SendKeys “~”
objShell.SendKeys “dir /w”
WScript.Sleep 200
objShell.SendKeys “~”
objShell.SendKeys “netstat”
WScript.Sleep 200
objShell.SendKeys “~”
‘give lengthy commands time to finish
WScript.Sleep 10000

‘make sure we get window again
objShell.AppActivate “Telnet ” & strComputer
‘run another command
objShell.SendKeys “net share”
WScript.Sleep 200
objShell.SendKeys “~”

‘Close session
‘make sure we get window again
objShell.AppActivate “Telnet ” & strComputer
objShell.SendKeys “exit”
WScript.Sleep 200
objShell.SendKeys “~”

If you can launch a Telnet session with integrated credentials you can simplify a few things. This script assumes you need to enter a username and password so it starts by prompting you for connection information:

strComputer=InputBox(“What server or device do you want to connect to?”,_
strTitle,strDefaultServer)
If Len(strComputer)=0 Then WScript.quit

strUsername=InputBox(“What credential do you want to use”,strTitle,_
strDefaultUser)
If Len (strUsername)=0 Then WScript.Quit

strPassword=InputBox(“What password do you want to use?”,strTitle,_
strDefaultPassword)
If Len (strPassword)=0 Then WScript.Quit

With this information we’re ready to launch Telnet with the Run method.

objShell.Run “Telnet ” & strComputer

We need to give the command time to start so the script sleeps 5 seconds.

WScript.Sleep 5000

Then we need to grab the window. You have to know what is in the title bar of the window you want to activate. With Telnet, I know it is “Telnet computername”.

objShell.AppActivate “Telnet ” & strComputer

I can then send the logon credentials. The ~ character is the same as {Enter}. Notice I’m also sleeping for 2 seconds to give the console a chance to keep up. I’ve found that you need to go slow with this approach and give Telnet time to process the keys you are sending.

‘Send login credentials
objShell.SendKeys strUsername & “~”
WScript.Sleep 2000
objShell.SendKeys strPassword & “~”
WScript.Sleep 2000

At this point I should have an active Telnet session and can begin sending commands.  Again, I’ve found it more effective to split up commands and pause between them.

‘Send commands
objShell.SendKeys “net localgroup administrators”
WScript.Sleep 200
objShell.SendKeys “~”
objShell.SendKeys “dir /w”
WScript.Sleep 200
objShell.SendKeys “~”
objShell.SendKeys “netstat”
WScript.Sleep 200
objShell.SendKeys “~”
‘give lengthy commands time to finish
WScript.Sleep 10000

If there is a chance that something else could have grabbed focus, you’ll want to call AppActivate again. You definitely want to do this when you’re ready to end your session.

‘make sure we get window again
objShell.AppActivate “Telnet ” & strComputer

At this point I can send the Exit command and the Telnet session will terminate. The Telnet window will dismiss itself in a few seconds.

objShell.SendKeys “exit”
WScript.Sleep 200
objShell.SendKeys “~”

A few final words on SendKeys.  You always need to make sure the right window has focus, otherwise key strokes  will go to the wrong place. There is no easy way to verify that the telnet connection was successful. So if you ran this script and attempted to connect to an unavailable server or have the wrong credentials, the rest of the script will keep going, trying to send key strokes to whatever window currently has focus. You can mitigate this problem by closing all other apps other than a CMD prompt where you’re running the script.

Timing is everything with SendKeys, and especially with tools like Telnet. You’ll need to test in your environment to see how much time you need to pause between commands. Remember that wscript.sleep takes a value in milliseconds. A script like this may not be the speediest, but it can get the job done when there are no other alternatives.

Technorati tags: , , ,