recent

Titulo

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.

Syntax:
telnet <ip address/hostname> <port number>
Linux: Telnet to Multiple Servers and Ports:
#!/bin/ksh
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
fi
out='output.txt'
`(cat /dev/null > output.txt)` # empty output file
for i in ${arrayName[@]}
do
        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
        else
                printf "\n" | tee -a $out
                echo "$host $port : Connection Failed" | tee -a $out
        fi
        printf "\n" | tee -a $out
        echo "==============================" | tee -a $out
done
exit
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
  try
  {
    $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 test.rebex.net 22
Trying 195.144.107.198...
Connected to test.rebex.net.
Escape character is '^]'.
SSH-2.0-MyServer_1.0.0
^]         ## 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]gmail.com 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!

8 comments

  1. I ran the powershell file to make the tests on windows. I did work correctly but didn't write the results on the output file. Have I done something wrong?

    ReplyDelete
  2. Same issue. I was able to run the script but the output.txt was empty.

    ReplyDelete
  3. Hi All,

    I was facing the same issue where it was not writing results to output text file. Below are 2 scripts to write output to text file

    1. Foreach ($t in $server_list)
    {
    $source = $t.Split(':')[0]
    $port = $t.Split(':')[1]
    "Connecting to $source on port $port" | Out-File -FilePath .\outuput.txt -Append
    try
    {
    $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
    "Connected`n" | Out-File -FilePath .\outuput.txt -Append
    }
    catch [Exception]
    {
    $_.Exception.GetType().FullName | Out-File -FilePath .\outuput.txt -Append
    $_.Exception.Message | Out-File -FilePath .\outuput.txt -Append
    }
    }

    ReplyDelete
  4. 1st way to print output to text file

    1. $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
    try
    {
    $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.

    ReplyDelete
    Replies
    1. Please ignore my above scripts. Below is correct one.
      1. $server_list = @('hostname1:1521', 'hostname2:1521', 'hostname3:1521')
      Foreach ($t in $server_list)
      {
      $source = $t.Split(':')[0]
      $port = $t.Split(':')[1]
      "Connecting to $source on port $port" | Out-File -FilePath .\outuput.txt -Append
      try
      {
      $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
      "Connected`n" | Out-File -FilePath .\outuput.txt -Append
      }
      catch [Exception]
      {
      $_.Exception.GetType().FullName | Out-File -FilePath .\outuput.txt -Append
      $_.Exception.Message | Out-File -FilePath .\outuput.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.

      Delete
  5. 2nd way of writing script output to text file.

    2.$server_list = @('hostname1:1521', 'hostname2:1521', 'hostname3:1521')
    Foreach ($t in $server_list)
    {
    $source = $t.Split(':')[0]
    $port = $t.Split(':')[1]
    write-output "Connecting to $source on port $port" >> C:\Documents\output.txt
    try
    {
    $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
    write-output "Connected`n">> C:\Documents\output.txt
    }
    catch [Exception]
    {
    write-output $_.Exception.GetType().FullName>> C:\Documents\output.txt
    write-output $_.Exception.Message >> C:\Documents\output.txt
    }
    }

    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.

    ReplyDelete
  6. Write-host is used for displaying message on powershell or command prompt window where as write-output is used to write output to text file.

    ReplyDelete

Powered by Blogger.