commit c35fc1a7dedf7828d6fccc3d95c91f4a3853e1e0 Author: Josh Holtrop Date: Sat Mar 5 08:46:43 2011 -0500 add CGI script and sample MAC list config file diff --git a/wol.macs b/wol.macs new file mode 100644 index 0000000..41c52e8 --- /dev/null +++ b/wol.macs @@ -0,0 +1 @@ +machine 11:22:33:44:55:66 diff --git a/wol.py b/wol.py new file mode 100755 index 0000000..7ba7116 --- /dev/null +++ b/wol.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +import re +import cgi +from subprocess import * + +# Enable these for debugging +import cgitb +cgitb.enable() + +MAC_FILE = '/etc/wol.macs' +REFRESH = 15 + +def header(): + print "Content-Type: text/html\n" + +def printHead(title): + print ''' + %s + +''' % (title, REFRESH) + +def checkup(hostname): + p = Popen(['ping', '-c1', '-W2', hostname], stdout = PIPE, stderr = PIPE) + for line in p.communicate()[0].split('\n'): + if re.search(r'\sbytes\sfrom\s', line): + return True + return False + +def getMACs(): + f = open(MAC_FILE, 'r') + macs = {} + for line in f.read().split('\n'): + m = re.match('(\S+)\s+(\S+)', line) + if m is not None: + name, mac = m.group(1, 2) + macs[name] = mac + return macs + +def main(): + header() + print '' + printHead('Wake on LAN Interface') + print '' + print '

%s

' % 'Wake on LAN Interface' + macs = getMACs() + form = cgi.FieldStorage() + if 'name' in form: + name = form['name'].value + if name in macs: + if 'wake' in form: + Popen(['wakeonlan', macs[name]], stdout = PIPE, stderr = PIPE) + elif 'shutdown' in form: + Popen(['ssh', '-i', '/var/www/.ssh/id_rsa', + '-l', 'root', name, 'shutdown', '-h', 'now'], + stdout = PIPE, stderr = PIPE) + print '' + print '' + +if __name__ == "__main__": + main()