4

I have more than 10 Linux machines. How may I power off all machines using a single script?

The password and user is the same for all of the machines.

Kusalananda
  • 333,661

4 Answers4

5

Try to use ansible.

Install ansible:

apt-get install ansible

Add your hosts to hosts file:

vim /etc/ansible/hosts
server1
server2
server3

Generate ssh key and add it on remote servers:

ssh-keygen
cat /etc/ansible/hosts | xargs -i ssh-copy-id {}

Run shutdown on servers:

ansible all -m shell -a "shutdown -h now"

You can check hosts availability by command before and after shutdown:

ansible all -m ping

Source 1, Source 2

Quarind
  • 269
4

There's many mays to do it.

One option is to use ssh key pairs instead of passwords to ssh without prompting for password. Then, you can do this :

#!/bin/bash

for server; do ssh $server 'halt; exit'; done

Usage:

./script.bash server1 server2 1.2.3.4

Or you can use a better approach with a tool like or pssh

1

Setup an ssh key for the user “shutdown”. If you look at /etc/passwd, this user’s default shell is /sbin/shutdown. So just logging in will execute the shutdown command.

0

I use fabric (http://www.fabfile.org/). You'd write a python script and can then run it on remote systems. It's quick and works pretty well for me for remote system administration.

L.Ray
  • 469