.net – C# Telnet Library

.net – C# Telnet Library

Best C# Telnet Lib Ive found is called Minimalistic Telnet. Very easy to understand, use and modify. It works great for the Cisco routers I need to configure.

n

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

Here is my code that is finally working

n

using System;nusing System.IO;nusing System.Net;nusing System.Net.Sockets;nusing System.Text.RegularExpressions;nusing System.Threading;nnclass TelnetTestn{nn    static void Main(string[] args)n    {n        TelnetTest tt = new TelnetTest();nn        tt.tcpClient = new TcpClient(myserver, 23);n        tt.ns = tt.tcpClient.GetStream();nn        tt.connectHost(admin, admin);n        tt.sendCommand();nn        tt.tcpClient.Close();n    }nnpublic void connectHost(string user, string passwd) {n    bool i = true;n    while (i)n    {n        Console.WriteLine(Connecting.....);n        Byte[] output = new Byte[1024];n        String responseoutput = String.Empty;n        Byte[] cmd = System.Text.Encoding.ASCII.GetBytes(\n);n        ns.Write(cmd, 0, cmd.Length);nn        Thread.Sleep(1000);n        Int32 bytes = ns.Read(output, 0, output.Length);n        responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);n        Console.WriteLine(Responseoutput:  + responseoutput);n        Regex objToMatch = new Regex(login:);n        if (objToMatch.IsMatch(responseoutput)) {n           cmd = System.Text.Encoding.ASCII.GetBytes(user + \r);n           ns.Write(cmd, 0, cmd.Length);n        }nn        Thread.Sleep(1000);n        bytes = ns.Read(output, 0, output.Length);n        responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);n        Console.Write(responseoutput);n        objToMatch = new Regex(Password);n        if (objToMatch.IsMatch(responseoutput))n        {n            cmd = System.Text.Encoding.ASCII.GetBytes(passwd + \r);n            ns.Write(cmd, 0, cmd.Length);n        }nn        Thread.Sleep(1000);n        bytes = ns.Read(output, 0, output.Length);n        responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);n        Console.Write(Responseoutput:  + responseoutput);nn        objToMatch = new Regex(#);n        if (objToMatch.IsMatch(responseoutput))n        {n            i = false;n        }nn    }nn    Console.WriteLine(Just works);n}n}n

.net – C# Telnet Library

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

Leave a Reply

Your email address will not be published.