C# Telnet Library
Asked Answered
H

7

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.

Hydrophobia answered 23/12, 2008 at 21:59 Comment(1)
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.Hydrophobia
C
53

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.

Clustered answered 8/10, 2009 at 14:56 Comment(6)
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. :)Hydrophobia
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.Traylor
Started using it too. Great find.Pathoneurosis
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.Durante
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. github.com/9swampy/TelnetDetta
Thanks @Detta 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.Melantha
P
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");
}
}
Persona answered 11/1, 2012 at 22:45 Comment(3)
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.Jarlath
Where is tcpClient field?Depicture
what is ns Variable refer to ?Convergent
M
3

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

Metametabel answered 18/6, 2010 at 20:12 Comment(0)
H
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.

Hydrophobia answered 24/12, 2008 at 0:4 Comment(0)
K
1

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

Hope this helps.

Regards, Andy.

Knackwurst answered 23/3, 2009 at 14:0 Comment(0)
A
1

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

Alcoholicity answered 11/1, 2011 at 18:9 Comment(0)
F
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.

Fates answered 23/12, 2008 at 22:8 Comment(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.Hydrophobia
Surely it might have occurred to you that this guy has nothing to say about the server using Telnet or SSH? Just a clue.Gleeman
@Fates you would be surprised how many companies still use older technology like Telnet in their Day2Day business :/Melantha

© 2022 - 2024 — McMap. All rights reserved.