How to Telnet via BASH Script

This article will show you how to Telnet via BASH script and return the status as open or close. This has been tested on CentOS/RedHat and hopefully should work on all other Linux distributions as well.

Create a file telnet_script.sh. This script is to Telnet SSH port i.e. 22

# vi telnet_script.sh
#!/bin/bash
echo "172.16.66.200 22" | \
while read host port; do
  r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
  if [ "$r" = "0" ]; then
    echo $host $port is open
  else
    echo $host $port is closed
  fi
done

 Output:

telnet script 1

This is when Telnet for SSH Port responds OK.

Output:

telnet script 2

This is when Telnet for SSH Port responds FAIL.

If you encounter any errors during the process, let us know via Contact Us and will try our best to help.

Most Read Articles

Add/Detect a new disk in ...

Written By Farooq Mohammed Ahmed on Sunday, 11 January 2015 19:22
Add/Detect a new disk in ...

Replace SSL Certificates ...

Written By Farooq Mohammed Ahmed on Friday, 16 December 2016 08:05
Replace SSL Certificates ...

Using awk in Alias Comman...

Written By Farooq Mohammed Ahmed on Friday, 19 February 2016 14:22
Using awk in Alias Comman...

Sed - Insert Text before ...

Written By Farooq Mohammed Ahmed on Monday, 20 November 2017 00:58
Sed - Insert Text before ...

Script to Monitor Nagios ...

Written By Farooq Mohammed Ahmed on Monday, 18 July 2016 22:48
Script to Monitor Nagios ...