Example of running Linux commands and starting SSH service in PHP _php example

Source: Internet
Author: User
Tags php script sleep stdin

After upgrading the VPS, due to the compatibility problem of Ubuntu upstart and OpenVZ, the SSHD service does not start automatically, after attempting veportal console and File Manager and submitting technical support can not solve the problem.

Can only rely on their own, the general idea is to perform the SU in PHP to execute the sshd service, because WordPress is still alive, and can be directly in the background edit the subject-related PHP script. Just insert the prepared code snippet into the header.php and visit the homepage in the browser.

Related Code logic
1. Use PHP's proc_open to open a process, redirect stdin, stdout, stderr, where a python program is executed.
2. Open a pty in this Python program and run an SH.
3. Using stdin pipe in step 1 to send the SU command to a python program, Python writes the command data from stdin to the PTMX, while the stdin, stdout, and stderr of SH are redirected to and PYT Hon Open ptmx paired with Pts. In other words, the SU command will eventually be transferred to the SH process.
4. The SH process naturally executes the SU command, at which point the stdin of the SU process, stdout, and stderr will also be redirected to that pts.
5. After a period of sleep (mainly such as Su really ran up), and then write the password, the data flow process and steps 3, 4 consistent.

Related Code Snippets:

Copy Code code as follows:

<?php
$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.