Replacing Telnet.exe (now removed from Vista)

Wed, Aug 30, 2006 2-minute read

Probably the most useful network tool on any operating system is Telnet.  Not for connecting to Telnet servers, of course, as the Telnet protocol is about as insecure as they come.  Instead, it’s useful for debugging connection problems with arbitrary ports and arbitrary protocols.

Debugging an HTTP problem?  You can Telnet into port 80 to help you resolve it.
Debugging a mail retrieval issue?  You can Telnet into port 110 to help you resolve it.
Debugging a mail sending issue?  You can Telnet into port 25 to help you resolve it.

Unfortunately, this workhorse was removed from Vista’s default installation.  Here’s a simple PowerShell replacement script.  It’s great for debugging, but useless (of course) for terminal emulation:

(Edit: A much improved version of this script is in the PowerShell Cookbook as Send-TcpRequest)

## Connect-Computer.ps1
## Interact with a service on a remote TCP port
    [string] $remoteHost = "localhost",
    [int] $port = 80
## Open the socket, and connect to the computer on the specified port
write-host "Connecting to $remoteHost on port $port"
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }

write-host "Connected.  Press ^D followed by [ENTER] to exit.`n"

$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)

$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding

   ## Allow data to buffer for a bit
   start-sleep -m 500

   ## Read all the data available from the stream, writing it to the
   ## screen when done.
      $read = $stream.Read($buffer, 0, 1024)   
      write-host -n ($encoding.GetString($buffer, 0, $read)) 

   ## Read the user's command, quitting if they hit ^D
   $command = read-host
   if($command -eq ([char] 4)) { break; }
   ## Write their command to the remote host     

## Close the streams