#!/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 ''
names = macs.keys()
names.sort()
for name in names:
up = checkup(name)
print '- '
print ''
print '
'
print '
'
print ''
if __name__ == "__main__":
main()