Example of running Linux commands and starting the SSH service in PHP

Source: Internet
Author: User
Tags vps
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 );
}
?>

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.