forked from KusakabeShi/babeld-ibgp-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_cost.py
52 lines (47 loc) · 1.48 KB
/
update_cost.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
#!/usr/bin/python3
import jinja2
import socket
import os
import time
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect("/var/run/babeld/ro.sock")
def recvall(sock):
BUFF_SIZE = 128
sock.settimeout(1)
data = b''
waitmore = 0
while True:
part = sock.recv(BUFF_SIZE)
data += part
#print(part.decode("utf8") ,end = "")
if data[-3:].decode("utf8") == "ok\n":
#print("recvall done")
return data
if len(part) < BUFF_SIZE:
#print(f"partsize={len(part)} wait more")
if len(part) == 0:
waitmore += 1
if waitmore > 5:
return ""
return ""
sockret = recvall(sock).decode("utf8")
sock.sendall(b"dump\n")
sockret = recvall(sock).decode("utf8")
sock.sendall(b"quit\n")
#print(sockret)
datas = {}
neighbors = []
for line in sockret.split("\n"):
if "installed yes" in line:
elem = line.split(" ")[1:]
data = {}
for k,v in zip(elem[::2], elem[1::2]):
data[k] = v
datakey = data["prefix"].split("/")[0].replace(":","_").replace(".","_")
datas[datakey] = data["metric"]
neighbors += [{"prefix":data["prefix"] , "metric": data["metric"]}]
print( {"prefix":data["prefix"] , "metric": data["metric"]} )
#print(neighbors)
ibgptemplate = jinja2.Template(open('bird/igp_metric.conf.j2').read())
result=ibgptemplate.render(neighbors = neighbors)
open("bird/igp_metric.conf" , "w").write(result)