Telnet服务器的响应在C#应用中只有回声。

问题描述 投票:0回答:1

我需要开发一个应用程序,能够在用Telnet连接到一个ip后发出一些命令,然后只记录正在进行的响应。

所以我试过PrimS.Telnet和Minimalistic.Telnet这样的包;问题是它在其他telnet服务器上可以工作,但在这个服务器上不行;我得到的都是大写的回音。

enter image description here

而当我使用Putty时(我不能自动化),它确实给出了正确的回应。

我必须先按一个回车键才能得到那个奇怪的故障字符。

#

这是正常的事情吗?我是不是遗漏了什么,为什么我的C#应用不能在这个服务器上使用?

编辑 1.我已经发现我的C#不支持一些telnet命令。 我已经发现我的C#不支持一些telnet命令,这些命令会要求不回传文本(见 Telnet命令). 所以我的问题是如何解析这些telnet命令,以便我可以发送它们?

c# echo telnet parrot
1个回答
1
投票

好的小例子给你。方法 AskReceive 发送一个命令并等待200英里秒的回答。它使用流来发送和接收。如果你发送 clearTextWriter.WriteLine(commandline) 你正在向你的设备发送一个字符串命令。

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

                }

            }
        }

    }
}

© www.soinside.com 2019 - 2024. All rights reserved.