Если у нас есть коллекция компьютеров Linux в локальной сети, как написать сценарий оболочки для отключения всех компьютеров

658
user331621

У меня есть лаборатория и в том, что коллекция компьютеров Linux подключена к локальной сети. Я хочу, как написать сценарий оболочки, чтобы закрыть все ПК, когда мы запускаем этот сценарий оболочки.

-1
Возможный дубликат [Можно ли отключить удаленные компьютеры без ввода пароля?] (Http://superuser.com/questions/615570/is-it-possible-to-shutdown-remote-computers-without-inputing-the- пароль) или [Как выключить компьютер с определенным IP-адресом?] (http://superuser.com/questions/57705/how-to-shut-down-a-computer-having-a-particular-ip- адрес) Вы также можете найти полезным вопрос [Выключение без пароля пользователя sudo в Ubuntu] (http://superuser.com/questions/92925/shutdown-without-sudo-user-password-in-ubuntu) в вашем задании. Breakthrough 9 лет назад 0

2 ответа на вопрос

1
Cristian Ciupitu

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@$ shutdown -h now done < inventory.txt 
Я вздрогнул, когда увидел `root @` не только по одной причине ... Breakthrough 9 лет назад 0
@ Прорыв, поэтому ты проголосовал за это? Cristian Ciupitu 9 лет назад 0
Тот факт, что OP может не иметь демона SSH, [пользователь root должен быть уже отключен через SSH] (http://superuser.com/questions/124763/have-a-bash-script-remotely-shutdown -another-computer-on-the-lan), это потребовало бы жесткого кодирования паролей `root`, если они были (или хранения ключей вместе), и [вам не нужен` root`, чтобы выключить машину вниз] (http://superuser.com/questions/92925/shutdown-without-sudo-user-password-in-ubuntu). Breakthrough 9 лет назад 0
@ Прорыв, если у ОП еще не настроен SSH или этот ответ ему не подходит, он может сказать это. Я также не понимаю, как вход в систему как пользователь root может стать причиной отрицательного голосования. Я понимаю, что это причина не для голосования, а для голосования. Cristian Ciupitu 9 лет назад 0
1
ben

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

Похожие вопросы