Telnet server response only echoes in C# application

729 Views Asked by At

I need to develop an application that is able to put out some commands after connecting to an ip with Telnet, and then just logs the ongoing responds;

So I have tried package like PrimS.Telnet and Minimalistic.Telnet; The thing is it works with other telnet servers, but not with this one; All I get are echo's in uppercases:

enter image description here

While when I use Putty (which I can't automate) it does give the right respond:

I have to press one enter first before getting that weird glitch character away

#

Is this something normal? Am I missing something here why I can't use my C# application with this server?

edit 1: I already found out that my C# does not support some telnet commands that would ask not to echo text back (see Telnet Commands). So my question is how to I parse those telnet commands so I can send them?

1

There are 1 best solutions below

2
Rui Caramalho On

Ok small example for you. Method AskReceive sends a command and waits 200 mileseconds for the answer. It uses Stream to send and receive. If you send clearTextWriter.WriteLine(commandline) you are sending a string command to your device.

using System;
using System.IO;
using System.Net.Sockets;


namespace CommonCore.Classes.Helper
{
    class TelnetDevice01
    {
        static int connectionTimeout = 1300;

        string AskReceive(string commandline, ref string _log)
        {
            _log += "> " + commandline + Environment.NewLine;
            clearTextWriter.WriteLine(commandline);

            string _str;

            System.Threading.Thread.Sleep(200);

            _str = clearTextReader.ReadLine();
            _log += "< " + _str + Environment.NewLine;

            return _str;
        }

        void ExitError(string str, ref string _log, ref string _error)
        {
            _error = str;
            _log += "!! Error : " + str + Environment.NewLine + Environment.NewLine;
            clearTextWriter.WriteLine("QUIT");

        }

        StreamReader clearTextReader = null;
        StreamWriter clearTextWriter = null;

        public void ConnectTelnet(string login, string password, string server, int port,
            out string log, out string resume, out string error
            )
        {

            string _response = "";

            resume = "";
            error = "";
            log = "";
            TcpClient client = new TcpClient();

            //Make the connection with timeout
            if (!client.ConnectAsync(server, port).Wait(connectionTimeout))
            {
                //log = ex.ExceptionToString();
                error = $"Could not connect '{server}' at port '{port}'";
                log += Environment.NewLine + error + Environment.NewLine;
                resume = Environment.NewLine + $"[FAIL] Port={port}. Could not connect '{server}' at port '{port}'" + Environment.NewLine;
                return;
            }

            using (client)
            {

                using (NetworkStream stream = client.GetStream())
                using (clearTextReader = new StreamReader(stream))
                using (clearTextWriter = new StreamWriter(stream) { AutoFlush = true })
                {
                    log += Environment.NewLine + Environment.NewLine + "## Connected" + Environment.NewLine;

                    //Read the start response line like "User:" ?'
                    string connectResponse = clearTextReader.ReadLine();
                    log += "< " + connectResponse + Environment.NewLine;
                    if (!connectResponse.StartsWith("login"))
                    {
                        ExitError(_response, ref log, ref error);
                        resume = Environment.NewLine + $"Expecting 'login'";
                        return;
                    }

                    //Send login
                    if (!(_response = AskReceive(login, ref log)).StartsWith("password"))
                    {
                        ExitError(_response, ref log, ref error);
                        resume = Environment.NewLine + $"Asnswer should have been 'password'";
                        return;
                    }

                    // Is asking for password, let's send the pass now
                    if (!(_response = AskReceive(password, ref log)).StartsWith("Login OK"))
                    {
                        ExitError(_response, ref log, ref error);
                        resume = Environment.NewLine + $"Answer should have been 'Login OK'";
                        return;
                    }

                    //Send CMD SMDR
                    _response = AskReceive($"SMDR", ref log);

                    //Check if the answer is what you want
                    // like _response.Contains("blabla")

                }

            }
        }

    }
}