Linux – Is it possible to shutdown remote Computers without inputing the password

linuxshell

I used the following bash to shutdown remote computers,but for each computer,I must input the password for it.Is it possible to input the password Automatically?

#!/bin/bash

ssh -t root@192.168.1.11 "sudo poweroff"
ssh -t root@192.168.1.12 "sudo poweroff"
ssh -t root@192.168.1.13 "sudo poweroff"
ssh -t root@192.168.1.14 "sudo poweroff"
ssh -t root@192.168.1.15 "sudo poweroff"
ssh -t root@192.168.1.16 "sudo poweroff"
ssh -t root@192.168.1.17 "sudo poweroff"
ssh -t root@192.168.1.18 "sudo poweroff"
ssh -t root@192.168.1.19 "sudo poweroff"
ssh -t root@192.168.1.20 "sudo poweroff"
ssh -t root@192.168.1.21 "sudo poweroff"
ssh -t root@192.168.1.22 "sudo poweroff"
ssh -t root@192.168.1.23 "sudo poweroff"
ssh -t root@192.168.1.24 "sudo poweroff"
ssh -t root@192.168.1.25 "sudo poweroff"
ssh -t root@192.168.1.26 "sudo poweroff"

Best Answer

You don't need to. Since you are logging on as root there is absolutely no reason to use sudo, just run poweroff or (better) shutdown -h directly.

If for some reason not explained in your question you really do need sudo, use sudo's -S option. From man sudo:

   -S          The -S (stdin) option causes sudo to read
               the password from the standard input
               instead of the terminal device.  The
               password must be followed by a newline
               character.

For example:

#!/bin/bash

ssh -t root@192.168.1.11 "echo PASSWORD1 | sudo -S poweroff"
ssh -t root@192.168.1.12 "echo PASSWORD2 | sudo -S poweroff"