Automated Stopserver & Shutdown?

Home » CentOS » Automated Stopserver & Shutdown?
CentOS 8 Comments

Dear All In an environment , I have 20 CentOS servers running together . For shutting them down , I need to issue the followings on each of the servers :
#./stopServer
#init 0
This is cumbersome to try to issue these on huge amount of servers to keep them safely going shutdown. Can you please let me know how can I
automate it and say let just one server send stop processes and shutdown commands to the other ones and then goes shutdown himself?
Thank you

8 thoughts on - Automated Stopserver & Shutdown?

  • A simple script which loops though a text file that lists the hostname /
    username / password for the login and then runs the “shutdown – h now”
    command? Though, I would be uneasy with listing root passwords in a random text file.

  • Thank you for your help . So how to open a session with another server to login with root password and issue a command on it ?

  • Am 17.11.2014 um 06:56 schrieb Hadi Motamedi:

    Try out ansible.

    Using it you can automate many more tasks distributed over your server environment through very simple configurations. It is very flexible and powerful.

    Alexander

  • Thank you for your help. Is there any way to automate an SSH session on them say write a script to automatically SSH to them via root password and pass a command to them?

  • Greetings,

    Please google for passwordless SSH authentication (Which many list members most likely will disapprove)

    Request you to try something and post queries when it fails completely with error message etc.

    http://www.firedaemon.com/blog/passwordless-root-ssh-public-key-authentication-on-CentOS-6

    http://www.itzgeek.com/how-tos/linux/CentOS-how-tos/password-less-login-ssh-CentOS-6-rhel-6.html#axzz3JKqkl74g

    You have been posting queries in this in other lists often without doing your homework.

    and post a thanks if any solution works out for you as all the members here are voluntary contributors whi are sharing their valuable time and experience.

  • I do not believe that, on reflection, you really would want to do that. An alternative approach is to use ssh-keygen to create a key pair for root on the control host and then add that public key to the authorized_keys file inside
    /root/.ssh on each of the target hosts. So, something like this:

    On control host as root user:

    ssh-keygen -t rsa -b 4096
    Generating public/private rsa key pair. Enter file in which to save the key (/> home directory>/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /
    > home directory>/.ssh/id_rsa. Your public key has been saved in /> home directory>/.ssh/id_rsa.pub. The key fingerprint is:
    1c:6c:91:76:64:83:a5:32:4e:ac:df:28:ed:cb:9a:dd @

    That will give you the following files in /root/.ssh

    ll .ssh total 28
    -rw-r–r–. 1 root root 4619 Nov 17 2014 authorized_keys
    -rw——-. 1 root root 3239 Nov 17 2014 id_rsa
    -rw-r–r–. 1 root root 756 Nov 17 2014 id_rsa.pub

    Now login to each of your target hosts as root and transfer
    /root/.ssh/id_rsa.pub from the control host and append (>>) it to
    /root/.ssh/authorized_keys on the target host. If the .ssh directory does not already exist on the target host then you should first create the necessary files by running ssh-keygen on the target (otherwise you need consider whether or not selinux is enforced or not? is the mode set correctly? yada, yada).

    Once you have setup the target hosts for SSH and have added the control host’s public key to .ssh/authorized_keys then you should be able to SSH into each from the control host without having to provide a password.

    You will however need to add each remote host’s identity
    (/etc/ssh/ssh_host_rsa_key.pub) to your own /root/.ssh/known_hosts file the first time that you connect. This can be automatically created and maintained by the SSH client so long as the remote host’s SSH identity is not subsequently changed. If you have previously ssh’ed into the remote hosts than this will already have been done.

    Once the connections have been set up between the control host and all of the target hosts then you should be a able to do this (mind the -t option to ssh):

    for host in \
    long_host_name.domain.tld \
    targethost{01,02,03,04,05,06}.domain.tld \
    othername{x,y,z}.domain.tld \
    yet_another_very_long_host_name.domain.tld \
    192.168.0.2{1,5,7,8,9} ;
    do SSH -t $host ‘echo -e “\n\n$HOSTNAME\n” ; /root/stopServer ; init 0’;
    done

    Personally, I would use a separate account for this and add that account to the /etc/sudoers file on each of the targets:
    shutdown_userid NOPASSWD: localhost=/sbin/init 0
    shutdown_userid NOPASSWD: localhost=/sbin/shutdown -h now shutdown_userid NOPASSWD: localhost=/root/stopServer

    The ssh-keygen / authorized_hosts setup would then need to be done in that account’s home directory on each target rather than root’s. Then use ‘sudo
    /root/stopServer ; sudo init 0’ to run the shutdown scripts.

    I also would use ‘shutdown -h now’ instead of ‘init 0’