Daily Archives: March 3, 2009

Remote Server Shutdown

One of my recent projects was to initiate a server-wide shutdown should our UPS ever run low.  I currently have Zenoss monitoring the health status of the UPS, including the remaining charge on the battery.  Using Zenoss thresholds, I can make a script execute if the battery ever runs low.

Our Zenoss deployment currently runs on CentOS, so I put down a method to shut down each type of host from a linux platform.  After much researching and testing, this is what I came up with (and am currently using in my shutdown script).

For Windows:

net rpc SHUTDOWN -C "Automated shutdown" -f -I "$server" -W $domain -U $username%$password

I replaced my arguments with some that would be more readable.  User and password are separated with %.

For Linux:

CMD="ssh -l $username $server shutdown -h now"
              expect -c "
              match_max 100000
              spawn $CMD
 
              expect {
                \"Are you sure you want to continue connecting (yes/no)?\" {
                send \"yes\r\"
                exp_continue
                }
                \"password:\" {
                send \"$password\r\"
                expect -re \"$username*\"
                }
              }
              "

I wanted to shutdown linux servers without using keys but had to overcome the “do you want to connect” prompt.  In order to do that I had to use exact which can be installed with “yum install exact”

By turning these into functions and passing arguments, you can make a shutdown script in a couple of minutes.