Home  >  Article  >  Backend Development  >  Example of running Linux commands and starting SSH service in PHP_PHP Tutorial

Example of running Linux commands and starting SSH service in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:22963browse

After upgrading the VPS, due to the compatibility issue between Ubuntu's upstart and OpenVZ, the sshd service did not start automatically. After trying vePortal's console and file manager and submitting to technical support, the problem could not be solved.

You have to rely on yourself. The general idea is to execute the su command in PHP to execute the sshd service, because WordPress is still alive and you can directly edit theme-related PHP scripts in the background. Just insert the prepared code snippet into header.php and visit the homepage in the browser.

Related code logic
1. Use PHP's proc_open to open a process and redirect stdin, stdout, stderr, and a python program will be executed here.
2. Open a pty in this python program and run a sh.
3. Use the stdin pipe redirected in step 1 to send the su command to the python program. python will write the command data from stdin to ptmx. At this time, the stdin, stdout and stderr of sh are redirected to python. Open ptmx on paired pts. In other words, the su command will eventually be transferred to the sh process for processing.
4. The sh process naturally executes the su command. At this time, the stdin, stdout, and stderr of the su process will also be redirected to that pts.
5. After sleeping for a period of time (mainly waiting for su to really start running), write the password again. The data flow process is consistent with steps 3 and 4.

Related code snippets:

Copy code 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' rootn");
fflush($pipes[ 0]);
sleep(3);
fwrite($pipes[0], "PASSWORDn");
fflush($pipes[0]);
fclose($pipes[0] );
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/788643.htmlTechArticleAfter upgrading the VPS, due to the compatibility issue between Ubuntu's upstart and OpenVZ, the sshd service did not start automatically. After trying vePortal's console and file manager and submission technical support...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn