简体   繁体   中英

How to telnet from remote server - c#

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM