Super lightweight remote command line client and server (including file transfer), command line File Transfer
Function: Implements TCPIP-based command line operations on remote computers and supports remote file transmission.
Platform: windows (similar to linux)
Principle: after the client connects to the server, it first listens to the keyboard input, saves the cmd command entered by the keyboard, and sends it to the server. The server saves the command and runs it by calling the command line, obtain the command line execution result and send it to the client. To obtain the file command, read the file and send the file to the client.
Code: source code download link
// Core code int getfile (const char * path, SOCKET ClientSocket, const char * file) {int rtn = 0; char buffer [DEFAULT_BUFLEN]; // string finalfile = string (path) + "\" + string (file); printf ("finalfile: % s", finalfile. c_str (); FILE * fp = fopen (finalfile. c_str (), "rb"); while (1) {int size = fread (buffer, 1, sizeof (buffer), fp); if (size = 0) {if (feof (fp) {# if 0if (-1 = sendData (ClientSocket, buffer, size, size) {rtn =-1; IF_ERROR _ GOTO_END ;}# endifbreak;} else if (ferror (fp) {rtn = ferror (fp); IF_ERROR_GOTO_END ;}} else {if (-1 = sendData (ClientSocket, buffer, size, size) {rtn =-1; IF_ERROR_GOTO_END ;}}end: fclose (fp); return rtn ;}// http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-cint exec (char * lastpath, int pathsize, char * cmd, SOCKET ClientSocket, string out) {// the last line of the command result string is the current path; it cannot be placed at the most First, you need to obtain the path char finalcmd [0x300] = ""; char buffer [0x300]; // int rtn = 0; string result = ""; strcpy (finalcmd, (string ("cd/d") + string (lastpath) + string ("&") + string (cmd) + string ("& cd ")). c_str (); // 2> & 1) & the error message and current path strength are added to the & cd method, but only the last error is displayed when an error is reported and no path is reported, therefore, FILE * pipe = _ popen (finalcmd, "r"); static int I = 0; printf ("\ n >>==== command % d =====\ ncmd: % s \ n ==== command % d =======< <\ n ", I, finalcmd, I ++); if (! Pipe) return-1; while (! Feof (pipe) {if (fgets (buffer, sizeof (buffer), pipe )! = NULL) {result + = buffer; // printf ("% s", buffer); int sendLen = 0; if (-1 = sendData (ClientSocket, buffer, strlen (buffer), sendLen) {rtn =-1; IF_ERROR_GOTO_END;} if (strlen (buffer) {ZeroMemory (lastpath, pathsize); strcpy (lastpath, buffer );} zeroMemory (buffer, sizeof (buffer) ;}}_ pclose (pipe); pipe = NULL; out = result; END: if (pipe! = NULL) {_ pclose (pipe); pipe = NULL;} if (lastpath [strlen (lastpath)-1] = '\ n') lastpath [strlen (lastpath) -1] = '\ 0'; // remove the final carriage return printf ("lastpath: % s", lastpath); return rtn;} int sendData (SOCKET ClientSocket, char * buf, int bufLen, int & sendLen) {// Echo the buffer back to the senderint rtn = 0; rtn = send (ClientSocket, buf, bufLen, 0 ); sendLen = rtn; if (rtn = SOCKET_ERROR) {printf ("send failed with error: % d \ n", WSAGetLastError ();} // printf ("Bytes sent: % d \ n ", iSendResult); return rtn ;}
Test:
1. Enable the server process
2. Open the client process HackClient.exe 127.0.0.1 from the command line.
3. Use the dir command to browse the directory of the server program
4、get a file from the service end program, and copy the final hackserver.exe file from the bottom.
:
Note:
1. If the addrinfo struct is not defined in the compilation error, it may be because it was compiled with vc6, but this struct is not defined in the ws2tcpip. h file of vc6, which can be compiled with vs2008.
2. Open the server execution file server.exeand client.exe. The parameter of clientclient.exe is the IP address of the server.
3. Ensure that the server port can open the listener normally.
Refer:
Http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c
What are the differences between remote logon and file transfer? What protocols do they need? Which layer is the TCP/IP protocol?
Remote Logon (telnet) means to directly log on to the telnet server and perform related operations, which is equivalent to directly operating the system.
File Transfer (FTP) is to upload or download files on the server, only some operations on files.
They are all the top layer based on TCP/IP, and the application processes use telnet and ftp protocols respectively.
How can I remotely execute executable files on the server when logging on to FTP through the command line? The command is not executed on the local machine, but remotely.
If you can, use ssh or telnet to log on. ftp is generally used only for file transfer.