Wednesday 13 July 2011

Shutdown a Windows machine from a Linux box

You can shutdown a windows box if you have samba installed.

Using:
net rpc SHUTDOWN -C "some comment here" -f -I x.x.x.x -U user_name%password
As long as the user you supplied has rights to shutdown the system it will work.

This bash script scans the network and turns off all systems that are left on over night.
#!/bin/bash
wks=(`nmap -sL --dns-servers 10.x.x.x,10.x.x.x 10.x.x.x/22, 10.x.x.x.x/23 grep FQDN|cut -d" " -f2 |grep -v -f serverlist`)
for (( i=0; i < "${#wks[@]}"; i++)); do
net rpc SHUTDOWN -C "This system was left on after hours and is being shutdown" -f -I "${wks[$i]}" -U user_name%password
done
Basically what the script does is scans the network(s) with nmap, pipes it though grep and cut to get the FQDN. Then "grep -v -f serverlist" is an exclude list of servers we don't want to shutdown. From there it puts the workstations into an array and turns off each system.

Possibly Related Posts

1 comment:

  1. Hey,

    Great post!
    I use this method for the company that I work for. However, your script is more sophisticated then mine! It's made me go back to the drawing board and mine and I am now in the process of re-writing it.
    Just wanted to add to that if you want, nmap has the option "--excludefile" which allows you to create a list of excluded machines/servers.
    You can also us the -iL option to do an include list (the exclude list takes priority).

    Thanks though for the for this bit of code, definitely helpful!

    ReplyDelete