0

What I want is a PHP file that I can call that shuts a computer down using SSH. Simple as that. Below is something that I've knocked up. Obviously, it doesn't work. I'm no PHP expert. Maybe a PHP expert could show me how to get it working. Thanks.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('192.168.1.124');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

$ssh->write("sudo shutdown -h now");
sleep(5);
$ssh->write("password\n");

?>
neubert
  • 15,947
  • 24
  • 120
  • 212
JosephFTaylor
  • 99
  • 2
  • 11
  • does this answer help: http://stackoverflow.com/questions/6270419/how-to-execute-ssh-commands-via-php ? – Maximus2012 Aug 05 '13 at 16:40
  • I tried that but it comes up with host key validation errors and other crazy errors. Give me a minute and I'll tell you the exact errors and the code I've used. – JosephFTaylor Aug 05 '13 at 17:01
  • I've put echo in front of the $ssh->writes in my code above, it echo's 11 in the browser. I've also tried echo '$ssh->exec('sudo shutdown -h now');' but it shows 'sudo: no tty present and no askpass program specified' in the browser. 'echo $ssh->exec('ssh -t -t 192.168.1.124 "sudo shutdown -h now"');' tells me that 'Host key verification failed.' Thanks – JosephFTaylor Aug 05 '13 at 17:08
  • then I think its some sort of SSH error and nothing to do with PHP as such. – Maximus2012 Aug 05 '13 at 19:11
  • How to do sudo with phpseclib: http://phpseclib.sourceforge.net/ssh/examples.html#sudo – neubert Aug 06 '13 at 13:35

2 Answers2

1

I ended up finding a way to shutdown the computer safely using an AppleScript.

The PHP file is:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('192.168.1.124');
if (!$ssh->login('josephftaylor', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('osascript Documents/shutdown.scpt');

?>

The AppleScript is saved in my documents and is:

tell application "System Events"
    shut down
end tell

Hope this helps anyone else trying to do something similar to me. Thanks for everyone's help!

JosephFTaylor
  • 99
  • 2
  • 11
-1

You could try giving 'username' NOPASSWD privileges to /sbin/shutdown

Log into the remote box and do sudo visudo

Add the following line (adjust for correct username):

username ALL= NOPASSWD: /sbin/shutdown

Now that user should be able to sudo shutdown without a password, and hopefully sudo won't get mad about not being on a tty.

Source

Travis Hegner
  • 2,465
  • 1
  • 12
  • 11
  • This is a very bad idea - shutdown down a production machine *should* require supercow privileges, for obvious reasons. Additionally, the "password" is a good safe-guard for doing stupid things, like **halting** a server. – nand Aug 05 '13 at 18:11
  • I tried it and it still didn't work. I must have done it wrong. – JosephFTaylor Aug 05 '13 at 18:17
  • Under normal circumstances, I would agree, but the OP must give himself the privileges to accomplish his goal. If the goal **is** to shutdown the machine, then it's **not** a security risk. As long as the user account is dedicated to only this script, and this script is properly protected then it won't be an issue. – Travis Hegner Aug 05 '13 at 18:19
  • I'm trying to shutdown my Mac using a PHP file stored on my Raspberry Pi, I want to be able to either run the PHP file direct from a browser, or to use SiriProxy to run the PHP file. – JosephFTaylor Aug 05 '13 at 18:21
  • Still sudo: no tty present and no askpass program specified – JosephFTaylor Aug 05 '13 at 18:22
  • When I run 'echo $ssh->exec('sudo shutdown -h now');' – JosephFTaylor Aug 05 '13 at 18:23
  • Using 'echo $ssh->exec('ssh -l josephftaylor 192.168.1.14 "sudo shutdown -h now"');' Shows 'Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password).' in the browser. – JosephFTaylor Aug 05 '13 at 18:30
  • Using the exec() method will require certificate based authentication so that you don't have to enter the password to authenticate to the remote machine. `ssh-keygen` from your R-PI, then `ssh-copy-id`. This will generate a cert, and install it onto your MAC so that the actual `exec()` call to the `ssh` command won't require a password. The the sudo changes you made earlier should allow the shutdown command to execute as your user. – Travis Hegner Aug 05 '13 at 18:37
  • Okay, I ran 'ssh-keygen' on the Pi then 'ssh-copy-id -i rsa.pub_location josephftaylor@192.168.1.124'. Still doesn't work. I must be a major retard. – JosephFTaylor Aug 05 '13 at 18:49