Welcome, guest | Sign In | My Account | Store | Cart

Shuts down or reboots a remote computer.

Python, 51 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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.RESOURCETYPE_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.GetCurrentProcess(),
                                           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)

Would be helpful if you were automating installs overnight and need computers to shut down or reboot remotely.

7 comments

Joe Grossberg 16 years, 11 months ago  # | flag

think it through. If you shut down the box remotely (as opposed to restarting it), who is going to hit the power button and turn it back on for you?

Unless you're totally decommissioning a machine, this sounds like a bad idea.

Fadly Tabrani (author) 16 years, 11 months ago  # | flag

Think it through, again... Hi Joe, I couldn't agree with you more but any smart administrator wouldn't run it against critical machines if that's what you meant. There are scenarios when u need this, if you actually thought it through. One example is the management of desktop machines. I manage a few hundred of them and it is company's policy to switch off ya machines over the weekend. We didn't have that policy before the blaster worm came and the desktops that were left running broke the network including the critical servers. And if that doesn't get you thinking, somebody actually asked me before if it could be done. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/358449.

Anyway, if you actually read the code, it has options for rebooting. Cheers.

Jason Whitlark 16 years, 11 months ago  # | flag

Wake On Lan is the other side of this. See: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/358449

(Clarification of previous comment)

Eric Wong 16 years, 11 months ago  # | flag

Doesn't power off. I have 2 issues when using this script:

1) Computer doesn't power off like a regular shutdown does. It stays on the "Safe to power off" screen and I have to push the power button to power off.

2) After using this script to shutdown a computer and power off by pushing power button, I can't wake it up using wake-on-lan. I have to physically push the power button.

Anyone know why these things happen with this script? Thanks.

Clark Updike 16 years, 7 months ago  # | flag

Worked great! I use MS Remote Desktop and have had problems with the remote machine failing to accept a connection. I started a ping with

ping -t <hostname>

and ran the script in restart mode, and watched as the machine restarted (ping started timing out) and as it came back online (ping started succeeding). Saves me a trip to work to physically restart the machine. Thanks!

Anne Gunn 16 years, 2 months ago  # | flag

Thank you -- very helpful. I'm working on client/server software that runs, on the client side, while the machine boots. I wanted to verify that I didn't have resource leaks on the server side. Within minutes of finding your code, I was able to modify it by putting the reboot in a loop (and adding a test user id to the client), and ran the test overnight, with my server rebooting the client every ten minutes. What a productivity boost!

morgan Goose 14 years, 5 months ago  # | flag

Check for Users. I wonder If there is a way to check first to see if a user is signed on. In some cases you could have say a 24 hour lab, and not want to shut down a machine someone is doing work on.