CTelnet for Win32/Linux ver 0.4 2001/10/28 ver 0.3 2000/09/05 ver 0.2 2000/08/30 ver 0.1 2000/07/22 Tomoichi Ebata E-mail:See http://www.kobore.net/mailAddress.gif http://www.kobore.net/ 0. History ~~~~~~~~~~ ver 0.4 2001/10/28 - Reviewed about license.(Changed to BSD license) ver 0.3 2001/09/05 - added two new methods for waiting for some specific characters except of login prompt (for example, relogin with "su" ) ver 0.2 2001/08/30 - Added the prompt waiting time against the misjudge of prompt waiting characters. ver 0.1 2001/07/22 - The first release 1. Copyright ~~~~~~~~~~~~ Copyright (c) 2000,2001,2002,2003,2004,2005 Tomoichi Ebata. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer as the first lines of this file unmodified. 2 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY Tomoichi Ebata ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Tomoichi Ebata BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2. What is "CTelnet for Win32/Linux" ? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "CTelnet for Win32/Linux" is a telnet agent library. With "CTelnet" class, you can do start, operations, termination of telnet,and get the result message. You can understand how to use Ctelnet, if you see the following codes. // telnetTest.cpp : Defines the entry point for the console application. // #include "StdAfx.h" #include #include "telnet.h" void main(void) { CTelnet telnet("192.168.0.1"); if (!telnet.isConnect()) { printf("%s\n",telnet.getmsg()); return; } printf(".....Telnet connection successfully\n"); if (!telnet.login("guest","guest")) { printf("%s\n",telnet.getmsg()); return; } printf(".....Login successfully\n"); telnet.cmd("su", "assword:"); printf("%s\n",telnet.getmsg()); telnet.cmd("admin"); printf("%s\n",telnet.getmsg()); telnet.cmd("whoami"); printf("%s\n",telnet.getmsg()); telnet.cmd("ls -la"); printf("%s\n",telnet.getmsg()); telnet.cmd("dummy"); printf("%s\n",telnet.getmsg()); telnet.cmd("date"); printf("%s\n",telnet.getmsg()); telnet.cmd("mail"); printf("%s\n",telnet.getmsg()); telnet.cmd("exit"); } 3. Environment for "CTelnet for Win32/Linux" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Some APIs of Regular expression library, re_comp(),re_exec() are needed. Linux possesses these function regularly, however Windows OSs don't. The following Regular expression library might be helpful. http://www.kobore.net/soft/soft.html#Rexpdll 4. Supported operation system ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This library is confirmed to be available on Windows NT SP5 and RedHat Linux 6.2J(Japanese version). However, this library uses the primitive APIs as possible, so you can compile this on almost all operation systems. At first, I wanted to use STL for the character managements (thought the program size should be big), however I knew there were many difficulties to make windows DLL, and I didn't like the situation that users should prepare STL. 5. CTelnet class methods ~~~~~~~~~~~~~~~~~~~~~~~~~ (1) CTelnet::CTelnet( const char* host = "localhost", // host name to connect(IP // address is also available) int messageSize = 1024, // Buffer size for getmsg() const char* prompt = "[$%#>] ", // Waiting prompt(Regular // expression is available) int port = 23, // Telnet port number double loginTimeOut = 5.0, // Login timeout (second(s)) double waitTimeOut = 1.5 // cmd(),waitfor() timeout // (second(s)) ); Creates a Ctelnet instance and establishes the telnet connection. (2) CTelnet::~CTelnet() cuts the telnet connection, and destroyes the instance. (3) bool CTelnet::isConnect(); Confirms if a Ctelnet instance is made. Return true if it succeed, or false. (4) bool CTelnet::login( const char* user, // user name const char* password ="" // password ); authenticates a login user with the user name and the password. Return true if it succeed, or false. (5) bool CTelnet::cmd( const char* str // input command ); inputs a command. The timeout uses the value that is set by constructor. Return true if it succeed, or false. (6) bool CTelnet::cmd( const char* str, // input command const char* waitstr // detected characters ); inputs a command and waits for specific characters (within 40 characters) The timeout uses the value that is set by constructor. Return true if it succeed, or false. (7) bool CTelnet::cmd( const char* str, // input command timeval waitTimeOut // timeout ); inputs a command. It is possible to specify the timeout. Return true if it succeed, or false. (8) bool CTelnet::cmd( const char* str, // input command const char* waitstr, // detected characters timeval waitTimeOut // timeout ); (9) inputs a command and waits for specific characters (within 40 characters) It is possible to specify the timeout. Return true if it succeed, or false. (10) bool CTelnet::waitfor( const char *str // input command ); waits for specific characters (within 40characters) The timeout uses the value that is set by constructor. Return true if it succeed, or false. (11) bool CTelnet::waitfor( const char* str, // input command timeval waitTimeOut // timeout ); waits for specific characters (within 40characters) It is possible to specify the timeout. Return true if it succeed, or false. (12) char* CTelnet::getmsg(void) shows the latest message that login(),cmd() and waitfor() return from telnet server. If login(),cmd() and waitfor() fail, the latest error message is shown. 6. Content of source code ~~~~~~~~~~~~~~~~~~~~~~~~~ Please use the archive file that includes source codes as a project. The library needs the following two files. telnet.cpp telnet.h The following is a test code. telnetTest.cpp The source codes of regular expression library are added in the project. Before you use them, please read as follows. http://www.hh.kobore.net/soft/rexpdll.txt 7. How to compile ~~~~~~~~~~~~~~~~~ Extract the archive in the appropriate directory, and compile your source file with VC++. In case of Linux, put the following three files telnet.cpp, telnet.h, telnetTest.cpp on the appropriate directory, and gcc telnet.cpp telnetTest.cpp and, ./a.out you can confirm that your program works with Ctelnet library. 8. Others ~~~~~~~~~ I made the Linux shared library for Ctelnet (not yet open). However, I recommend to compile your code with CTelnet source code. from the viewpoint of the performance. "telnet" is a very popular name, so I recommend not to use the name "as is" but to change the name.