Using Linux to kill Windows

Kasyx

Expert Member
Joined
Jun 6, 2006
Messages
2,565
Reaction score
1
Location
127.0.0.1
Yeah, bit of a misleading subject line, but I wanted to get your attention :D

Basically, I am wondering if there is a way to use Linux (i.e. a bash script or crontab line) to send something like a KILL signal to a Windows Server 2003 box on the network, basically telling it to switch off, and then, later, using something like Wake on LAN to start it up again.

How plausible is this? I am asking Google as well, but I figured I would ask the Brain Trust as well.
 
I've seen some python scripts that claim to be able to shutdown a remote windows server. YMMV though.
 
What mischief are you getting up to? Tsk tsk!!! :D

*sigh* if only :D

I have a client asking about it. For some reason they like to switch off their Windows Server 2003 fileserver in the afternoons, and would like to know if I can automate this process with the immense power of my Linux.
 
*sigh* if only :D

I have a client asking about it. For some reason they like to switch off their Windows Server 2003 fileserver in the afternoons, and would like to know if I can automate this process with the immense power of my Linux.

Couldn't you just use the windows scheduler?
 
The other issue is that they have a UPS connected to the Linux server (but obviously also powering the Windows box), and they want the Linux box to be able to tell the Windows box to die when (note, when, not if) the power goes out.
 
The other issue is that they have a UPS connected to the Linux server (but obviously also powering the Windows box), and they want the Linux box to be able to tell the Windows box to die when (note, when, not if) the power goes out.

Cant you get a linux based ups that can pick up when the power has dropped? Lol, sounds like an interesting project.
 
Cant you get a linux based ups that can pick up when the power has dropped? Lol, sounds like an interesting project.

Yeah, obviously :D
Using apcupsd, however I need the Linux box to then tell the Windows box of said travesty, thus making it shut down.
 
Round about way to get it done... create a scheduled task on the windows box that runs a batch file stored locally every minute.

The batch file should run the windows shutdown command then delete itself. For the purpose of this example I'm calling the file sd.cmd and storing it in the root of c: drive. It should look like this...

shutdown -t xx -s
delete c:\sd.cmd

... where xx is the time in seconds before the shutdown occurs.
Setup your schedule to run this command (c:\sd.bat) every minute but don't enable it at first.
Rename the batch file something like sd.bak then enable the schedule. Ensure that the schedule is being run by a user with admin credentials (create a dummy admin account for this if you like). Since the file has been renamed, the shutdown won't occur until it is copied back to the original name. You then simply copy sd.bak to a new file called sd.cmd using a script from the linux box across a samba share to effect the shutdown (within the next minute when the schedule runs).

Note that the file deletes itself after running, producing a harmless 'batch file not found' error on the command line. If you have it running with other credentials, the user won't see the command window at all and will only get a message that the machine is shutting down.

To abort a shutdown, simply enter the following at the windows command prompt
shutdown -a

You prolly know most of this so excuse the unnecessary explanations... yeah, it's a redneck solution, but works :D

Maybe there is some other admin utility that allows that kind of command executioin from Linux, but I'm not sure...
Other solution is to use VNC and some sort of macro to execute a shutdown, but that sounds like too much pt.
 
Last edited:
Round about way to get it done... create a scheduled task on the windows box that runs a batch file stored locally every minute.

The batch file should run the windows shutdown command then delete itself. For the purpose of this example I'm calling the file sd.cmd and storing it in the root of c: drive. It should look like this...

shutdown -t xx -s
delete c:\sd.cmd

... where xx is the time in seconds before the shutdown occurs.
Setup your schedule to run this command (c:\sd.bat) every minute but don't enable it at first.
Rename the batch file something like sd.bak then enable the schedule. Ensure that the schedule is being run by a user with admin credentials (create a dummy admin account for this if you like). Since the file has been renamed, the shutdown won't occur until it is copied back to the original name - sd.cmd. You then simply copy the file to a new file called sd.cmd using a script from the linux box across a samba share to effect the shutdown (within the next minute when the schedule runs).

Note that the file deletes itself after running, producing a harmless 'batch file not found' error on the command line. If you have it running with other credentials, the user won't see the command window at all and will only get a message noting that the machine is shutting down.

To abort a shutdown, simply enter the following at the windows command prompt
shutdown -a

You prolly know most of this so excuse the unnecessary explanations... yeah, it's a redneck solution, but works :D

Maybe there is some other admin utility that allows that kind of command executioin from Linux, but I'm not sure...
Other solution is to use VNC and some sort of macro to execute a shutdown, but that sounds too much pt.

...

*head asplodes*

Dude...
Though laughing in Occam's Razor's face, that might just work...

Nice.
 
Another route would be to have your scheduled batch file on the windze box look for the presence of a dummy text file, if it exists, then delete the file and shut down the PC. You then copy this file to the windoze box from the linux box when you want to shut down... same thing, just a lil more work...

but... kudos to nod... I like this one:

#!/usr/bin/env python
# win32shutdown.py

import win32api
import win32con
import win32netcon
import win32security
import win32wnet


def shutdown(host=None, user=None, passwrd=None, msg=None, timeout=0, force=1,
reboot=0):
""" Shuts down a remote computer, requires NT-BASED OS. """

# Create an initial connection if a username & password is given.
connected = 0
if user and passwrd:
try:
win32wnet.WNetAddConnection2(win32netcon.RESOURCET YPE_ANY, None,
''.join([r'\\', host]), None, user,
passwrd)
# Don't fail on error, it might just work without the connection.
except:
pass
else:
connected = 1
# We need the remote shutdown or shutdown privileges.
p1 = win32security.LookupPrivilegeValue(host, win32con.SE_SHUTDOWN_NAME)
p2 = win32security.LookupPrivilegeValue(host,
win32con.SE_REMOTE_SHUTDOWN_NAME)
newstate = [(p1, win32con.SE_PRIVILEGE_ENABLED),
(p2, win32con.SE_PRIVILEGE_ENABLED)]
# Grab the token and adjust its privileges.
htoken = win32security.OpenProcessToken(win32api.GetCurrent Process(),
win32con.TOKEN_ALL_ACCESS)
win32security.AdjustTokenPrivileges(htoken, False, newstate)
win32api.InitiateSystemShutdown(host, msg, timeout, force, reboot)
# Release the previous connection.
if connected:
win32wnet.WNetCancelConnection2(''.join([r'\\', host]), 0, 0)


if __name__ == '__main__':
# Immediate shutdown.
shutdown('salespc1', 'admin', 'secret', None, 0)
# Delayed shutdown 30 secs.
shutdown('salespc1', 'admin', 'secret', 'Maintenance Shutdown', 30)
# Reboot
shutdown('salespc1', 'admin', 'secret', None, 0, reboot=1)
# Shutdown the local pc
shutdown(None, 'admin', 'secret', None, 0)
... but reserve my comments till you've tried it :D
 
Last edited:
Yeah, that does look pretty handy... thanks - this thread has been helpful :)
Just a little disappointing that it had nothing to do with Linux boxes executing a worldwide windoze DOS across the net at midnight or something... ahh well... next time :D
 
Last edited:
Considering today's date ... my solution would include one of these.

Note: precision aiming at the server's power switch will be essential.
 
Top
Sign up to the MyBroadband newsletter
X