Telnet Automation with PowerShell made simple…

Whaoo! After hours searching the internet for some code that could simply help me automate my Telnet sessions, and not finding a simple solution was frustrating. I had to give up and admit that this one goes to VBScript. YES! I said this one goes to VBScript. But, WAIT A SECOND! PowerShell can provide some leverage when you combine these two scritping technologies. Again, it’s all about integrating technologies to help you be productive and efficient at work.

Let’s cut to the chase and here’s the low down. Many you have been wondering How Can I Automate my Telnet session? Well, VBScript have provided some means to do this for some time. Now, I can use PowerShell to make it more pretty.

Here’s the sample VBScript code use to send commands to a Telnet session: (keep in mind, I’m connecting to Microsoft Telnet in this scenario)

set oShell = CreateObject("WScript.Shell")
oShell.run("Telnet")
WScript.Sleep 1000
oShell.SendKeys("Open 127.0.0.1 23")
WScript.Sleep 1000
oShell.SendKeys("{Enter}")
WScript.Sleep 1000
oShell.SendKeys("n")
WScript.Sleep 1000
oShell.SendKeys("{Enter}")
WScript.Sleep 1000
oShell.SendKeys"MyName"
WScript.Sleep 1000
oShell.SendKeys("{Enter}")
WScript.Sleep 1000
oShell.SendKeys("MyPassword")
WScript.Sleep 1000
oShell.SendKeys("{Enter}")
WScript.Sleep 1000
oShell.SendKeys("MyCommand")
WScript.Sleep 1000
oShell.SendKeys("{Enter}")
WScript.Sleep 1000

This is, in it basic form,  and with no complexity. Now, using PowerShell we can make it a little more functional by automating the process. I decided to add a text file containing a list of Telnet commands I want to automatically execute during my session. Then, I created a PowerShell function that will build and then execute my auto-generated VBScript.

My command list text file “cmdlist.txt” contains 2 commands for testing purpose and it’s stored in my C:\Temp folder:
Help
Exit

Now, Here’s my “Connect-MyTelnet” function (in its basic form) prototype which put everything together:

function Connect-MyTelnet{
Param(
 [string] $IPAddress,
 [string] $Port,
 [string] $UserName,
 [string] $Password,
 [string] $cmdlistPath
)
	## - Setting default values:
	if($port -eq $null){ $Port = "23"; };
	if($cmdlistPath -eq $null) { $CmdlistPath = 'c:\temp\cmdlist.txt'; };

	## create vbscript file: MyTelnetSession.vbs
## - For Microsoft Telnet:
$MyVBScript = @"
set oShell = CreateObject("WScript.Shell")`r`n
oShell.run("Telnet")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("Open $IPAddress $Port")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("n")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$UserName")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$Password")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
"@;

	## - Get file with telnet commands:
	[array] $Cmdlist = Get-Content $cmdlistPath;

	## loop through and build each telnet command line:
	foreach($cmd in $cmdlist)
	{
		## - Build VBscript lines:
		$MyVBScript += 'oShell.SendKeys("'+$cmd+'")'+"`r`n";
		$MyVBScript += "WScript.Sleep 1000`r`n";
		$MyVBScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
		$MyVBScript += 'WScript.Sleep 1000'+"`r`n";
	}

	## - Close Telnet Session:
		$MyVBScript += 'oShell.SendKeys("  QUIT")'+"`r`n";
		$MyVBScript += "WScript.Sleep 1000`r`n";
		$MyVBScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
		$MyVBScript += 'WScript.Sleep 1000'+"`r`n";

	## - Save and execute generated VBscript:
	$MYVBScript | Out-File -FilePath c:\temp\MyTelnet.vbs -Encoding ASCII;
	& c:\temp\MyTelnet.vbs
}; Set-Alias ct Connect-MyTelnet;

Save the above code as a “Connect-MyTelnet.ps1”, then to load in PowerShell use the “. ./Connect-MyTelnet.ps1” from the folder you saved the file. Finally, to execute run the following one-liner:

Connect-MyTelnet `
	-IPAddress '127.0.0.1' `
	-Port 23 -UserName Max `
	-Password '$mypasword1!' `
	-CmdlistPath c:\temp\cmdlist.txt;

This will create/execute the vbscript that will open a Microsoft Telnet session and submit a series of Telnet commands. Now you got something to work and play.

Bonus!!

If you want to use “PuttyTel.exe” just substitute the PowerShell VBscript code to the following:

## - For PuttyTel:
$MyVBScript = @"
set oShell = CreateObject("WScript.Shell")`r`n
oShell.run("c:\temp\PuttyTel")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$IPAddress")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$UserName")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$Password")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
"@;

This entry was posted in PowerShell. Bookmark the permalink.