Solved

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

Posted on 2004-09-09
14
2,043 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

11 Experts available now in Live!

Get 1:1 Help Now