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:

Logseq Sync Options

January 16, 2024
5 minute read
dendron logseq software productivity

How I add web articles to my personal knowledge base

October 29, 2023
3 minute read
dendron logseq software productivity

If your security architecture is largely based on protective measures, think again

September 3, 2023
3 minute read
security