Pages

Tuesday, April 12, 2016

How to telnet multiple windows server

How do we telnet multiple windows server


In my recent project i got a requirement to set up a shared SQL Server from where i have to connect to multiple servers and perform the task related to day to day DBA activities.

This was a newly built server and therefore i have to struggle alot to find on what ports my database servers are working and whether they are accessible from this shared server or not ?

This was the biggest huddle to cross, so that i can install SQL Client and start working from it.

We can check the port connectivity by using below command.

CMD > Telnet HOSTNAME PORT_NUMBER

Eg: Telnet 191.10.10.1 1433

By doing above operation we can find out whether port 1433 is accessible to connect 191.10.10.1 server from the source location where we are executing it. It's a command prompt task.

Now how to achieve this for more than 500 servers ? We cannot do manually for hours together and get irritated.

I did lot of googling on this and came up with below solution. This is best way to proceed.

By using Powershell scripting.

Step 1: Login to the Shared server from where you want to check connectivity

Step 2: Open a notepad and copy the below script by changing "Servername" and port 1433 as per     your need.



$test = @('servername1:1433','servername2:1433','servername3:1433')

Foreach ($t in $test)
{
  $source = $t.Split(':')[0]
  $port = $t.Split(':')[1]
  
  Write-Host "Connecting to $source on port $port"

  try
  {
    $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
  }
  catch [Exception]
  {
    Write-Host $_.Exception.GetType().FullName
    Write-Host $_.Exception.Message
  }

  Write-Host "Connected`n"
}


Step 3: Now save the above script as "Powershell_script.ps1"

Don't forget to change the server names and port numbers as per your need.

Step 4: Go to Command prompt and run the below script

CMD > powershell -File Pwoershell_script.ps1

Above script will give result like 

Connecting to Servername on port 1433

Connected

If you want to log the complete output, especially when you are checking for hundreds of servers.

Use below command.

CMD > powershell -File Pwoershell_script.ps1 >>Output.log

You will find the output file in the same path where your command prompt is prompting now.

Hope it helps.