C# Telnet 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/390188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:39:25  来源:igfitidea点击:

C# Telnet Library

c#.nettelnet

提问by salt.racer

Is there a good, free telnet library available for C# (not ASP .NET)? I have found a few on google, but they all have one issue or another (don't support login/password, don't support a scripted mode).

是否有适用于 C#(不是 ASP .NET)的好的免费 telnet 库?我在谷歌上找到了一些,但它们都有一个或另一个问题(不支持登录名/密码,不支持脚本模式)。

I am assuming that MS still has not included a telnet library as part of .NET v3.5 as I couldn't find it if it was. I would loooooove to be wrong though.

我假设 MS 仍然没有将 telnet 库作为 .NET v3.5 的一部分包含在内,因为我找不到它。不过,我很可能是错的。

采纳答案by Richard

Best C# Telnet Lib I've found is called Minimalistic Telnet. Very easy to understand, use and modify. It works great for the Cisco routers I need to configure.

我发现的最好的 C# Telnet Lib 称为 Minimalistic Telnet。非常容易理解、使用和修改。它非常适合我需要配置的 Cisco 路由器。

http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx

http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx

回答by salt.racer

I doubt very much a telnet library will ever be part of the .Net BCL, although you do have almost full socket support so it wouldnt be too hard to emulate a telnet client, Telnet in its general implementation is a legacy and dying technology that where exists generally sits behind a nice new modern facade. In terms of Unix/Linux variants you'll find that out the box its SSH and enabling telnet is generally considered poor practice.

我非常怀疑 telnet 库是否会成为 .Net BCL 的一部分,尽管您确实有几乎完整的套接字支持,因此模拟 telnet 客户端不会太难,Telnet 在其一般实现中是一种遗留和垂死的技术,其中存在通常位于一个漂亮的新现代外观后面。就 Unix/Linux 变体而言,您会发现开箱即用的 SSH 和启用 telnet 通常被认为是不好的做法。

You could check out: http://granados.sourceforge.net/- SSH Library for .Net http://www.tamirgal.com/home/dev.aspx?Item=SharpSsh

您可以查看:http: //granados.sourceforge.net/- .Net 的 SSH 库 http://www.tamirgal.com/home/dev.aspx?Item=SharpSsh

You'll still need to put in place your own wrapper to handle events for feeding in input in a scripted manner.

您仍然需要放置自己的包装器来处理以脚本方式输入输入的事件。

回答by salt.racer

I ended up finding MinimalistTelnetand adapted it to my uses. I ended up needing to be able to heavily modify the code due to the unique** device that I am attempting to attach to.

我最终找到了MinimalistTelnet并将其调整为我的用途。由于我尝试连接的独特**设备,我最终需要能够大量修改代码。

** Unique in this instance can be validly interpreted as brain-dead.

** 在这种情况下唯一可以有效地解释为脑死亡。

回答by MagicAndi

I am currently evaluating two .NET (v2.0) C# Telnet libraries that may be of interest:

我目前正在评估可能感兴趣的两个 .NET (v2.0) C# Telnet 库:

Hope this helps.

希望这可以帮助。

Regards, Andy.

问候,安迪。

回答by HOWA

Another one with a different concept: http://www.klausbasan.de/misc/telnet/index.html

另一个具有不同概念的:http: //www.klausbasan.de/misc/telnet/index.html

回答by Horst Walter

Another one, it is an older project but shares the complete source code: http://telnetcsharp.codeplex.com/

另一个,它是一个较旧的项目,但共享完整的源代码:http: //telnetcsharp.codeplex.com/

回答by Prakash

Here is my code that is finally working

这是我的代码,终于可以工作了

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;

class TelnetTest
{

    static void Main(string[] args)
    {
        TelnetTest tt = new TelnetTest();

        tt.tcpClient = new TcpClient("myserver", 23);
        tt.ns = tt.tcpClient.GetStream();

        tt.connectHost("admin", "admin");
        tt.sendCommand();

        tt.tcpClient.Close();
    }

public void connectHost(string user, string passwd) {
    bool i = true;
    while (i)
    {
        Console.WriteLine("Connecting.....");
        Byte[] output = new Byte[1024];
        String responseoutput = String.Empty;
        Byte[] cmd = System.Text.Encoding.ASCII.GetBytes("\n");
        ns.Write(cmd, 0, cmd.Length);

        Thread.Sleep(1000);
        Int32 bytes = ns.Read(output, 0, output.Length);
        responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
        Console.WriteLine("Responseoutput: " + responseoutput);
        Regex objToMatch = new Regex("login:");
        if (objToMatch.IsMatch(responseoutput)) {
           cmd = System.Text.Encoding.ASCII.GetBytes(user + "\r");
           ns.Write(cmd, 0, cmd.Length);
        }

        Thread.Sleep(1000);
        bytes = ns.Read(output, 0, output.Length);
        responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
        Console.Write(responseoutput);
        objToMatch = new Regex("Password");
        if (objToMatch.IsMatch(responseoutput))
        {
            cmd = System.Text.Encoding.ASCII.GetBytes(passwd + "\r");
            ns.Write(cmd, 0, cmd.Length);
        }

        Thread.Sleep(1000);
        bytes = ns.Read(output, 0, output.Length);
        responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
        Console.Write("Responseoutput: " + responseoutput);

        objToMatch = new Regex("#");
        if (objToMatch.IsMatch(responseoutput))
        {
            i = false;
        }

    }

    Console.WriteLine("Just works");
}
}