Shell script to shutdown all computers in a network

| | 1 min read

The following is the shell script that will shutdown all computers in the network. This script will be really help full for system administrators who need not want to check all systems, whether all the systems in the office has been shutdown properly when the employees leave the office. The script will check all the active ips in the network and write the those ips to a file. It also login to each system and execute the shutdown command. The things that are to be done before running the script is to 'Make sure your public-key has been added in the authorized_keys file in .ssh folder of root'. This allows the script to login to each system by the method of public-key authentication rather than prompting for password. And also the 'StrictHostKeyChecking no' will avoid all prompts during ssh.

  

#!/bin/bash

####Script to find all active ips in the network and shutdown all the systems by logging into it.

nmap -sP 192.168.1.0/24 | grep 192 | cut -d " " -f 5 | sed '1d' | sed '$d' > activeips.txt

while read activeips
do
  echo "$activeips"
  ssh -o "StrictHostKeyChecking no" root@$activeips 'shutdown -h now'
done < activeips.txt