How to Telnet via BASH Script

by | Feb 25, 2015 | RHEL / CentOS

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.

Related Articles….