From 05bf2c8be209e619f8c9e727fd1353198b042642 Mon Sep 17 00:00:00 2001 From: makefu Date: Wed, 9 Sep 2015 14:41:38 +0200 Subject: begin packaging of tinc_stats --- .../scripts/adv_graphgen/tinc_graphs/Supernodes.py | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py (limited to 'retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py') diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py new file mode 100755 index 00000000..c8514c66 --- /dev/null +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 + +def find_potential_super(path="/etc/tinc/retiolum/hosts"): + import os + import re + + needle_addr = re.compile("Address\s*=\s*(.*)") + needle_port = re.compile("Port\s*=\s*(.*)") + for f in os.listdir(path): + with open(path+"/"+f) as of: + addrs = [] + port = "655" + + for line in of.readlines(): + + addr_found = needle_addr.match(line) + if addr_found: + addrs.append(addr_found.group(1)) + + port_found = needle_port.match(line) + if port_found: + port = port_found.group(1) + + if addrs : yield (f ,[(addr ,int(port)) for addr in addrs]) + + +def try_connect(addr): + try: + from socket import socket,AF_INET,SOCK_STREAM + s = socket(AF_INET,SOCK_STREAM) + s.settimeout(2) + s.connect(addr) + s.settimeout(None) + s.close() + return addr + except Exception as e: + pass + + +def check_one_super(ha): + host,addrs = ha + valid_addrs = [] + for addr in addrs: + ret = try_connect(addr) + if ret: valid_addrs.append(ret) + if valid_addrs: return (host,valid_addrs) + + +def check_all_the_super(path): + from multiprocessing import Pool + p = Pool(20) + return filter(None,p.map(check_one_super,find_potential_super(path))) + + +def main(): + import os + hostpath=os.environment.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") + + for host,addrs in check_all_the_super(hostpath): + print("%s %s" %(host,str(addrs))) + +if __name__ == "__main__": + main() + +# vim: set expandtab:ts=:sw=2 -- cgit v1.2.3 From 219fab970c7fe455d3dd9bc48e909d96a234046b Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 10 Sep 2015 15:48:38 +0200 Subject: adv_graphgen: finish packaging --- .../scripts/adv_graphgen/tinc_graphs/Supernodes.py | 33 +++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py') diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py index c8514c66..bc66b337 100755 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py @@ -7,21 +7,26 @@ def find_potential_super(path="/etc/tinc/retiolum/hosts"): needle_addr = re.compile("Address\s*=\s*(.*)") needle_port = re.compile("Port\s*=\s*(.*)") for f in os.listdir(path): - with open(path+"/"+f) as of: - addrs = [] - port = "655" + try: + with open(path+"/"+f) as of: + addrs = [] + port = "655" - for line in of.readlines(): + for line in of.readlines(): - addr_found = needle_addr.match(line) - if addr_found: - addrs.append(addr_found.group(1)) + addr_found = needle_addr.match(line) + if addr_found: + addrs.append(addr_found.group(1)) - port_found = needle_port.match(line) - if port_found: - port = port_found.group(1) - - if addrs : yield (f ,[(addr ,int(port)) for addr in addrs]) + port_found = needle_port.match(line) + if port_found: + port = port_found.group(1) + + if addrs : yield (f ,[(addr ,int(port)) for addr in addrs]) + except FileNotFoundError as e: + print("Cannot open hosts directory to be used to find potential supernodes") + print("Directory used: {}".format(path)) + raise def try_connect(addr): @@ -54,11 +59,11 @@ def check_all_the_super(path): def main(): import os - hostpath=os.environment.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") + hostpath=os.environ.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") for host,addrs in check_all_the_super(hostpath): print("%s %s" %(host,str(addrs))) - + if __name__ == "__main__": main() -- cgit v1.2.3