PDA

View Full Version : Using Linux to kill Windows



Kasyx
01-04-2008, 09:46 AM
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.

Datura
01-04-2008, 09:47 AM
What mischief are you getting up to? Tsk tsk!!! :D

icyrus
01-04-2008, 09:49 AM
I've seen some python scripts that claim to be able to shutdown a remote windows server. YMMV though.

Kasyx
01-04-2008, 10:00 AM
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.

icyrus
01-04-2008, 10:03 AM
*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?

Kasyx
01-04-2008, 10:06 AM
Couldn't you just use the windows scheduler?

Well... Yeah...

But... Linux...

What I need is a "Sleep on LAN" magic packet :D

chiskop
01-04-2008, 10:06 AM
Couldn't you just use the windows scheduler?

But then how would you turn it on again?

Kasyx
01-04-2008, 10:08 AM
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.

ghoti
01-04-2008, 10:10 AM
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.

Kasyx
01-04-2008, 10:13 AM
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.

ghoti
01-04-2008, 10:19 AM
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.

Not to sure if this will help, and I know nothing about wake up on LAN or anything like that :D

http://www.timmatheson.com/2006/05/how-to-remote-shutdown-your-windows-xp-machine-with-a-text-message-and-outlook/

mancombseepgood
01-04-2008, 10:22 AM
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.

Kasyx
01-04-2008, 10:26 AM
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.

Datura
01-04-2008, 10:26 AM
Not to sure if this will help, and I know nothing about wake up on LAN or anything like that :D

http://www.timmatheson.com/2006/05/how-to-remote-shutdown-your-windows-xp-machine-with-a-text-message-and-outlook/

Snap :D:D

Just finished reading that and was about to post the link. I will be trying that out later. Very clever.

Nod
01-04-2008, 10:27 AM
http://www.linuxforums.org/forum/redhat-fedora-linux-help/60324-remote-shutdown-windows-linux-box.html

mancombseepgood
01-04-2008, 10:28 AM
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

Kasyx
01-04-2008, 10:53 AM
I like that PowerOff program. Basically lets one telnet into the server and give it commands from there.
http://users.pandora.be/jbosman/applications.html

Thanks for all the help guys :) Much appreciated.

mancombseepgood
01-04-2008, 10:57 AM
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

Roman4604
01-04-2008, 11:17 AM
Considering today's date ... my solution would include one of these (http://us.st11.yimg.com/us.st.yimg.com/I/yhst-78359274419338_1990_34087351http://us.st11.yimg.com/us.st.yimg.com/I/yhst-78359274419338_1990_34087351).

Note: precision aiming at the server's power switch will be essential.

Kasyx
01-04-2008, 11:17 AM
Don't worry, this is just phase 1 :D

*mwuahahahaha*

mancombseepgood
01-04-2008, 12:08 PM
Pffffft! :D

thisgeek
02-04-2008, 06:52 AM
http://freshmeat.net/projects/networkupstools/

Install nut on your linux box as above, it replaces apcupsd. nut has a windows client which can receive the notifications and initiate shutdown.

This is a more elegant solution than some of those (quite ingenious) suggestions proposed above. ;)

mancombseepgood
02-04-2008, 08:53 AM
Hey! :D

At least I can call it my solution :D:D

Kasyx
02-04-2008, 09:29 AM
Hmm...

Turns out one can use a SNMP trap when using apcupsd.

You can set it up to behave like an SNMP server and then have the Windows machine run an SNMP client which would receive power-down notifications from the server when the power goes out.

thisgeek
02-04-2008, 09:37 AM
Hmm. how difficult is it to configure windows to respond to that snmp notification?

Kasyx
02-04-2008, 09:45 AM
Hmm. how difficult is it to configure windows to respond to that snmp notification?

Not sure, but one could probably find a a decent third-party SNMP client app somewhere on the interwabz.

SoftDux-Rudi
03-04-2008, 12:29 AM
Here's a simpler, easier way of doing it: http://blog.softdux.com/everything-todo-with-linux/linux-howtos/how-to-shutdown-windows-via-the-network-from-linux.html

I have a Powercom 1000VA UPS, which is monitored by the SME 7.3 (CentOS4.6) Linux server, using the included upsmon script, and have added the commands

fskmh
03-04-2008, 12:53 AM
Seeing as you have a Linux server and Windows clients, nut (Network UPS tools) is probably the best solution.

http://mybroadband.co.za/photos/showphoto.php?photo=3196

nut ftw :D

SoftDux-Rudi
03-04-2008, 06:07 AM
that's if you could get the router to work with nut. I couldn't.

The_Librarian
03-04-2008, 07:34 AM
http://freshmeat.net/projects/networkupstools/

Install nut on your linux box as above, it replaces apcupsd. nut has a windows client which can receive the notifications and initiate shutdown.

This is a more elegant solution than some of those (quite ingenious) suggestions proposed above. ;)

Thanks!

Just to slightly derail this - any linux programs with which you can milk a PABX buffer box?

Thinking of putting up a small linux boxen in the server room which'll monitor the ups and milk the pabx, and when power goes, will notify the windoze servers...

*toddles off to do research*

mancombseepgood
03-04-2008, 10:32 AM
A regular tty shell with ability to save should do... it is serial right?

MarvinMoses
11-04-2008, 05:45 PM
Linux box: use nubuntu fire up netcat lockon to eskom loadshedding page if you can understand it.

Eskom: transfer this script across *.* alter the loadshedding time of the area the server is located in to shutdown when you want say 3.00pm then again script the time to bring electricity back to the area.

Server box: configure the server to restart after unshedualed shutdown. oh remove all admin passwords so machine boots into a secure desktop.

mancombseepgood
12-04-2008, 06:17 AM
Marvin ate too much chinese and is tripping on msg!!