Powershell replacement for telnet connectivity test

April 14, 2019
1 minute read
software security

Recently I was several times involved in networking and firewall troubleshooting sessions for Windows servers. Networking and firewall folks traditionally use telnet to check for connectivity on a given port. Thus also in these sessions inevitably someone would say “Can you try to telnet to the URL on port 443?”.

Now the issue is that Windows servers don’t come with telnet. You could install it but that’s neither advisable in a troubleshooting session nor in line with most company’s security policies. On the other hand Windows servers run Powershell natively. Here is a very simple Powershell script that tries to create a TCPSocket on a given port. That’s mostly good enough to verify if connectivity is available or not.

$rhost = "example.com"
$rport = "443"

$socket = New-Object System.Net.Sockets.TcpClient($rhost, $rport)

if ($socket -ne $null) {
  echo "Connection to $rhost on port $rport successful"
} else {
  echo "Can't connect to $rhost on port $rport"
}

In newer versions of Windows server (2012 and above) you can also simply use

Test-NetConnection -Computername "example.com" -Port 443 | select TCPTestSucceeded

The advantage of the former version is that it works on all supported versions of Windows server (starting from 2008).

Share on:

Markdown Tasks in Vscode

November 8, 2022
3 minute read
dendron software

Thanks to Wayback Machine

November 20, 2021
1 minute read
software

Using SSH Agent on Windows

November 10, 2021
2 minute read
security software
comments powered by Disqus