-
Notifications
You must be signed in to change notification settings - Fork 17
/
swhttpd.py
executable file
·86 lines (74 loc) · 2.57 KB
/
swhttpd.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
#!/usr/bin/env python3
import sys, logging
import redis
import syslog
import socket
import re
import tempfile
import http.server
import socketserver
import time
import config
def log(*args):
print(time.strftime("%Y-%m-%d %H:%M:%S") + ':', ' '.join(args))
syslog.syslog(syslog.LOG_INFO, ' '.join(args))
def error(*args):
print(time.strftime("%Y-%m-%d %H:%M:%S") + ': ERROR:', ' '.join(args))
syslog.syslog(syslog.LOG_ERR, ' '.join(args))
class swbootHttpHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
db = redis.Redis()
switch = db.get(self.client_address[0]).decode()
model = db.get('client-{}'.format(self.client_address[0])).decode()
if switch == None or model == None:
log("Switch not found:", self.client_address[0])
self.send_error(404, "File not found")
return None
if self.path == "/juniper-confg":
log("Generating Juniper config for",
self.client_address[0], "name =", switch)
f = tempfile.TemporaryFile()
f.write(config.generate(switch, model).encode())
content_length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "application/octet-stream")
self.send_header("Content-Length", content_length)
self.end_headers()
self.copyfile(f, self.wfile)
log("Config sent to", self.client_address[0], "name =", switch)
f.close()
return
elif self.path == "/juniper.tgz":
log("Sending JunOS file", config.models[model]['image'], "to",
self.client_address[0], "name =", switch)
if (model in config.models) and ('image' in config.models[model]):
# All good! Overwrite the requested file path and send our own.
self.path = config.models[model]['image']
f = self.send_head()
if f:
self.copyfile(f, self.wfile)
log("Sent JunOS to", self.client_address[0], "name =", switch)
f.close()
else:
log("Unknown file:", self.path)
self.send_error(404, "File not found")
# We write our own logs.
def log_request(self, code='-', size='-'):
pass
def log_error(self, format, *args):
pass
class swbootTCPServer(socketserver.ForkingTCPServer):
def server_bind(self):
self.socket.bind(self.server_address)
log("swhttpd started")
try:
httpd = swbootTCPServer(("", 80), swbootHttpHandler)
httpd.serve_forever()
except socket.error as err:
sys.stderr.write("Socket error: %s\n" % str(err))
sys.exit(1)
except KeyboardInterrupt:
sys.stderr.write("\n")
except Exception as err:
sys.stderr.write("Something went wrong: %s\n" % err)