Click here to Skip to main content
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

 
AnswerRe: Parse problem at login PinmemberTom Janssens30-Jun-11 2:57 
GeneralRe: Parse problem at login Pinmembersleepyman30-Jun-11 7:35 
GeneralRe: Parse problem at login PinmemberTom Janssens1-Jul-11 0:44 
QuestionProblem executing telnet commands Pinmembersleepyman27-Jun-11 21:01 
AnswerRe: Problem executing telnet commands PinmemberTom Janssens27-Jun-11 21:51 
GeneralRe: Problem executing telnet commands [modified] Pinmembersleepyman27-Jun-11 22:39 
GeneralProblem Pinmemberfares121-May-11 4:58 
GeneralRe: Problem PinmemberTom Janssens22-May-11 3:57 
I have no idea; maybe you might need to wait longer or send a CRLF or something similar...

GeneralMy vote of 5 Pinmembermedophin11-May-11 16:30 
QuestionProblem with commands... Pinmemberraananv31-Dec-10 3:12 
AnswerRe: Problem with commands... PinmemberTom Janssens31-Dec-10 5:50 
GeneralRe: Problem with commands... Pinmemberraananv31-Dec-10 6:23 
GeneralMy vote of 5 PinmemberKapil Singh Verma30-Dec-10 20:55 
GeneralMy vote of 5 Pinmemberclueponic30-Nov-10 1:25 
GeneralHelpful article PinmemberMember 469491012-Oct-10 17:11 
Generalminilistic telnet Pinmemberjohn Saxby21-Sep-10 10:59 
GeneralRe: minilistic telnet PinmemberTom Janssens23-Sep-10 8:18 
Generalwow perfect :-) :-) :-) PinmemberMember 18269033-Sep-10 21:12 
GeneralRe: wow perfect :-) :-) :-) PinmemberTom Janssens3-Sep-10 23:01 
Questionconsole output message Pinmemberapfelwein24-Aug-10 0:45 
GeneralMy vote of 5 Pinmemberfreemap9123-Aug-10 22:16 
GeneralRe: My vote of 5 PinmemberTom Janssens24-Aug-10 0:27 
Generaltail -f Pinmembersundalongqueso15-Aug-10 0:30 
GeneralRe: tail -f PinmemberTom Janssens15-Aug-10 15:58 
GeneralRe: tail -f Pinmembersundalongqueso18-Aug-10 13:59 

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
Web04 | 2.8.1411022.1 | Last Updated 6 Jun 2007
Article Copyright 2007 by Tom Janssens
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid