-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathztp_tftp.py
65 lines (44 loc) · 1.37 KB
/
ztp_tftp.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
from fbtftp.base_handler import BaseHandler
from fbtftp.base_server import BaseServer
from app.broker import trigger_job
import os
LISTEN_ON = '0.0.0.0'
SERVER_PORT = 69
TFTP_ROOT = '/opt/ztp/tftproot'
RETRIES = 3
TIMEOUT = 5
class TftpData:
def __init__(self, filename):
path = os.path.join(TFTP_ROOT, filename)
self._size = os.stat(path).st_size
self._reader = open(path, 'rb')
def read(self, data):
return self._reader.read(data)
def size(self):
return self._size
def close(self):
self._reader.close()
class StaticHandler(BaseHandler):
def get_response_data(self):
return TftpData(self._path)
class TftpServer(BaseServer):
def get_handler(self, server_addr, peer, path, options):
return StaticHandler(
server_addr, peer, path, options, session_stats)
def session_stats(stats):
print('')
print('#' * 60)
print('Peer: {} UDP/{}'.format(stats.peer[0], stats.peer[1]))
print('File: {}'.format(stats.file_path))
print('Sent Packets: {}'.format(stats.packets_sent))
print('#' * 60)
if stats.packets_sent > 0:
trigger_job(stats.peer[0], stats.file_path)
def main():
server = TftpServer(LISTEN_ON, SERVER_PORT, RETRIES, TIMEOUT)
try:
server.run()
except KeyboardInterrupt:
server.close()
if __name__ == '__main__':
main()