Control All Computers in a Lab from a Single System

Quoting Dhandeep, our super-cool lab-admin:

now , all 70 systems in the lab can be switched on and switched off by single commands from the hostel…

Yes, that and a lot more is possible in our Software Systems Lab now. How? Read on…

The Setup

We have over 70 systems with Ubuntu 10.04 LTS installed on them. There is an administrative account (let’s call it admin for this post) and a guest (limited privilege) account on each. Needless to say, admin password is known only to admins and guest password is known to all who use the lab. All these systems are configured to be able to controlled remotely (read: OpenSSH server is installed on each).

Basic Idea

  1. Log in via SSH without a password

  2. Write your desired command and run it in background

  3. Run the above in a loop for the lab’s subnet.

Detailed Steps

See Tips for Remote Unix Work (SSH, screen, and VNC) for the first step (and for more immensely useful tips on remote usage of *NIX systems).

For Step 2, here is one example command:

ssh -t admin@labsystem "echo  | sudo -S shutdown -h now" &

In the above command labsystem is usually replaced with an IP address like 192.168.xxx.xxx and the with the password of the admin account.

WARNING: it’s not suggested to use the above command out in the open to save the password from prying eyes; also note that for additional security, you need to take a measure to make sure this is not saved in bash history or if the command is in a script, it’s not accessible to others.

The requirement of ampersand at the end depends on particular usage (if you want to run, let’s say,  uptime command over ssh, you would not want the output to go to background, or you can redirect the output to some file). Putting the process in background, in this case, will help in the next step.

The -S switch for sudo makes it possible to supply the password via stdin (we had discovered this switch from sudo’s man page, but didn’t manage to conclude “echo pass |” will do the trick until we discovered it at StackOverflow)

Step 3: use your favorite scripting language (bash, python, etc.) and run the above command for all the systems of your lab subnet. An example in bash:

for ip in {101..180}
do
	ssh -t admin@192.168.xxx.$ip "echo  | sudo -S shutdown -h now" &
done

The above code snippet will run the desired command for all systems in subnet within the IP range 192.168.xxx.101 to 192.168.xxx.180. Now, you can clearly see how putting the process in the background will help - the next iteration of the loop need not wait for the command in previous iteration to finish!

In the passing, here’s a small video I shot featuring Dhandeep when he got all excited to see this working:

That’s it. Try this out, share your tricks and have some *NIX fun in your lab. :-)

PS: I have not covered how systems can be switched on with this setup. It basically involves broadcasting a magic packet to the subnet. Hope Dhandeep comes up with a blog post on that soon. ;-) Here it is: On the push of a button..

Ciao

Kartik

Posted in post with tags commands friends gnu/linux hacker Informative innovation jugaad Linux NIT Calicut programming terminal ubuntu