Link to home
Start Free TrialLog in
Avatar of micba
micba

asked on

Telnet in C++. Open session, send commands and save received data on file.

I'd like to write a C++ program able to open a telnet session, send commands and save on a file received data.
Some example?
Thanks.
Avatar of pankajtiwary
pankajtiwary

Looking at your peoblem, there are 2 solutions. Either you can make a server and a client of your own and define your own commands (login, write, read, blah blah...) but that will be a long job. The better practise is to create just a client and use the well defined functions for the transactions. I have not worked on telnet n all so, you need to yourself find the specs on net and inserstand the commands about how to interact with the server.

After doing all this, you need to know how to code sockets. In this part you will actually connecting to the server, send the predefined commands, get the results back and write it into a file.

You can start on your own and we will help you.

cheers!!!
Which OS?

In Unix, it's (relatively) easy:
1. Open a pipe with pipe(). You'll get two file descriptors, one for the parent and one for the child.
2. fork.
3. use dup2 in the child to replace both stdin (fd 0) and stdout (fd 1) with the child fd from pipe (you can also replace stderr fd 2).
4. Exec telnet.

Then you can simply read and write from the parent end of the pipe - it's connected with telnet's stdin/stdout.

In case you want to serve stdin and stdout simultaneously (like to catch unexpected output), you should set the parent's pipe end to non-blocking:

int parent_fl = fcntl(parent_pipe,F_GETFL);
fcntl(parent_pipe, F_SETFL, parent_fl | O_NONBLOCK);

Now reading/writing when the pipe is empty/full will return -1 with errno == EAGAIN (Resource temporarily unavailable).
Avatar of micba

ASKER

The OS is Windows :-(
Avatar of Axter
Check out the following links:
http://code.axter.com/TelnetSingleCommandConsoleApp.ZIP
http://code.axter.com/FTPclient.h
http://code.axter.com/FTPclient.cpp

I recommend you use ftp to transfer the file, and use telnet to issue a command.
You can use code in above link as a starting point.
Here's a code example using the code in the above TelnetSingleCommandConsoleApp.ZIP project:

#include "SocketRx.h"
#include "SocketDx.h"
#include "SocketTx.h"

void SendTelnetCommand(const char* HostName, const char* UserID,
                      const char* Password, const char* Command,
                      const char* UserIDPrompt = NULL,
                      const char* PasswordPrompt = NULL,
                      const char* LogoutPrompt = NULL,
                      int PortToRunOn = IPPORT_TELNET)
{
     WSADATA wd;
     WSAStartup(0x0101,&wd);
     Telnet_Client_Cmd My_Telnet_Client_Cmd(HostName,UserID,Password, Command);
     My_Telnet_Client_Cmd.DoWaitForThreadEnd();
     WSACleanup();
}


int main(int argc, char* argv[])
{//The following command sends command to run test.csh script file
     SendTelnetCommand("telnetsite.foo.com","UserName07","password", "./test.csh");

     return 0;
}



Also check out the following links:

Console telnet for Win32 including source code
http://www.musc.edu/~brannanp/telnet

A console telnet application with source code
http://www.itribe.net/virtunix/telc10a.zip

http://users.neca.com/vmis/wsock.htm

http://www.codeproject.com/internet/telnet_server.asp

http://archive.ncsa.uiuc.edu/SDG/Software/PCTelnet
You can also get the PuTTY source code from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Yes, putty is definitely a good example.
Avatar of micba

ASKER

Thanks Axter,
but I have 2 errors:

Error:  Error: Unresolved external 'Telnet_Client_Cmd::Telnet_Client_Cmd(const char*,const char*,const char*,const char*,const char*,const char*,const char*,int)' referenced from module prova_telnet.cpp

Error:  Error: Unresolved external 'Telnet_Client_Cmd::DoWaitForThreadEnd()' referenced from module prova_telnet.cpp
This is trickyer than you think.  You probablyu need to have two threads, one sending commands, one reading back the output.  If you need the sending and receiving to be "smart" and syncronzed, that's even harder.  Good luck.

>>but I have 2 errors:

What compiler are you using?
Did you add the *.cpp files to your project?
Avatar of micba

ASKER

My compiler is Borland C++ 5.01.
To test I made a project and I added: 1 file with your code example and file telnet_client_cmd.cpp.
No more the last 2 errors, but now:

Error:  Error: Unresolved external 'CSocketDx::CSocketDx(const char*,int)' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CSocketDx::TelnetConnect()' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CSocketRx::CSocketRx(unsigned int,void*&,Telnet_Client_Cmd*)' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CSocketTx::CSocketTx(unsigned int,void*&,Telnet_Client_Cmd*)' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CSocketTx::~CSocketTx()' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CSocketRx::~CSocketRx()' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CSocketDx::~CSocketDx()' referenced from module telnet_client_cmd.cpp
Error:  Error: Unresolved external 'CProtocolRx::~CProtocolRx()' referenced from module telnet_client_cmd.cpp
>>To test I made a project and I added: 1 file with your code example and file

In order to compile the code, you need *all* the source files (except for TelnetSendCmdApp.cpp).

You need to add the other source files that are in the example project to your project.
You'll also need the header files in the example project.
Avatar of micba

ASKER

Thanks Axter,
but when i execute the program there is this error: "You have accidently used the dummy version of OwlMain"
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial