Example of running the Linux command in PHP and starting the SSH service _php tutorial

Source: Internet
Author: User
Tags vps
After upgrading the VPS, due to the compatibility of Ubuntu upstart and OpenVZ, the SSHD service does not start automatically, after trying the veportal console and File Manager and submit technical support can not solve the problem.

Only on their own, the general idea is to do the SU command in PHP to perform the sshd service, because WordPress is still alive, and can be directly in the background to edit the topic related PHP script. Just insert the prepared code snippet into header.php and visit the homepage in your browser.

Related Code logic
1. Using PHP proc_open to open a process, redirect stdin, stdout, stderr, here will execute a python program.
2. Open a pty in this Python program and run an SH.
3. Using the redirected stdin pipe in step 1 to send the SU command to the Python program, Python writes the command data from stdin to PTMX, when the stdin, stdout, and stderr of SH are redirected to the PYT Hon Open ptmx on paired pts. This means that the SU command will eventually be transferred to the SH process.
4. The SH process naturally executes the SU command, when the SU process's stdin, stdout, stderr will also be redirected to that pts.
5. After sleep for some time (mainly when Su really ran up), and then write the password, the data flow process is consistent with steps 3 and 4.

Related Code snippet:
Copy the Code code 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);
}
?>

http://www.bkjia.com/PHPjc/788643.html www.bkjia.com true http://www.bkjia.com/PHPjc/788643.html techarticle after upgrading the VPS, due to Ubuntu upstart and OpenVZ compatibility issues, resulting sshd service does not automatically start, in the attempt to veportal console with File Manager and submit technical support ...

  • Related Article

    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.