Example _ PHP Tutorial

Source: Internet
Author: User
Example of running Linux commands and starting the SSH service in PHP. After upgrading the VPS, the sshd service is not automatically started due to the compatibility problem between Ubuntu upstart and OpenVZ. after trying to upgrade the vePortal console and filemanager and submitting technical support, due to the compatibility between upstart and OpenVZ of Ubuntu, the sshd service is not automatically started. after trying the console and file manager of vePortal 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 and 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 );
}
?>


After VPS is renewed, the sshd service is not automatically started due to the compatibility problem between upstart and OpenVZ of Ubuntu. the console and file manager of vePortal and the technical support for submission are attempted...

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.