-1

I have lab and in that ,collection of Linux computer are connected in LAN. i want how to writ e shell script to shut down all PC when we run that shell script.

Cristian Ciupitu
  • 5,513
  • 2
  • 37
  • 47
  • Possible duplicate of [Is it possible to shutdown remote Computers without inputing the password?](http://superuser.com/questions/615570/is-it-possible-to-shutdown-remote-computers-without-inputing-the-password) or [How to shut down a computer having a particular IP address?](http://superuser.com/questions/57705/how-to-shut-down-a-computer-having-a-particular-ip-address) You may also find the question [Shutdown without sudo user password in ubuntu](http://superuser.com/questions/92925/shutdown-without-sudo-user-password-in-ubuntu) useful in your quest. – Breakthrough Jun 09 '14 at 03:39

2 Answers2

1

Put the hostnames or IP addresses in a file named inventory.txt like this:

computer1
computer2
192.168.1.1

Use this script:

while read host; do
    ssh root@${host} shutdown -h now
done < inventory.txt
Cristian Ciupitu
  • 5,513
  • 2
  • 37
  • 47
  • I shuddered when I saw `root@` for many more than just one reason... – Breakthrough Jun 09 '14 at 03:41
  • @Breakthrough, is that why you voted it down? – Cristian Ciupitu Jun 09 '14 at 03:41
  • That, the fact that OP might not have an SSH daemon, [the `root` user should be disabled over SSH already](http://superuser.com/questions/124763/have-a-bash-script-remotely-shutdown-another-computer-on-the-lan), this would require hard-coding the `root` passwords if there were any (or keeping the keys all together), and [you don't need `root` to shut the machine down](http://superuser.com/questions/92925/shutdown-without-sudo-user-password-in-ubuntu). – Breakthrough Jun 09 '14 at 03:43
  • @Breakthrough, if the OP doesn't have SSH already set up or this answer doesn't suit him, he's free to say so. I also don't see how logging in as root can be a reason for down voting. I can understand it being a reason for not up voting, but not down voting. – Cristian Ciupitu Jun 09 '14 at 03:56
1

I apologize I am not allowed to add a comment (not high enough rep?) so unfortunately have to post this as an answer

I'm curious too why answer was voted down too. There are a lot of merits in what @Cristian is answering and I agree that if OP doesn't have SSH daemon he would either state he needs a workaround OR he would realize maybe he needs it installed. Considering most of the answers for this type of thing would use SSH daemon I think it's a good answer.

That being said, I also agree that you should use root to do this and use ssh key authentication. That's the best and most secure answer. Of course you do not use a password in an ssh script - harder to do (probably would need to use expect) and less secure.

I don't know about using a non-root user to do a poweroff/reboot. Using the link you sent, you realize that the sudo user is configured to have no password and then poweroff the machine. That means any user that knows the username can sudo to that user and poweroff. Is that what you really want?

Apologies again that this is posted as a separate answer - I want to add it as a comment to @Cristian's answer as his answer is one I would vote for

ben
  • 166
  • 5