Click here to Skip to main content
13,191,331 members (78,681 online)
Click here to Skip to main content
Add your own
alternative version


138 bookmarked
Posted 6 Jun 2007

Quick tool : A minimalistic Telnet library

, 6 Jun 2007
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 Virtual Sales Lab
Belgium Belgium

You may also be interested in...


Comments and Discussions

AnswerRe: Thanks Tom, you deserve well the five star rating. Pin
Tom Janssens29-Jul-13 23:38
memberTom Janssens29-Jul-13 23:38 
Questionhelp me . Pin
student_milworm14-Jul-13 21:52
memberstudent_milworm14-Jul-13 21:52 
QuestionVery easy to use! Pin
alexdhu1-Jul-13 6:02
memberalexdhu1-Jul-13 6:02 
GeneralMy vote of 5 Pin
Jon Sagara6-Jun-13 4:51
memberJon Sagara6-Jun-13 4:51 
GeneralRe: My vote of 5 Pin
Tom Janssens1-Jul-13 20:38
memberTom Janssens1-Jul-13 20:38 
QuestionIssue with login Pin
TadejP8124-May-13 9:50
memberTadejP8124-May-13 9:50 
QuestionCannot download Pin
itiwcsingkaww22-May-13 15:28
memberitiwcsingkaww22-May-13 15:28 
Questionhow to i get a serial number of a device using the ip address through telnet using this code Pin
jinnah201328-Apr-13 8:24
memberjinnah201328-Apr-13 8:24 
how to i get a serial number of a device using the ip address through telnet using this code
QuestionThanks and a little Question Pin
Member 999687125-Apr-13 5:29
memberMember 999687125-Apr-13 5:29 
QuestionThank You. Pin
Member 999795118-Apr-13 17:19
memberMember 999795118-Apr-13 17:19 
AnswerRe: Thank You. Pin
Tom Janssens18-Apr-13 20:53
memberTom Janssens18-Apr-13 20:53 
QuestionReading more than one line at a time Pin
slopesandsam9-Apr-13 19:45
memberslopesandsam9-Apr-13 19:45 
AnswerRe: Reading more than one line at a time Pin
Tom Janssens9-Apr-13 20:46
memberTom Janssens9-Apr-13 20:46 
GeneralRe: Reading more than one line at a time Pin
slopesandsam9-Apr-13 20:54
memberslopesandsam9-Apr-13 20:54 
GeneralMy vote of 5 Pin
nieljake18-Jan-13 5:08
membernieljake18-Jan-13 5:08 
BugBuggy Pin
Member 96668847-Dec-12 14:32
memberMember 96668847-Dec-12 14:32 
GeneralRe: Buggy Pin
Member 1153152919-Aug-15 5:11
memberMember 1153152919-Aug-15 5:11 
QuestionUnable to login Pin
rogerknit7-Nov-12 18:12
memberrogerknit7-Nov-12 18:12 
AnswerRe: Unable to login Pin
Tom Janssens7-Nov-12 20:04
memberTom Janssens7-Nov-12 20:04 
QuestionLogin problem Pin
Luis Velarde5-Sep-12 13:38
memberLuis Velarde5-Sep-12 13:38 
AnswerRe: Login problem Pin
Luis Velarde6-Sep-12 5:03
memberLuis Velarde6-Sep-12 5:03 
GeneralRe: Login problem Pin
Tom Janssens6-Sep-12 6:38
memberTom Janssens6-Sep-12 6:38 
AnswerRe: Login problem Pin
Kieu Gol24-Nov-13 7:22
memberKieu Gol24-Nov-13 7:22 
QuestionTab Key Pin
prithumit28-Aug-12 3:40
memberprithumit28-Aug-12 3:40 
AnswerRe: Tab Key Pin
Tom Janssens28-Aug-12 4:11
memberTom Janssens28-Aug-12 4:11 

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.

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