0

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?

Michiel Krol
  • 243
  • 3
  • 9
  • Hard to say since you haven't shared your code, but my guess is you haven't sent the correct telnet command sequence. IAC DO ECHO. https://tools.ietf.org/html/rfc857 – itsme86 Jun 15 '20 at 15:02
  • I've made a Class to comunicate to SMTP server. I also send commands and get the answers. It uses `NetworkStream` and `StreamReader(stream)` + `StreamWriter(stream)`.. I think it's what yopu need. I don't need any Telnet application to achieve that.. https://stackoverflow.com/questions/60932425/i-cant-send-an-e-mail/60937764#60937764 – Rui Caramalho Jun 15 '20 at 15:16
  • @itsme86 Sow how do you send those commands? Just as you send a normal text command? – Michiel Krol Jun 15 '20 at 15:36
  • 1
    I'm writing an example.. wait a few minutes please – Rui Caramalho Jun 15 '20 at 16:40

1 Answers1

1

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")

                }

            }
        }

    }
}

Rui Caramalho
  • 455
  • 8
  • 16
  • Thank you for your efforts. I do have a C# package i'm currently using: PrimS.Telnet and Minimalistic.Telnet; And they work great with a lot of telnet servers, yet I have one telnet server that has it's echo on. So my question is how do I get those bytecodes (Telnet Commands) converted so I can send them with the current send methods? – Michiel Krol Jun 15 '20 at 19:35
  • 1
    For what I read (I'm no expert) you must disable the echo from either the client or the server. On a windows telnet client it's the command `u localecho` or `unset localecho` – Rui Caramalho Jun 15 '20 at 19:57