How to shutdown all linux clients from server in your network (LAN)

This is the sample LAN. Now i am going to configure the server and 3 clients (for shutting down 3 clients from server) .Using Secure Shell (SSH) , we can execute any terminal commands like shutdown ...
and halt on the remote system. but we need to enter the password for connecting remote system. By configuring public key both server and client , we can easily logon to the remote system without using password and then we can execute the shutdown command

Consider this scenario,


Server Side (192.168.3.100)
1) Generate public key

root@myserver#ssh-keygen -t dsa -f .ssh/id_dsa
Generating public/private dsa key pair.
.ssh/id_dsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_dsa.
Your public key has been saved in .ssh/id_dsa.pub.
The key fingerprint is:
a6:3e:4d:48:af:73:04:d0:53:82:7d:ba:98:b6:fb:dd root@myserver

(Leave passphrase – Simply press enter key]
2) copy the server (192.168.3.100) public key to remote system (192.168.3.101)
root@myserver#cd .ssh

root@myserver#scp id_dsa.pub root@192.168.3.101:~/.ssh/id_dsa.pub

The authenticity of host ’192.168.3.101 (192.168.3.101)’ can’t be established.
RSA key fingerprint is 5c:5b:84:54:a9:95:6b:64:85:74:9b:cc:ce:60:ed:1d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ’192.168.3.101′ (RSA) to the list of known hosts.
root@192.168.3.101′s password:
id_dsa.pub 100% 603 0.6KB/s 00:00
root@myserver#

3) login to remote host(192.168.3.101) from local (192.168.3.100)
root@myserver#ssh -X root@192.168.3.101
root@192.168.3.101′s password:
Last login: Fri Oct 5 11:51:37 2007
Starting MySQL: [ OK ]
Starting httpd:
[root@client1 ~]#

4) In remote system append the public key to authorized_keys file
[root@client1 ~]# cd .ssh

[root@client1 .ssh]# cat id_dsa.pub >> authorized_keys
[root@client1 .ssh]# chmod 640 authorized_keys
[root@client1 .ssh]# rm id_dsa.pub

rm: remove regular file `id_dsa.pub’? y
[root@client1 .ssh]#exit

5) Now you can logon to the remote (192.168.3.101) system without password from your server (192.168.3.100)
6) Execute the shutdown command from server (for shutdown the client)
root@myserver#ssh -X 192.168.3.101 halt
The same way you can configure the client 2, and client 3 systems .
If the network has more number of systems , then you can use the following shell script in server for shutting down all client machines after configuring the public key on all machines
[root@myserver ~]# vi ssh-shutdown.sh

for (( $i=100 ; $i<=250 ; $i++ ))
do
ssh -X root@192.168.3.$i halt
done

(This program will shutdown the all network client system from 100 to 250)

No comments:

Post a Comment