summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/adv_graphgen/find_super.py
blob: df01734efbf6ee703cc37cea7369e3d92f631558 (plain)
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
#!/usr/bin/python

def find_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 check_super(path="/etc/tinc/retiolum/hosts"):
  from socket import socket,AF_INET,SOCK_STREAM
  for host,addrs in find_super(path):
    valid_addrs = []
    for addr in addrs:
      try:
        s = socket(AF_INET,SOCK_STREAM)
        s.settimeout(3)
        s.connect(addr)
        #print("success connecting %s:%d"%(addr))
        s.settimeout(None)
        s.close()
        valid_addrs.append(addr)
      except Exception as e:
        pass
        #print("cannot connect to %s:%d"%(addr))
    if valid_addrs: yield (host,valid_addrs)


if __name__ == "__main__":
  """
  usage
  """
  for host,addrs in check_super():
    print host,addrs