Click here to Skip to main content
15,304,161 members
Articles / Mobile Apps / Windows Mobile
Posted 6 Jun 2007


144 bookmarked

Quick tool : A minimalistic Telnet library

Rate me:
Please Sign up or sign in to vote.
4.87/5 (78 votes)
6 Jun 2007CPOL3 min read
Send commands to your servers from your programs using the Telnet protocol
Image 1


This article provides a minimalistic Telnet interface. Possible applications of this class include :

  • A Telnet console
  • A Windows GUI around some UNIX commands that need to be executed on a server
  • A program that executes scripts (scripted telnet)
This example offers both a minimal console, and a possibility to use a script, using file piping.


A little while ago one of my clients had the problem that a person who was managing a system using UNIX scripts (HP-UX) was leaving the company. This person was not to be replaced by a new employee, so his functions would have to be done by several other persons.

When estimating the cost of teaching the other people how to use UNIX, we made some calculations, and found a much cheaper solution: we built a simple Windows program that contained a GUI for every script used.

The user simply has to press some buttons, select some values and press the "Start" button. When the user presses the start button the UNIX commands are built using the parameters from the GUI, and sent to the server using Telnet.

Since I remember spending quite a few hours figuring out the minimal implementation of the Telnet protocol, I decided to do a simple rewrite for the users of codeproject.

Using the code

The class is actually quite simple to use. Please take a look at the example code :

//create a new telnet connection to hostname "gobelijn" on port "23"
TelnetConnection tc = new TelnetConnection("gobelijn", 23);

//login with user "root",password "rootpassword", using a timeout of 100ms, 
//and show server output
string s = tc.Login("root", "rootpassword",100);

// server output should end with "$" or ">", otherwise the connection failed
string prompt = s.TrimEnd();
prompt = s.Substring(prompt.Length -1,1);
if (prompt != "$" && prompt != ">" )
    throw new Exception("Connection failed");

prompt = "";

// while connected
while (tc.IsConnected && prompt.Trim() != "exit" )
    // display server output

    // send client input to server
    prompt = Console.ReadLine();

    // display server output


I suppose this is self-explanatory, isn't it ? Create the Telnet connection, login, send the login output to the screen, and while connected send serveroutput to the screen, read a command from the commandline, and again, send the serveroutput to the screen.
If the command is "exit" then the loop needs to finish.

Instead of reading the input from the console, the input can also be piped from a script: if the script was named telnetstuff.txt then execute the script as follows :

MinimalisticTelnet < telnetstuff.txt > output.txt

The result will be inside the file output.txt. Currently the servername, port, username and password is hardcoded, but it should be a piece of cake to change this into command line parameters.

How does it work

The TelnetConnection parses every byte received from the TcpClient, and if a byte series is a Telnet option request (DO, DONT, WILL, WONT), the client simply responds that the option is not available by sending (DONT, WONT) in return.

The only exception to these responses is the command SGA: suppress go ahead, since this command allows async traffic.

Points of Interest

The TelnetConnection.Read function assumes that if there is no data available for more then TimeOutMs milliseconds, the output is complete.

The login works by parsing the server output after the initial connection. It will look for a colon ":" in the screen output, and will send username and password after the colon.

To check if the connection succeeded, I look for whether the serveroutput ends with a "$" or a ">". If your Telnet server has another prompt, please replace this.

Votes & Comments

The votes & comments on my previous article gave me the motivation to publish a new article, so please keep your votes & comments coming !!


  • 2007-06-06: First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tom Janssens
Founder Virtual Sales Lab
Belgium Belgium

Comments and Discussions

QuestionThanks! Just save me some work. Needed a small tweak for .net 3.5 Pin
Marcos Guzewski30-Oct-20 4:51
MemberMarcos Guzewski30-Oct-20 4:51 
PraiseGreat working example! Pin
TJM.CODE10-Jun-20 5:57
MemberTJM.CODE10-Jun-20 5:57 
QuestionGreat App, 5 stars! Though, is it possible to use without the console window appearing? Pin
Ducklingg2-Oct-19 19:47
MemberDucklingg2-Oct-19 19:47 
QuestionThank you. i do get an exception some time, do you know why? Pin
Member 143695758-Jun-19 18:37
MemberMember 143695758-Jun-19 18:37 
GeneralMy vote of 5 Pin
bradut21-May-19 20:42
professionalbradut21-May-19 20:42 
PraiseSuper stuff !!! Pin
Joezer BH10-Feb-19 2:05
professionalJoezer BH10-Feb-19 2:05 
QuestionGreat article! Pin
Member 136033761-Sep-18 19:31
MemberMember 136033761-Sep-18 19:31 
PraiseVery helpful Pin
AlessandroF7317-Oct-17 5:58
MemberAlessandroF7317-Oct-17 5:58 
QuestionHow to check if a connection is NOT possible? Pin
Member 1345453910-Oct-17 2:27
MemberMember 1345453910-Oct-17 2:27 
QuestionKeepalive? Pin
Member 1230052724-Apr-17 10:09
MemberMember 1230052724-Apr-17 10:09 
QuestionHow to remove unwanted special characters in server ouputs Pin
nikhil201517-Apr-17 21:05
Membernikhil201517-Apr-17 21:05 
PraiseOutstanding Pin
Member 355001431-Oct-16 21:42
MemberMember 355001431-Oct-16 21:42 
PraiseNice! Pin
Member 1158578826-Jun-16 21:02
MemberMember 1158578826-Jun-16 21:02 
QuestionI can't get server response .Plz Help me Pin
MohammadReza Naseri24-Apr-16 9:54
MemberMohammadReza Naseri24-Apr-16 9:54 
QuestionHow to perform ESC key action on telnet console using c# ? Pin
nikhil201529-Mar-16 3:58
Membernikhil201529-Mar-16 3:58 
QuestionNot able to enter automatically when blinking cursor is before a string on telnet console Pin
nikhil201528-Mar-16 21:10
Membernikhil201528-Mar-16 21:10 
QuestionReading the same lines many times Pin
dimkasta2-Feb-16 0:38
Memberdimkasta2-Feb-16 0:38 
QuestionWindows 8 vs Window 7 Pin
User05108215-Jan-16 3:29
MemberUser05108215-Jan-16 3:29 
QuestionAdditional help Pin
Collin Meaney15-Jun-15 11:24
MemberCollin Meaney15-Jun-15 11:24 
QuestionThis throws an exception when trying to connect with the tcp socket Pin
Member 1085520730-May-14 8:51
MemberMember 1085520730-May-14 8:51 
QuestionSending commands to Cisco switch? Pin
Mark Hammill29-Apr-14 4:41
MemberMark Hammill29-Apr-14 4:41 
QuestionTelnet over Telnet session Pin
GabrielBarros26-Mar-14 9:15
MemberGabrielBarros26-Mar-14 9:15 
GeneralMy vote of 5 Pin
rcole1173-Jan-14 5:52
Memberrcole1173-Jan-14 5:52 
QuestionA minimalistic Telnet library is not working for web application Pin
amitcs199028-Nov-13 11:05
professionalamitcs199028-Nov-13 11:05 
QuestionMissing when Read output Multiline " ---more--------" Pin
Kieu Gol24-Nov-13 7:19
MemberKieu Gol24-Nov-13 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.