-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmogrifier.py
81 lines (62 loc) · 2.43 KB
/
transmogrifier.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
#!/usr/bin/env python3.2
import re
import sys
import argparse
import subprocess
class LibAraLogFileParser:
def __init__(self):
self.nodes = { "ff:ff:ff:ff:ff:ff" : "broadcast" }
self.binary = '/usr/local/bin/node'
def transform(self, input, output):
pattern = '([a-fA-F0-9]{2}[:]?){6}'
regular_expression = re.compile(pattern)
with open(input, "r") as input_file:
with open(output, "w") as output_file:
for line in input_file:
match = regular_expression.finditer(line)
modified_line = line
if match:
for mac in match:
hostname = self._get_node(line[mac.start() : mac.end()])
# let's replace the mac addresses with the hostnames
modified_line = modified_line.replace(line[mac.start() : mac.end()], hostname, 1)
output_file.write(modified_line)
def _get_node(self, mac):
if mac not in self.nodes:
hostname = self._query_mac(mac)
if hostname == '':
# the mac address belongs (maybe) to an tap interface
temp_mac = mac.replace(mac[0:2],"50",1)
hostname = self._query_mac(temp_mac)
# check if again no hostname was found
if hostname == '':
print("unknown mac address: " + mac)
# we simply store the mac address as hostname
hostname = mac
else:
hostname += '.[tap]'
else:
hostname += '.[wifi]'
hostname = hostname.replace('\n', '')
self.nodes[mac] = hostname
return self.nodes[mac]
def _query_mac(self, mac):
pipe = subprocess.Popen([self.binary, 'list', '-m', mac], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return pipe.communicate()[0].decode('utf-8')
def main():
parser = argparse.ArgumentParser(description="transmogrifier - a script for transforming libARA (testbed) logs")
parser.add_argument('-i', '--input', dest='input', type=str, default='', action='store', help='input file to be transformed')
parser.add_argument('-o', '--output', dest='output', type=str, default='', action='store', help='output file for transformation')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
arguments = parser.parse_args()
if arguments.input != '' or arguments.output != '':
log = LibAraLogFileParser()
log.transform(arguments.input, arguments.output)
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
main()