Made script executable
[Wake-On-Lan-Python.git] / wol.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
#
# Based on wol.py from http://code.activestate.com/recipes/358449-wake-on-lan/
# Amended to use configuration file and hostnames
# 
# Copyright (C) Fadly Tabrani, B Tasker
#
# Released under the PSF License See http://docs.python.org/2/license.html
#
#
 
 
import socket
import struct
import os
import sys
import ConfigParser
 
 
myconfig = {}
 
 
def wake_on_lan(host):
    """ Switches on remote computers using WOL. """
    global myconfig
 
    try:
      macaddress = myconfig[host]['mac']
 
    except:
      return False
 
    # Check macaddress format and try to compensate.
    if len(macaddress) == 12:
        pass
    elif len(macaddress) == 12 + 5:
        sep = macaddress[2]
        macaddress = macaddress.replace(sep, '')
    else:
        raise ValueError('Incorrect MAC address format')
 
    # Pad the synchronization stream.
    data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
    send_data = '' 
 
    # Split up the hex values and pack.
    for i in range(0, len(data), 2):
        send_data = ''.join([send_data,
                             struct.pack('B', int(data[i: i + 2], 16))])
 
    # Broadcast it to the LAN.
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sock.sendto(send_data, (myconfig['General']['broadcast'], 7))
    return True
 
 
def loadConfig():
        """ Read in the Configuration file to get CDN specific settings
 
        """
        global mydir
        global myconfig
        Config = ConfigParser.ConfigParser()
        Config.read(mydir+"/.wol_config.ini")
        sections = Config.sections()
        dict1 = {}
        for section in sections:
                options = Config.options(section)
 
                sectkey = section
                myconfig[sectkey] = {}
 
 
                for option in options:
                        myconfig[sectkey][option] = Config.get(section,option)
 
 
        return myconfig # Useful for testing
 
def usage():
        print 'Usage: wol.py [hostname]'
 
 
 
if __name__ == '__main__':
    mydir = os.path.dirname(os.path.abspath(__file__))
    conf = loadConfig()
 
    try:
    # Use macaddresses with any seperators.
 
        if sys.argv[1] == 'list':
                print 'Configured Hosts:'
                for i in conf:
                        if i != 'General':
                                print '\t',i
                print '\n'
        else:
 
                if not wake_on_lan(sys.argv[1]):
                        print 'Invalid Hostname specified'
                else:
                        print 'Magic packet should be winging its way'
    except:
        usage()