This article describes how to run Linux commands in PHP and start the SSH service. Because the SSH service of VPS fails, the server cannot be accessed, for more information, see
This article describes how to run Linux commands in PHP and start the SSH service. Because the SSH service of VPS fails, the server cannot be accessed, for more information, see
After the VPS is upgraded, the sshd service is not automatically started due to the compatibility problem between Ubuntu upstart and OpenVZ, after trying the vePortal console and file manager and submitting technical support, the problem cannot be solved.
You can only rely on yourself. The general idea is to run the su command in PHP to execute the sshd service. Because WordPress is still alive, you can directly edit the PHP script related to the topic in the background. Just insert the prepared code snippet into header. php and visit the homepage in the browser.
Related code Logic
1. Use proc_open of PHP to open a process and redirect stdin, stdout, and stderr. A python program is executed here.
2. Open a pty in this python program and run a sh.
3. use stdin pipe in step 1 to send the su command to the python program. python writes the command data from stdin to ptmx, stdout and stderr are redirected to the pts paired with the ptmx opened in python. That is to say, the su command will eventually be transferred to the sh process for processing.
4. The sh process naturally executes the su command, and stdin, stdout, and stderr of the su process will also be redirected to the pts.
5. After sleep for a period of time (mainly when su is running), write the password again. The data flow process is consistent with Steps 3 and 4.
Related code snippets:
The Code is as follows:
$ Descriptorspec = array (
0 => array ("pipe", "r"), // stdin
1 => array ("pipe", "w"), // stdout
2 => array ("pipe", "w") // stderr
);
$ Process = proc_open ("python-c 'import pty; pty. spawn (\"/bin/sh \ ") '", $ descriptorspec, $ pipes );
If (is_resource ($ process )){
Fwrite ($ pipes [0], "su-c 'service ssh start' root \ n ");
Fflush ($ pipes [0]);
Sleep (3 );
Fwrite ($ pipes [0], "PASSWORD \ n ");
Fflush ($ pipes [0]);
Fclose ($ pipes [0]);
Fclose ($ pipes [1]);
Fclose ($ pipes [2]);
Proc_close ($ process );
}
?>