1

New to telnet. There is a list of servers and I need to check, if each server is able to successfully telnet a certain server(10.20.30.100) with port(25).

The idea is to have a console application run from a single place which will check for all listed servers.

Followed @freedomn-m's solution, but still stuck.

var centralServer = "10.20.30.100";
var centralPort = 25;

var servers = "11.12.13.201,11.12.13.202,11.12.13.203";
var telnetPort = 23;

foreach (var server in servers.Split(','))
{
    TcpClient tc = null;
    try
    {
        tc = new TcpClient(server, telnetPort);
        tc.Client.Send(System.Text.Encoding.ASCII.GetBytes("telnet " + centralServer + " " + centralPort));    

    }
    catch (SocketException se)
    {
        // telnet failed on 'server'
    }
    finally
    {
        if (tc != null)
        {
            tc.Close();
        }
    }
}

Even if the centralserver or centralport is invalid/incorrect, no exception occurs. The code still proceeds to complete the for loop . I am unable to know if a server was able to successfully telnet. How should I proceed further.

sukesh
  • 2,379
  • 12
  • 56
  • 111
  • Hmmm, what's the problem? you just missed to open the TCP client, if the TCP client connects then your telnet server is reachable... – Gusman Jun 03 '15 at 08:08
  • 1
    So you want to determine whether you can open a telnet connection from each of the servers in that list to a common "other" server? Then surely you need to open the telnet connection from where you are onto each server in turn, and open the onwards telnet connection from there? – ClickRick Jun 03 '15 at 08:09

2 Answers2

2

You won't 'command' remote server to open connection to telnet server and return result with your existing code. If servers themselves have telnet enabled, you can accomplish your task in 3 steps for each server:

  1. Open telnet connection to 'telnet client` server
  2. Send 'telnet client' server command to open telnet connection to 'telnet server' server
  3. Check if it succeeded.

There is a C# implementation of telnet protocol. But you'll be better off using some monitoring solution like Zabbix.

cyberj0g
  • 3,707
  • 1
  • 19
  • 34
  • Is there any other way, other than running this app on each server. That is what exactly Iam trying to do. – sukesh Jun 03 '15 at 08:45
2

Consider how you would do this without code.

As you've described this, you have a number of servers, each of which need to be able to telnet to the central server - and you want to check they can from a single place.

Without code, you would telnet to the remote server, then, on that server, via your telnet client, send the telnet request to the destination server. (if using telnet alone, there are other ways such as RPC).

You can't execute C# code on the remote server unless you deploy your/an app there (which is an option ofc).

Updating your code:

var centralServer = "10.20.30.100";
var centralPort = 25;   // 25 as per OP

var servers = "11.12.13.201,11.12.13.202,11.12.13.203";
var telnetPort = 23;

foreach (var server in servers.Split(','))
{
    TcpClient tc = null;
    try
    {
        tc = new TcpClient(server, telnetPort);
        tc.Client.Send(System.Text.Encoding.ASCII.GetBytes("telnet " + centralServer + " " + centralPort));

        // tc.Client.Receive
    }
    catch (SocketException se)
    {
        // telnet failed on 'server'
    }
    finally
    {
        if (tc != null)
        {
            tc.Close();
        }
    }
}

As this question isn't about how to send/receive via telnet, I've left that for you to complete.

In addition: there are a number of C# telnet libraries that make it much easier to connect/send commands/receive responses.

Edit: Updated port to use telnet port. http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers The central server port may be 23 or 25 depending on original post.

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • `tc = new TcpClient(server, 25);` . You've used the port of centralserver. But this could be different right ? – sukesh Jun 03 '15 at 08:52
  • "You've used the port of centralserver" - no, it uses port 25 which was the one in your example. Port 25 is the SMTP port, so this should be be 23 for telnet. If the idea is that you're checking if your remote servers can connect to SMTP then leave the central port as 25. I'll edit answer. – freedomn-m Jun 03 '15 at 09:00
  • Hi. I have just tried with an invalid centralserver, but there is no exception. It just proceeds with the for loop. Why is it. How to acheive my purpose in this case. – sukesh Jun 03 '15 at 09:56
  • You need to add the call+check response (maybe even login with user+pass) inside the try. It's not a complete solution, just an answer to the question. – freedomn-m Jun 03 '15 at 10:51
  • could you please update your code with such example. – sukesh Jun 04 '15 at 09:27
  • There's plenty of examples, try the 'Related' section on the right, eg: http://stackoverflow.com/questions/390188/c-sharp-telnet-library – freedomn-m Jun 04 '15 at 09:29
  • Sure. All the ones I have gone through are for telnetting a server. In my case, the console app has to check if a remote server can telnet a common central server. – sukesh Jun 04 '15 at 10:19
  • The answer I suggested is that you telnet the server and from there telnet the central server via telnet (the first telnet). That was the answer to the question asked and answers the clarification statement, "the console app has to check if a remote server can telnet a common central server". Beyond this is a separate question which already has answers - if you can't get the existing answers to work for you then please ask a new question. – freedomn-m Jun 04 '15 at 10:28