可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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).
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.
回答1:
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.
http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx
回答2:
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"); } }
回答3:
回答4:
Here is a telnet library and program that uses it as an example. All the source (including the library source) is at the bottom of the article.
The example performs a login to a Cisco router and downloads the configuration
http://www.xpresslearn.com/networking/code/csharp-telnet-client
回答5:
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.
You could check out: http://granados.sourceforge.net/ - SSH Library for .Net 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.
回答6:
I ended up finding MinimalistTelnet and 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.
** Unique in this instance can be validly interpreted as brain-dead.
回答7:
I am currently evaluating two .NET (v2.0) C# Telnet libraries that may be of interest:
Hope this helps.
Regards, Andy.
回答8:
Another one, it is an older project but shares the complete source code: http://telnetcsharp.codeplex.com/