Click here to Skip to main content
12,397,805 members (54,526 online)
Click here to Skip to main content
Add your own
alternative version


131 bookmarked

Quick tool : A minimalistic Telnet library

, 6 Jun 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Send commands to your servers from your programs using the Telnet protocol


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 Core bvba
Belgium Belgium
Tom Janssens, owner of Core, a software and consultancy company.
Father of two sons named Quinten & Matisse, and married to a beautiful woman named Liesbeth.


You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
medophin11-May-11 15:30
membermedophin11-May-11 15:30 
QuestionProblem with commands... Pin
raananv31-Dec-10 2:12
memberraananv31-Dec-10 2:12 
AnswerRe: Problem with commands... Pin
Tom Janssens31-Dec-10 4:50
memberTom Janssens31-Dec-10 4:50 
GeneralRe: Problem with commands... Pin
raananv31-Dec-10 5:23
memberraananv31-Dec-10 5:23 
GeneralMy vote of 5 Pin
Kapil Singh Verma30-Dec-10 19:55
memberKapil Singh Verma30-Dec-10 19:55 
GeneralMy vote of 5 Pin
clueponic30-Nov-10 0:25
memberclueponic30-Nov-10 0:25 
GeneralHelpful article Pin
Member 469491012-Oct-10 16:11
memberMember 469491012-Oct-10 16:11 
Generalminilistic telnet Pin
john Saxby21-Sep-10 9:59
memberjohn Saxby21-Sep-10 9:59 
GeneralRe: minilistic telnet Pin
Tom Janssens23-Sep-10 7:18
memberTom Janssens23-Sep-10 7:18 
Generalwow perfect :-) :-) :-) Pin
Member 18269033-Sep-10 20:12
memberMember 18269033-Sep-10 20:12 
GeneralRe: wow perfect :-) :-) :-) Pin
Tom Janssens3-Sep-10 22:01
memberTom Janssens3-Sep-10 22:01 
Questionconsole output message Pin
apfelwein23-Aug-10 23:45
memberapfelwein23-Aug-10 23:45 
GeneralMy vote of 5 Pin
freemap9123-Aug-10 21:16
memberfreemap9123-Aug-10 21:16 
GeneralRe: My vote of 5 Pin
Tom Janssens23-Aug-10 23:27
memberTom Janssens23-Aug-10 23:27 
thanks !!

Generaltail -f Pin
sundalongqueso14-Aug-10 23:30
membersundalongqueso14-Aug-10 23:30 
GeneralRe: tail -f Pin
Tom Janssens15-Aug-10 14:58
memberTom Janssens15-Aug-10 14:58 
GeneralRe: tail -f Pin
sundalongqueso18-Aug-10 12:59
membersundalongqueso18-Aug-10 12:59 
GeneralRe: tail -f Pin
Tom Janssens19-Aug-10 0:59
memberTom Janssens19-Aug-10 0:59 
GeneralConnection failed Pin
jenny1826-Apr-10 16:00
memberjenny1826-Apr-10 16:00 
AnswerRe: Connection failed Pin
Tom Janssens26-Apr-10 17:17
memberTom Janssens26-Apr-10 17:17 
GeneralRe: Connection failed Pin
jenny1827-Apr-10 21:18
memberjenny1827-Apr-10 21:18 
GeneralNo response to commands Pin
KenGamble3-Apr-10 18:06
memberKenGamble3-Apr-10 18:06 
GeneralRe: No response to commands Pin
KenGamble4-Apr-10 5:43
memberKenGamble4-Apr-10 5:43 
GeneralRe: No response to commands Pin
Tom Janssens23-Aug-10 23:29
memberTom Janssens23-Aug-10 23:29 
Generalnot all data recive Pin
234543ts24-Mar-10 2:01
member234543ts24-Mar-10 2:01 
QuestionCommand line parameters Pin
konvod17-Nov-09 13:34
memberkonvod17-Nov-09 13:34 
QuestionAsync example? Pin
YttriumOxide5-Nov-09 16:10
memberYttriumOxide5-Nov-09 16:10 
AnswerRe: Async example? Pin
Tom Janssens10-Nov-09 3:40
memberTom Janssens10-Nov-09 3:40 
GeneralRe: Async example? Pin
Indrora28-Nov-09 4:17
memberIndrora28-Nov-09 4:17 
GeneralRead Until Method Pin
dchard8-Oct-09 4:52
memberdchard8-Oct-09 4:52 
GeneralRe: Read Until Method Pin
Tom Janssens12-Oct-09 23:31
memberTom Janssens12-Oct-09 23:31 
GeneralBest Telnet Client for C# Pin
dchard8-Oct-09 4:47
memberdchard8-Oct-09 4:47 
AnswerRe: Best Telnet Client for C# Pin
Tom Janssens12-Oct-09 23:29
memberTom Janssens12-Oct-09 23:29 
GeneralSome modifications that I would use to make sure telnet pushes your commands through Pin
Stephen Melvin16-Jul-09 6:28
memberStephen Melvin16-Jul-09 6:28 
GeneralRe: Some modifications that I would use to make sure telnet pushes your commands through Pin
Tom Janssens4-Aug-09 22:08
memberTom Janssens4-Aug-09 22:08 
QuestionProblem with SendTelnetData [modified] Pin
jeanosorio21-Apr-09 5:46
memberjeanosorio21-Apr-09 5:46 
AnswerRe: Problem with SendTelnetData Pin
Tom Janssens21-Apr-09 21:43
memberTom Janssens21-Apr-09 21:43 
GeneralThanks Pin
alex_t14-Apr-09 8:04
memberalex_t14-Apr-09 8:04 
GeneralRe: Thanks Pin
Tom Janssens14-Apr-09 9:15
memberTom Janssens14-Apr-09 9:15 
GeneralFailed to connect:no login prompt Pin
krsk4u6-Oct-08 22:22
memberkrsk4u6-Oct-08 22:22 
AnswerRe: Failed to connect:no login prompt Pin
Tom Janssens22-Oct-08 23:39
memberTom Janssens22-Oct-08 23:39 
GeneralYour telnet client rejected our request to use char-at-a-time mode [modified] Pin
J1MPIC18-Jun-08 2:13
memberJ1MPIC18-Jun-08 2:13 
GeneralRe: Your telnet client rejected our request to use char-at-a-time mode Pin
J1MPIC24-Jun-08 8:55
memberJ1MPIC24-Jun-08 8:55 
AnswerRe: Your telnet client rejected our request to use char-at-a-time mode Pin
Tom Janssens18-Aug-08 20:27
memberTom Janssens18-Aug-08 20:27 
GeneralError with the server output Pin
Amer Azzaz9-May-08 0:23
memberAmer Azzaz9-May-08 0:23 
AnswerRe: Error with the server output Pin
Tom Janssens9-Jun-08 3:36
memberTom Janssens9-Jun-08 3:36 
GeneralParseTelnet function generates malformed packet Pin
HStidsen20-Sep-07 0:07
memberHStidsen20-Sep-07 0:07 
GeneralRe: ParseTelnet function generates malformed packet Pin
Tom Janssens7-Dec-07 4:28
memberTom Janssens7-Dec-07 4:28 
GeneralRe: ParseTelnet function generates malformed packet Pin
Darked9-Apr-08 22:13
memberDarked9-Apr-08 22:13 
AnswerRe: ParseTelnet function generates malformed packet Pin
HStidsen10-Apr-08 1:45
memberHStidsen10-Apr-08 1:45 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 6 Jun 2007
Article Copyright 2007 by Tom Janssens
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid