-
Notifications
You must be signed in to change notification settings - Fork 0
/
webwol.py
114 lines (92 loc) · 2.69 KB
/
webwol.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
112
113
114
#!/usr/bin/env python
import sys
import os
import web
import subprocess
from wakeonlan import wol
abspath = os.path.dirname(__file__)
sys.path.append(abspath)
os.chdir(abspath)
import config
import woldb
urls = (
'/', 'Index',
'/add/?', 'Add',
'/add/(\d+)', 'Add',
'/edit/?', 'Edit',
'/edit/(\d+)?', 'Edit',
'/delete/(\d+)', 'Delete',
'/wol/?', 'Wol',
'/wol/(\d+)?', 'Wol',
'/ping/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', 'Ping'
)
render = web.template.render('templates/', cache=config.cache, base='base')
class Index:
def GET(self):
hosts = woldb.get_hosts()
return render.hosts(hosts)
class Add:
def GET(self):
return render.host_form('add', None)
def POST(self):
data = web.input(_method='post')
try:
name = data.name
mac = data.mac
if data.ip:
ip = data.ip
else:
ip = None
woldb.add_host(name, mac, ip)
except:
return render.error('Please check your input.')
return render.success('Host added to database.')
class Edit:
def GET(self, id=0):
if id > 0:
host = woldb.get_host(int(id))
return render.host_form('edit', host)
else:
hosts = woldb.get_hosts()
return render.edit_hosts(hosts)
def POST(self, id):
data = web.input(_method='post')
try:
woldb.update_host(id=id, name=data.name, mac=data.mac, ip=data.ip)
except:
return render.error('Please check your input.')
return render.success('Host was successfully edited.')
class Delete:
def GET(self, id):
woldb.delete_host(int(id))
raise web.seeother('/edit')
class Wol:
def POST(self, id=0):
id = int(id)
success = True
if id > 0:
host = woldb.get_host(id)
success = self.wakeup(host)
else:
hosts = woldb.get_hosts()
for host in hosts:
if not self.wakeup(host):
success = False
if success:
return render.success('Magic packet was sent.')
else:
return render.error('An error occured. Please check your \
webserver\'s logs.')
def wakeup(self, host):
success = True
try:
wol.send_magic_packet(host.mac, ip_address=host.ip)
except:
success = False
return success
class Ping:
def POST(self, ip):
pingable = subprocess.call('ping -W 3 -c 1 %s' % ip, shell=True)
return pingable
app = web.application(urls, globals(), autoreload=True)
application = app.wsgifunc()