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.
Some example?
Thanks.
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).
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).
ASKER
The OS is Windows :-(
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.
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 TelnetSingleCommandConsole App.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(HostN ame,UserID ,Password, Command);
My_Telnet_Client_Cmd.DoWai tForThread End();
WSACleanup();
}
int main(int argc, char* argv[])
{//The following command sends command to run test.csh script file
SendTelnetCommand("telnets ite.foo.co m","UserNa me07","pas sword", "./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
#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(HostN
My_Telnet_Client_Cmd.DoWai
WSACleanup();
}
int main(int argc, char* argv[])
{//The following command sends command to run test.csh script file
SendTelnetCommand("telnets
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.
ASKER
Thanks Axter,
but I have 2 errors:
Error: Error: Unresolved external 'Telnet_Client_Cmd::Telnet _Client_Cm d(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::DoWait ForThreadE nd()' referenced from module prova_telnet.cpp
but I have 2 errors:
Error: Error: Unresolved external 'Telnet_Client_Cmd::Telnet
Error: Error: Unresolved external 'Telnet_Client_Cmd::DoWait
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?
What compiler are you using?
Did you add the *.cpp files to your project?
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(cons t 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(unsi gned int,void*&,Telnet_Client_C md*)' referenced from module telnet_client_cmd.cpp
Error: Error: Unresolved external 'CSocketTx::CSocketTx(unsi gned int,void*&,Telnet_Client_C md*)' 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 telnet_client_cmd.cpp.
No more the last 2 errors, but now:
Error: Error: Unresolved external 'CSocketDx::CSocketDx(cons
Error: Error: Unresolved external 'CSocketDx::TelnetConnect(
Error: Error: Unresolved external 'CSocketRx::CSocketRx(unsi
Error: Error: Unresolved external 'CSocketTx::CSocketTx(unsi
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
>>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.
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.
ASKER
Thanks Axter,
but when i execute the program there is this error: "You have accidently used the dummy version of OwlMain"
but when i execute the program there is this error: "You have accidently used the dummy version of OwlMain"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!!