RSS Amplifier

Martin's Blog · Apr 14, 2019

Powershell replacement for telnet connectivity test

0
Sign in to vote or save

Martin Stemplinger · Martin's Blog

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 simple 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).

Read the original on mstempl.netlify.app

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.