logo logo

How to Stabilize Your Automation using Telnet, Java and PowerShell

In this article, I will demonstrate the use of a custom Telnet function that I have created as a precondition to running our E2E testing. The function uses a Java API that interacts with PowerShell console named jPowerShell.

The Problem

Some systems (for example a mail server) are blocked to ping, direct UI navigation via selenium or an API call. These systems only accept Telnet connection on certain ports. We need to test these systems as part of our E2E tests. Instead of running many E2E tests that will fail because the server/service is down, we want to verify that the Telnet connection succeeded and only then we should continue to perform our tests.

Enabling Telnet On Windows

In order to enable Telnet, go to windows features and turn on Telnet client feature:

No alt text provided for this image

Telnet a Service

Example telnet command on google SMTP server in port 25:

telnet smtp.gmail.com 25

This is the result of a successful connection:

No alt text provided for this image

In our automation testing, we need a status code or a Boolean result to determine if our connection worked. We can’t get them using this command. Let’s explore what Powershell has to offer us regarding this subject.

Telnet on Powershell

After reading this article I realized I can get a Telnet response object when running this command:

New-Object System.Net.Sockets.TcpClient("smtp.gmail.com", 25)

The response was:

Client              : System.Net.Sockets.Socket
Available           : 0
Connected           : True
ExclusiveAddressUse : False
ReceiveBufferSize   : 65536
SendBufferSize      : 65536
ReceiveTimeout      : 0
SendTimeout         : 0
LingerState         : System.Net.Sockets.LingerOption
NoDelay             : False

In order to extract only the “Connected” value from this object I used Powershell pipe operator and selected only this property:

New-Object System.Net.Sockets.TcpClient("smtp.gmail.com", 25) |  Select -ExpandProperty Connected

The response is:


Telnet on Java using PowerShell

With this knowledge in hand, the next step is to create a function that accepts two parameters: the IP of the server and his port. The function should perform a Telnet and return a Boolean response for the Telnet success status. The function looks like this:

  public boolean canTelnet(String host, int port) {
 PowerShellResponse response = PowerShell.executeSingleCommand(String.format(
  "New-Object System.Net.Sockets.TcpClient('%s', %d) | Select -ExpandProperty Connected", host,
 return response.getCommandOutput().equalsIgnoreCase("True");

Now, with Java and PowerShell combined our Telnet test looks like this:

public void testConnection() throws Exception {
 Assert.assertTrue(canTelnet("smtp.gmail.com", 25));

In case this verification fails, the following tests in our test suite will not run.

In conclusion

In this article, we saw the problem that we were facing when we needed to perform some kind of a “health check” on servers that are only accessible via Telnet. Then, through PowerShell and Java, we came with a programmatic solution that is compatible with our test automation framework.

Happy testing!

About the author

Nir Tal

Automation engineer, in a journey of exploring automation, and CI/CD tools and process.
Passionate about technology and quality.

Leave a Reply