Solved

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

Posted on 2004-09-09
14
2,146 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:micba
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12013989
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!!!
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12014395
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).
0
 

Author Comment

by:micba
ID: 12014555
The OS is Windows :-(
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 30

Expert Comment

by:Axter
ID: 12014791
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12014849
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
0
 
LVL 30

Expert Comment

by:Axter
ID: 12014857
You can also get the PuTTY source code from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12015031
Yes, putty is definitely a good example.
0
 

Author Comment

by:micba
ID: 12015040
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
0
 
LVL 22

Expert Comment

by:grg99
ID: 12015302
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.

0
 
LVL 30

Expert Comment

by:Axter
ID: 12015582
>>but I have 2 errors:

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

Author Comment

by:micba
ID: 12015858
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
0
 
LVL 30

Expert Comment

by:Axter
ID: 12016183
>>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.
0
 

Author Comment

by:micba
ID: 12024248
Thanks Axter,
but when i execute the program there is this error: "You have accidently used the dummy version of OwlMain"
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 12025231
>>but when i execute the program there is this error: "You have accidently used the dummy version of OwlMain"

It looks like you might not have the required telnet logon information.

Verify your logon information, by using an existing telnet program.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now