📜 ⬆️ ⬇️

Remote shutdown of the PHP linux server

Perhaps you, like me, needed to shut down the server remotely (a strange need, because it is the reason for the server to work constantly). In any case, each has its own motives. So, I had to turn off the remote server from another computer, shutting down with android and ios.


We will turn off the computer via ssh in a php script (then we don’t even need to write applications for android and ios, but simply create a link to the script). For this we need to put the ssh library for php. This procedure is described in sufficient detail here .

Next, you need to write a script that will turn off the computer switcher.php:
<?php if (!function_exists ("ssh2_connect")) die ("function ssh2_connect doesn't exist"); if(!($con = ssh2_connect("localhost", 22))){ echo "fail: unable to establish connection\n"; } else { //       if(!ssh2_auth_password($con, "root", $_GET["password"])) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; //   if (!($stream = ssh2_exec($con, "poweroff" ))) { echo "fail: unable to execute command\n"; } else { // collect returning data from command stream_set_blocking ($stream, true); $data = ""; while ($buf = fread ($stream,4096)) { $data .= $buf; } fclose ($stream); echo $data; } } } ?> 

')
as a result, to shut down the server, use the url of the type: 127.0.0.1/switcher.php?password=my_pass

That's all, now on any device, you can add shortcuts to the desktop on any device and everything is OK, compromising security =)

I would be glad to other ways, there were options for ssh connections directly from different platforms, but this increases the development time, although safer

Source: https://habr.com/ru/post/151526/


All Articles