Super lightweight remote command line client and server (including File Transfer)

Source: Internet
Author: User

Super lightweight remote command line client and server (including 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.