?
Solved

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

Posted on 2004-09-09
14
Medium Priority
?
2,384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

743 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