Running Telnet for Multiple Servers and Ports

This article isn't about telnet utility, it is about how to write a script that runs telnet command for multiple servers and ports, and capture the result into a file. At the end of this blog, you will be able to run telnet command and capture the output in a file within few minutes. It makes you fast, efficient and avoids making mistakes if you are to run telnet for multiple IPs/Ports.

If you are new to telnet, telnet is short for telecommunication network that was fist introduced in telecommunication industry. Telnet is a type of network protocol which allows a user in one computer to logon to another computer which belongs to the same network. The telnet command is used along with the host name and then the user credentials are entered. Upon successful login, the remote user can access the applications and data in a way similar to the regular user of the system. Do you used telnet to login to PC? if yes, please comment below.

What is the purpose? I use this utility mostly during initial setup of server and jobs to test the connectivity of another host on firewall or within network or in other words, this is a test to check if the firewall is opened correctly or not. We use SSH, RLOGIN etc to login to remote servers & PCs not telnet. A utility called telnet must be installed before you can run this telnet command and most OS comes pre-installed out of the box.

telnet <ip address/hostname> <port number>
Linux: Telnet to Multiple Servers and Ports:
set -A arrayName "hostname1:port1" "hostname2:port2"
## create output.txt file, if it does not exist already
if [[ ! -e output.txt ]]; then
    touch output.txt
`(cat /dev/null > output.txt)` # empty output file
for i in ${arrayName[@]}
        host=`(echo $i | cut -f 1 -d ":")` # get host info from array
        port=`(echo $i | cut -f 2 -d ":")` # get port info from array
        printf "\n"| tee -a $out
        echo "Checking Connectivity on $host and Port $port" | tee -a $out
        if telnet -c $host $port </dev/null 2>&1 | grep -q Escape; then
                printf "\n" | tee -a $out
                echo " $host $port: Connected Successfully" | tee -a $out
                printf "\n" | tee -a $out
                echo "$host $port : Connection Failed" | tee -a $out
        printf "\n" | tee -a $out
        echo "==============================" | tee -a $out
Save the above script in a file as telnet.ksh and replace sample server and port with your requirement and the result will be saved into a output.txt file.

Windows: Telnet to Multiple Servers and Ports:
$server_list = @('hostname1:1521', 'hostname2:1521', 'hostname3:1521')
Foreach ($t in $server_list)
  $source = $t.Split(':')[0]
  $port = $t.Split(':')[1]
  Write-Host "Connecting to $source on port $port" | Out-File 'output.txt' -Append
    $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
  catch [Exception]
    Write-Host $_.Exception.GetType().FullName | Out-File 'output.txt' -Append
    Write-Host $_.Exception.Message | Out-File 'output.txt' -Append
  Write-Host "Connected`n" | Out-File 'output.txt' -Append
Copy the above  script and modify the hostname/ip and port number list in the first line and save into a file named it as telnet.ps1. Next run the script on power-shell using command listed below. This script displays the result on the screen and also spools out to a file output.txt.

PS C:\Users\dbarepublic\Documents\shell>  powershell -file telnet.ps1
Exit Telnet Session:
How to exit out of telnet session? there is a  saying, someone taught to climb up the tree but never taught you to climb down. I do not want you to feel it the same way here with telnet session because exit ctrl + c, ctrl + z commands that we are used to exiting out of session does not work here. There are two step process to exit out of telnet,  ctrl +] and then type quit.

Exit Example:
[prabin@centos-lab script]$ telnet 22
Connected to
Escape character is '^]'.
^]         ## ctrl + ]
telnet> quit
Connection closed.
[prabin@centos-lab script]$
What do you think? Did this save your time to telnet to 499 Servers that was asked to you? Can you now deliver accurate result in few minutes or less? I know you can!

Interested in working with me? I can be reached at pbaniya04[at] for any questions, consulting opportunities or you may drop a line to say HELLO. Thank your again for visiting my blog and looking forward to serving you more.

Have a Database-ious Day!

No comments

Powered by Blogger.