Question
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.
Solution
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.
OTHER TIPS
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");
}
}
Another one with a different concept: http://www.klausbasan.de/misc/telnet/index.html
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.
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.
I am currently evaluating two .NET (v2.0) C# Telnet libraries that may be of interest:
Hope this helps.
Regards, Andy.
Another one, it is an older project but shares the complete source code: http://telnetcsharp.codeplex.com/