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.

4 Answers 4

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

8
  • foo,bar,base are hostnames ?
    – MikePappa
    Commented Oct 17, 2017 at 16:17
  • Sure it is servers names... Commented Oct 17, 2017 at 16:20
  • will ip address work instead of hostname?
    – MikePappa
    Commented Oct 17, 2017 at 16:22
  • Yes, it will work, for sure... Commented Oct 17, 2017 at 16:24
  • 3
    Maybe you can try by yourself and start experimenting ?! Commented Oct 17, 2017 at 16:32
4

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
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .