C # Telnet Library

Is there a good, free telnet library available for C # (not ASP.NET)? I found several on google, but everyone has one problem (do not support login / password, do not support script).

I assume that MS still did not include the telnet library as part of .NET v3.5, as I could not find it if it were. I would loooooove to be wrong though.

+47
c # telnet
Dec 23 '08 at 21:59
source share
8 answers

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

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

+43
08 Oct '09 at 14:56
source share

Here is my code that finally works

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"); } } 
+6
Jan 11 '12 at 22:45
source share

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

+3
Jun 18 '10 at 20:12
source share

Here is the telnet library and program, which uses it as an example. The entire source (including the source of the library) is at the bottom of the article.

The example logs in to the Cisco router and loads the configuration.

http://www.xpresslearn.com/networking/code/csharp-telnet-client

+2
Jun 11 '10 at 17:11
source share

I doubt very much that the telnet library will ever be part of .NET BCL, although you have almost full socket support, so it would be too difficult to emulate the telnet client, Telnet in its common implementation is a legacy and dying technology in which, like usually located behind a beautiful new modern facade. Regarding the Unix / Linux variants, you will find that from its SSH and the inclusion of telnet it is usually considered bad practice.

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

You will still need to create your own wrapper for handling events to enter input into scripts.

+1
Dec 23 '08 at 22:08
source share

I ended the search for MinimalistTelnet and adapted it for my purposes. In the end, I had to change the code a lot because of the unique ** device I'm trying to connect to.

** Unique in this case can be correctly interpreted as a "dead brain".

+1
Dec 24 '08 at 0:04
source share

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

Hope this helps.

Regards, Andy.

+1
Mar 23 '09 at 2:00
source share

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

+1
Jan 11 '11 at 18:09
source share



All Articles