Click here to Skip to main content
11,412,833 members (73,934 online)
Click here to Skip to main content

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

Introduction

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.

Background

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);
Console.Write(s);

// 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
    Console.Write(tc.Read());

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

    // display server output
    Console.Write(tc.Read());
}

Console.WriteLine("***DISCONNECTED");
Console.ReadLine();

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 !!

History

  • 2007-06-06: First version

License

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

Share

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.

Blog: http://tojans.me
Github: http://github.com/ToJans
Twitter: http://twitter.com/ToJans
LinkedIn: http://www.linkedin.com/in/tomjanssens

Comments and Discussions

 
QuestionThis throws an exception when trying to connect with the tcp socket Pin
Member 10855207 at 30-May-14 9:51
memberMember 1085520730-May-14 9:51 
QuestionSending commands to Cisco switch? [modified] Pin
Member 10781587 at 29-Apr-14 5:41
memberMember 1078158729-Apr-14 5:41 
QuestionTelnet over Telnet session Pin
GabrielBarros at 26-Mar-14 10:15
memberGabrielBarros26-Mar-14 10:15 
GeneralMy vote of 5 Pin
rcole117 at 3-Jan-14 6:52
memberrcole1173-Jan-14 6:52 
QuestionA minimalistic Telnet library is not working for web application Pin
wskumar at 28-Nov-13 12:05
memberwskumar28-Nov-13 12:05 
QuestionMissing when Read output Multiline " ---more--------" Pin
Kieu Gol at 24-Nov-13 8:19
memberKieu Gol24-Nov-13 8:19 
GeneralSimple and Straight Forward Pin
Richard Osafo at 24-Oct-13 4:48
memberRichard Osafo24-Oct-13 4:48 
Questiontset: unknown terminal type network Terminal type? Pin
superselector at 11-Aug-13 23:40
membersuperselector11-Aug-13 23:40 
QuestionThanks Tom, you deserve well the five star rating. Pin
Jane-Jane Doe at 30-Jul-13 0:35
memberJane-Jane Doe30-Jul-13 0:35 
AnswerRe: Thanks Tom, you deserve well the five star rating. Pin
Tom Janssens at 30-Jul-13 0:38
memberTom Janssens30-Jul-13 0:38 
Questionhelp me . Pin
student_milworm at 14-Jul-13 22:52
memberstudent_milworm14-Jul-13 22:52 
QuestionVery easy to use! Pin
alexdhu at 1-Jul-13 7:02
memberalexdhu1-Jul-13 7:02 
GeneralMy vote of 5 Pin
Jon Sagara at 6-Jun-13 5:51
memberJon Sagara6-Jun-13 5:51 
GeneralRe: My vote of 5 Pin
Tom Janssens at 1-Jul-13 21:38
memberTom Janssens1-Jul-13 21:38 
QuestionIssue with login [modified] Pin
TadejP81 at 24-May-13 10:50
memberTadejP8124-May-13 10:50 
QuestionCannot download Pin
itiwcsingkaww at 22-May-13 16:28
memberitiwcsingkaww22-May-13 16:28 
Questionhow to i get a serial number of a device using the ip address through telnet using this code Pin
jinnah2013 at 28-Apr-13 9:24
memberjinnah201328-Apr-13 9:24 
QuestionThanks and a little Question Pin
Member 9996871 at 25-Apr-13 6:29
memberMember 999687125-Apr-13 6:29 
QuestionThank You. Pin
Member 9997951 at 18-Apr-13 18:19
memberMember 999795118-Apr-13 18:19 
AnswerRe: Thank You. Pin
Tom Janssens at 18-Apr-13 21:53
memberTom Janssens18-Apr-13 21:53 
QuestionReading more than one line at a time Pin
slopesandsam at 9-Apr-13 20:45
memberslopesandsam9-Apr-13 20:45 
AnswerRe: Reading more than one line at a time Pin
Tom Janssens at 9-Apr-13 21:46
memberTom Janssens9-Apr-13 21:46 
GeneralRe: Reading more than one line at a time Pin
slopesandsam at 9-Apr-13 21:54
memberslopesandsam9-Apr-13 21:54 
GeneralMy vote of 5 Pin
nieljake at 18-Jan-13 6:08
membernieljake18-Jan-13 6:08 
BugBuggy Pin
Member 9666884 at 7-Dec-12 15:32
memberMember 96668847-Dec-12 15:32 
QuestionUnable to login Pin
rogerknit at 7-Nov-12 19:12
memberrogerknit7-Nov-12 19:12 
AnswerRe: Unable to login Pin
Tom Janssens at 7-Nov-12 21:04
memberTom Janssens7-Nov-12 21:04 
QuestionLogin problem [modified] Pin
Luis Velarde at 5-Sep-12 14:38
memberLuis Velarde5-Sep-12 14:38 
AnswerRe: Login problem Pin
Luis Velarde at 6-Sep-12 6:03
memberLuis Velarde6-Sep-12 6:03 
GeneralRe: Login problem Pin
Tom Janssens at 6-Sep-12 7:38
memberTom Janssens6-Sep-12 7:38 
Ah great; thanks!

AnswerRe: Login problem Pin
Kieu Gol at 24-Nov-13 8:22
memberKieu Gol24-Nov-13 8:22 
QuestionTab Key Pin
prithumit at 28-Aug-12 4:40
memberprithumit28-Aug-12 4:40 
AnswerRe: Tab Key Pin
Tom Janssens at 28-Aug-12 5:11
memberTom Janssens28-Aug-12 5:11 
AnswerRe: Tab Key Pin
EgyptianRobot at 28-Aug-12 5:47
memberEgyptianRobot28-Aug-12 5:47 
QuestionCannot retrieve all the result data Pin
Phea Daravuth at 15-Jun-12 0:08
memberPhea Daravuth15-Jun-12 0:08 
AnswerRe: Cannot retrieve all the result data Pin
Tom Janssens at 15-Jun-12 1:46
memberTom Janssens15-Jun-12 1:46 
Questionusing dot notation for TelnetConnection parameter Pin
garaber at 16-May-12 17:09
membergaraber16-May-12 17:09 
AnswerRe: using dot notation for TelnetConnection parameter Pin
garaber at 16-May-12 17:19
membergaraber16-May-12 17:19 
GeneralRe: using dot notation for TelnetConnection parameter Pin
Tom Janssens at 17-May-12 21:21
memberTom Janssens17-May-12 21:21 
QuestionLittle Change for write function with new lines - Login now Ok! Pin
renatoosx at 8-Nov-11 3:13
memberrenatoosx8-Nov-11 3:13 
QuestionParse problem at login Pin
sleepyman at 30-Jun-11 1:48
membersleepyman30-Jun-11 1:48 
AnswerRe: Parse problem at login Pin
Tom Janssens at 30-Jun-11 2:57
memberTom Janssens30-Jun-11 2:57 
GeneralRe: Parse problem at login Pin
sleepyman at 30-Jun-11 7:35
membersleepyman30-Jun-11 7:35 
GeneralRe: Parse problem at login Pin
Tom Janssens at 1-Jul-11 0:44
memberTom Janssens1-Jul-11 0:44 
QuestionProblem executing telnet commands Pin
sleepyman at 27-Jun-11 21:01
membersleepyman27-Jun-11 21:01 
AnswerRe: Problem executing telnet commands Pin
Tom Janssens at 27-Jun-11 21:51
memberTom Janssens27-Jun-11 21:51 
GeneralRe: Problem executing telnet commands [modified] Pin
sleepyman at 27-Jun-11 22:39
membersleepyman27-Jun-11 22:39 
GeneralProblem Pin
fares1 at 21-May-11 4:58
memberfares121-May-11 4:58 
GeneralRe: Problem Pin
Tom Janssens at 22-May-11 3:57
memberTom Janssens22-May-11 3:57 
GeneralMy vote of 5 Pin
medophin at 11-May-11 16:30
membermedophin11-May-11 16:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150427.1 | Last Updated 6 Jun 2007
Article Copyright 2007 by Tom Janssens
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid