This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
forked from ffnord/mesh-announce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannounce.py
executable file
·185 lines (147 loc) · 4.45 KB
/
announce.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
import json
import argparse
import codecs
import os
import socket
import subprocess
import re
import netifaces as netif
from cpuinfo import cpuinfo
# Force encoding to UTF-8
import locale # Ensures that subsequent open()s
locale.getpreferredencoding = lambda _=None: 'UTF-8' # are UTF-8 encoded.
import sys
#sys.stdin = open('/dev/stdin', 'r')
#sys.stdout = open('/dev/stdout', 'w')
#sys.stderr = open('/dev/stderr', 'w')
# Utility functions for the announce.d files
def toUTF8(line):
return line.decode("utf-8")
def call(cmdnargs):
output = subprocess.check_output(cmdnargs)
lines = output.splitlines()
lines = [toUTF8(line) for line in lines]
return lines
# Local used functions
def setValue(node,path,value):
''' Sets a value inside a complex data dictionary.
The path Array must have at least one element.
'''
key = path[0]
if len(path) == 1:
node[key] = value;
elif key in node:
setValue(node[key],path[1:],value)
else:
node[path[0]] = {}
setValue(node[key],path[1:],value)
def gateway(batadv_dev):
output = subprocess.check_output(["batctl","-m",batadv_dev,"gwl","-n"])
output_utf8 = output.decode("utf-8")
lines = output_utf8.splitlines()
gw = None
for line in lines:
gw_line = re.match(r"^=> +([0-9a-f:]+) ", line)
if gw_line:
gw = gw_line.group(1)
return gw
def clients(batadv_dev):
output = subprocess.check_output(["batctl","-m",batadv_dev,"tl","-n"])
output_utf8 = output.decode("utf-8")
lines = output_utf8.splitlines()
count = 0
for line in lines:
client_line = re.match(r"^\s\*\s[0-9a-f:]+\s+-\d\s\[[W\.]+\]", line)
if client_line:
count += 1
return count
def addresses(bridge_dev):
ip_addrs = netif.ifaddresses(bridge_dev)
ip_list = []
try:
for ip6 in netif.ifaddresses(bridge_dev)[netif.AF_INET6]:
raw6 = ip6['addr'].split('%')
ip_list.append(raw6[0])
except:
pass
return ip_list
def mac_mesh(fastd_dev,meshmode=False):
interface = netif.ifaddresses(fastd_dev)
mesh = []
mac = None
try:
mac = interface[netif.AF_LINK]
mesh.append(mac[0]['addr'])
except:
KeyError
if meshmode:
return mesh
else:
return mac[0]['addr']
def mesh_interfaces(batadv_dev):
output = subprocess.check_output(["batctl","-m",batadv_dev,"if"])
output_utf8 = output.decode("utf-8")
lines = output_utf8.splitlines()
mesh = []
for line in lines:
dev_line = re.match(r"^([^:]*)", line)
interface = netif.ifaddresses(dev_line.group(0))
mac = interface[netif.AF_LINK]
mesh.append(mac[0]['addr'])
return mesh
def bat0_mesh(batadv_dev):
output = subprocess.check_output(["batctl","-m",batadv_dev,"if"])
output_utf8 = output.decode("utf-8")
lines = output_utf8.splitlines()
mesh = {"tunnel" : [], "other" : []}
for line in lines:
dev_line = re.match(r"^([^:]*)", line)
nif = dev_line.group(0)
interface = netif.ifaddresses(nif)
mac = interface[netif.AF_LINK]
if 'mesh' in config['network'] and nif in config['network']['mesh']:
mesh["other"].append(mac[0]['addr'])
else:
mesh["tunnel"].append(mac[0]['addr'])
return mesh
def node_id(dev):
if 'node_id' in config:
return config['node_id']
else:
return mac_mesh(dev).replace(':','')
def hostname():
if 'hostname' in config:
return config['hostname']
else:
return socket.gethostname()
def cpu_info():
info = cpuinfo.get_cpu_info()
return info['brand']
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--directory', action='store',
help='structure directory',required=True)
#parser.add_argument('-c', '--cfg', action='store',
# help='config file',required=True)
args = parser.parse_args()
options = vars(args)
try:
with open("config.json", 'r') as cfg_handle:
config = json.load(cfg_handle)
except IOError:
raise
batadv_dev = config['network']['batman']
fastd_dev = config['network']['fastd']
bridge_dev = config['network']['bridge']
data = {}
for dirname, dirnames, filenames in os.walk(options['directory']):
for filename in filenames:
if filename[0] != '.':
relPath = os.path.relpath(dirname + os.sep + filename,options['directory']);
fh = open(dirname + os.sep + filename,'r', errors='replace')
source = fh.read()
fh.close()
value = eval(source)
setValue(data,relPath.rsplit(os.sep),value)
#print(json.dumps(data, sort_keys=True, indent=4))
print(json.dumps(data))