Sever Remote Control Program works + source code

Source: Internet
Author: User
This software is still compiled in C. It can be implemented in Linux environment: download documents, edit documents, disable remote hosts, restart remote hosts, and modify administrator passwords.
As programming is just my hobby, And the level is really limited, so the software is relatively spam, I hope you can give me some advice
This software can only run in Linux.
The key code for server implementation is as follows:
Int main (INT argc, char ** argv)
// Argc is the number of parameters. argv [0] corresponds to the program. argv [1] corresponds to-P, argv [2] corresponds to the port number.
{
....................................
If (argc! = 3 | strcmp (argv [1], "-P") | (Port = atoi (argv [2]) <1024 ){
// If argc is not 0, argv [1] is not-P, or the port number is <1024, exit
....................................
}
If (uname (& myname) <0 | (He = gethostbyname (myname. nodename) = NULL ){
// Obtain the Host Name
....................................
}
Hostaddr = * (unsigned long *) (He-> h_addr );
Listen_fd = init_socket (port );
// Initialize the socket, create, bind, and listen to the socket
If (P = strrchr (argv [0], '/')! = NULL)
// Obtain the "/" string from the last occurrence to the end, that is, the program name, assigned to P
P ++;
Else
P = argv [0];
// If P is null, argv [0] is the program name and is assigned to P
Sprintf (root_path, "/APP/% s", P); // path: the program name under the app
P = argv [0] + strlen (argv [0])-1;
While (* P & * P! = '/'){
* P = toupper (* P); // converts a character to an uppercase English character.
P --;
}
Daemon (1, 1); // daemon
Syslog (log_err, "*** starting % s on % s ***", p + 1, inet_ntoa (* (struct in_addr *) & hostaddr )); // write related information in system logs
....................................
For (;;){
Fd_zero (& afdset );
// Afdset is the file descriptor set. The contact between afdset and all file handles is cleared.
Maxfd =-1;
Fd_set (listen_fd, & afdset); // Add a socket
If (listen_fd> maxfd)
Maxfd = listen_fd; // maintain the maximum value of maxfd.
If (select (maxfd + 1, & afdset, null) <0)
Continue;
....................................
If (fd_isset (listen_fd, & afdset ))
Conn_fd = accept (listen_fd, (struct sockaddr *) & USA, & sz );
// Return value success: return the final server-side file descriptor. At this time, the server can write information to this descriptor. Conn_fd = listen_fd.
Failed:-1 is returned. Conn_fd =-1.
Else
Continue;
....................................
If (r = fork () <0 ){
// The main task of Fork () is to initialize the data structure of the process to be created. When fork ()! = 0 indicates that it is a parent process and must be terminated.
....................................
} Else if (r = 0 ){
// When fork () = 0, it indicates that it is a sub-process and the execution
Client_addr = USA;
Close (listen_fd );
// Mark the set interface with a closed flag and return the process immediately
Get_http (); // obtain the HTTP request method through this function, and call do_cgi ()
Exit (0 );
}
Close (conn_fd );
}
}

The key code for downloading the documentation is as follows:
My_cgi ()
{....................................
Else if (strcmp (P, "Download") = 0)
// Judge the form variable. If it is download, run the down_load function.
Down_load (); // display the download document interface and specify the operation method for the demo document
....................................
}
Static down_load ()
{....................................
If (P = getenv ("path_info") = NULL |
Strncmp (P, "/demo/", 6 )! = 0 ){
Disp_file_menu (0 );
// Call disp_file_menu (0) by judging the path and Document Information. The parameter 0 indicates that the document download page is displayed.
Return;
}

If (FP = fopen (FN, "R "))! = NULL) {// fopen (FN, "R") indicates opening the file FN in read-only mode
While (I = fread (BUF, 1, 4096, FP)> 0)
Fwrite (BUF, 1, I, stdout );
Fclose (FP); // download the selected document
....................................
}

The key code for implementing the edit document function is as follows:
Void edit_file ()
{....................................
Disp_file_menu (1 );
// Call disp_file_menu (1) by judging the path and Document Information. Parameter 1 indicates that the document editing page appears.
....................................
Disp_edit_file (0 );
// Judge the status of the document. When the parameters are 1, 3, and 9, the corresponding actions are performed, and other parameters are not executed.
....................................
Disp_edit_file (1); // If the parameter is 1, "host connection failed" is returned"
....................................
Disp_edit_file (9); // If the parameter is 9, "the file is modified successfully" is returned"
....................................
}

Void disp_edit_file (INT type)
{....................................
Printf ("<script language =" JavaScript "> ");
Printf ("function thisform_onsubmit ()");
// After modifying the document, press "OK" to execute the Javascript script program and save the modified document.
Printf ("{");
Printf ("Return confirm (" Save the modified document? ");");
Printf ("}");
Printf ("</SCRIPT> ");
....................................
If (type = 1) // return different feedback information for different type values
Printf ("<p align =" center "> <strong> <font color =" # ff0000 "> host connection failed! </Font> </strong> </P> ");
Else if (type = 3)
....................................
Else if (type = 9)
....................................
}

The key code for disabling the remote host function is as follows:
Void host_stop ()
{
If (get_form ("stat") = NULL ){
Disp_host_control_hoststop (); // The host closing interface is displayed.
Return;
}
Execlp ("init", "init", "0", 0); // set the third parameter of execlp to 0 and close the host remotely.

The key code for restarting the remote host function is as follows:
Void host_restart ()
{
If (get_form ("stat") = NULL ){
Disp_host_control_hostrestart (); // display the hot start host interface
Return;
}
Execlp ("init", "init", "6", 0); // set the third parameter of execlp to 6 and restart the system.
}
 

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.