Quick tool : A minimalistic Telnet library

marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" topmargin="0" leftmargin="0">
下载源码:http://www.codeproject.com/useritems/MinimalisticTelnet.asp

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.

marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" topmargin="0" leftmargin="0">

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 learning the other persons 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 some hours figuring out the minimal implementation of the telnet protocol, I decided to do a simple rewrite for the users of codeproject.

marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" topmargin="0" leftmargin="0">

Using the code

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

Collapse
            //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 "___FCKpd___0quot; or ">", otherwise the connection failed
            string prompt = s.TrimEnd();
            prompt = s.Substring(prompt.Length -1,1);
            if (prompt != "___FCKpd___0quot; && 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();
 marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" topmargin="0" leftmargin="0">

I suppose this is self-explaining, isn't it ? Create the telnet connection, login, send the login output to the screen, an 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 sript 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.

marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" topmargin="0" leftmargin="0">

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 whether the serveroutput ends with a "$" or a ">". If your telnet server has another prompt, please replace this.

Voting & Comments

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

History

2007-06-06 : First version

<script src="/script/togglePre.js" type="text/javascript"></script>
marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd336_280.html" frameborder="0" width="336" scrolling="no" height="280" topmargin="0" leftmargin="0">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值