Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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"
share|improve this question
    
Is it possible for you to put your id_rsa.pub into each host's .ssh/authorized_keys? –  ott-- Jul 4 '13 at 15:00
    
Instead of copying and pasting the the same command, you can also use a for host in ... or a while read host ... < hosts.txt loop. –  Cristian Ciupitu Jun 9 at 4:24
add comment

2 Answers 2

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"
share|improve this answer
2  
Note that passing the password in this manner is a security risk. Just make sure you are aware of the implications. :) –  ChrisInEdmonton Jul 4 '13 at 14:53
add comment

To avoid password entering use ssh-keygen for authentication key creation and upload public key to all your servers. BTW, terdon right, if you root do not use sudo.

ssh root@192.168.1.11 poweroff
ssh root@192.168.1.12 poweroff
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.