9

The question is simple.
What would be the script I would have to use to shut down a computer in my network thru ssh.

Normaly i would go to command line and:

ssh desktop

delik@desktop's password: 

delik@desktop:~$ sudo shutdown -P 0

To power on I created a file and wrote:

wakeonlan xx:xx:xx:xx:xx:xx

And gave it the executable bit

That way to power on it requires only a double click. Would i be capable of doing the same to shutdown?

DeLiK
  • 372
  • 3
  • 4
  • 16

3 Answers3

12

For the following I am assuming that the user you are going to use in remote-host is the same you use in local-host.

In order to do what you want, you have to first authorize your local-host to connect to you remote-host with no password. To do that you have to (as described here):

  1. Install ssh:

    sudo apt-get install ssh
    
  2. Create public and private keys using ssh-key-gen on local-host by entering this command in your localhost:

    ssh-keygen
    

    You should save the generated key in:

    /home/yourusername/.ssh/id_rsa
    

    Press enter twice to leave the passphrase empty.

    Your identification has been saved in /home/yourusername/.ssh/id_rsa.
    Your public key has been saved in /home/yourusername/.ssh/id_rsa.pub.
    The key fingerprint is:
    XX:XX:XX:xX:XX:xX:XX:XX:XX:XX:XX:XX:XX:XX yourusername@local-host
    
  3. Copy the public key to the remote-host using ssh-copy-id:

    yourusername@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
    yourusername@remote-host's password:
    
    Now try logging into the machine, with:
    
    ssh remote-host
    

    and check in .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.

    Note: ssh-copy-id appends the keys to the remote-host’s /home/yourusername/.ssh/authorized_key.

  4. Login to remote-host without entering the password:

    ssh remote-host
    yourusername@remote-host:~$
    

    Access to remote-host with no password. Success!

Now you have to be able to execute sudo shutdown -P 0 with no password. You can do that by modifying /etc/sudoers on remote-host with visudo. That way, user yourusername can execute the shutdown command with no password asked.

  1. Login to the remote-host:

    ssh remote.host
    
  2. Run:

    sudo visudo
    

    By running visudo, you edit /etc/sudoers in a safe manner.

  3. Add this line to the file:

    yourusername ALL = NOPASSWD: /sbin/shutdown
    
  4. After doing that, get back to your local-host, create a new empty file and paste this line, modifying the remote-host's name:

    ssh remote.host sudo shutdown -P 0
    
  5. Save and close the file, right-click on it to go to its PropertiesPermissions, and tick Execute this file as a program.

Script done!

BeastOfCaerbannog
  • 12,964
  • 10
  • 49
  • 77
DeLiK
  • 372
  • 3
  • 4
  • 16
0

For me on my 18.04 server, the keygen and sudoers edits did not work on their own. Finally, after a big hassle, I managed to remote shutdown my server with the following command:

plink -batch -ssh -P 222 -t user@server -pw XXX "sudo -S shutdown"

I edited sudoers so that:

user ALL=(ALL) NOPASSWD:/sbin/shutdown
BeastOfCaerbannog
  • 12,964
  • 10
  • 49
  • 77
Jabin
  • 11
  • 2
-1

You can use an user-account with a keyfile instead of a password (so you don't have to type in the password to login). You can also give the user a "default shell" that is not bash or sh, but "shutmedown.sh" or something like this, which will exec your shutdown-code.

mene
  • 1
  • 1
  • Would you improve your answer with some examples on how to do that exactly? – DeLiK Sep 21 '12 at 09:09
  • You can check this out for the login part: http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/ You can use usermod -s /home/username/myscript.sh username to change the default shell. The script should be something like #!/bin/bash shutdown -P now – mene Sep 21 '12 at 09:17
  • You are right, the ssh login problem is solved, but now I have to build a script with a "pause" so it waits for `ssh desktop` to happen succesfully. And after that i have another problem how to give the command in `sudo` and enter the password? – DeLiK Sep 21 '12 at 09:52
  • I manage to do that. Now I can shut down with sudo shutdown -P 0 with no password – DeLiK Sep 21 '12 at 10:09
  • you can pause a script with the "sleep" command. "sleep 10" for 10 seconds. – mene Sep 21 '12 at 10:34
  • I already have the full condensed answer but i'm unable to post it here... need 10 rep.. – DeLiK Sep 21 '12 at 11:59
  • @mene, would you consider editing your answer to add the details you posted as comments? That way, your answer will be clear, complete and useful to other users. – Peachy Oct 02 '12 at 08:48