54

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.

salt.racer
  • 21,903
  • 14
  • 44
  • 51
  • The accepted answer is the final solution. I don't know if it's still available, but at the time the answer was accepted, the source code for Minimalistic Telnet was available. – salt.racer Sep 05 '12 at 04:59

7 Answers7

52

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.

Malice
  • 3,927
  • 1
  • 36
  • 52
Richard
  • 596
  • 6
  • 4
  • 1
    That's exactly the one that worked for me for the very reasons you outlined. I'm glad I'm not alone in liking it. :) – salt.racer Oct 09 '09 at 17:59
  • Minimalistic telnet worked perfectly for me and our LAN team. I ended up modifying it to read commands from a text file so our LAN team could easily deploy it. – Ben McCormack May 27 '10 at 12:42
  • Started using it too. Great find. – JCCyC Jun 20 '11 at 21:00
  • would really recommend this one to everyone, its very simple in contrast to some other projects out there. Please do note that it is built in a synchronous way, so no callback methods etc. I ended up extending this (single class) solution by letting it implement IDisposable etc + adding some common methods. Might post code if someone needs it. – stvn Aug 22 '12 at 11:35
  • 3
    This was by far the best example to work from I found too. My derived/updated work is on GitHub and published on NuGet if anyone's interested. https://github.com/9swampy/Telnet – 9swampy Oct 27 '15 at 18:51
  • 2
    Thanks @9swampy for providing an updated version on GitHub. This comment should be included as an update to the answer, as it's hard to find and your version is much improved over the original version. – MOnsDaR Jul 12 '18 at 09:10
11

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");
}
}
FrenkyB
  • 6,625
  • 14
  • 67
  • 114
Prakash
  • 119
  • 1
  • 2
  • 2
    Ok. rough and ready and works mostly, but i'd suggest before the .Read() to check if there is ns.DataAvailable because there could be no response, and you would be blocking forever; Also stick the .Read() within a loop to keep reading, as you may not have read all data in one go. – joedotnot Dec 25 '13 at 13:27
  • 3
    Where is `tcpClient` field? – SuB Jun 07 '17 at 18:06
  • 3
    what is ns Variable refer to ? – Malek Tubaisaht May 30 '19 at 13:11
3

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

HOWA
  • 31
  • 1
1

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.

salt.racer
  • 21,903
  • 14
  • 44
  • 51
1

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

Horst Walter
  • 13,663
  • 32
  • 126
  • 228
1

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

Hope this helps.

Regards, Andy.

Tangiest
  • 43,737
  • 24
  • 82
  • 113
0

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.

  • 3
    The issue is that I am trying to telnet into a remote device that only has telnet enabled. I am not trying to setup a machine. SSH is not an option. Telnet is the only option. I can run the command by hand or I can use a little program to do it for me. Thanks for the links. – salt.racer Dec 23 '08 at 22:14
  • 1
    Surely it might have occurred to you that this guy has nothing to say about the server using Telnet or SSH? Just a clue. – Pierre Gardin Aug 10 '12 at 11:24
  • @Matt you would be surprised how many companies still use older technology like Telnet in their Day2Day business :/ – MOnsDaR Jul 12 '18 at 09:14