Home >Backend Development >PHP Tutorial >Example of running Linux commands and starting SSH service in PHP_PHP Tutorial
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: