Statistics
| Branch: | Tag: | Revision:

root / utils / whois.py @ 049a5a10

History | View | Annotate | Download (1.3 kB)

1
import socket
2
from ipaddr import *
3
import re
4

    
5
RIPEWHOIS = 'whois.ripe.net'
6
GRNETWHOIS = 'whois.grnet.gr'
7

    
8
def query(query, hostname, flags):
9
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10
    s.connect((hostname, 43))
11
    # as IPv6 is not supported by flowspec for the time ommit -T route6 
12
    s.send(" -i origin -r -K -T route " + query + "\r\n")
13
    response = ''
14
    while True:
15
        d = s.recv(4096)
16
        response += d
17
        if not d:
18
            break
19
    s.close()
20
    query = response.splitlines()
21
    routes4 = []
22
    routes6 = []
23
    final_routes4 = []
24
    final_routes6 = []
25
    for line in query:
26
        m = re.match(r"(^route6?\:\s+)(?P<subnets>\S+)", line)
27
        if m:
28
            if IPNetwork(m.group('subnets')).version == 4:
29
                routes4.append(IPNetwork(m.group('subnets')))
30
            if IPNetwork(m.group('subnets')).version == 6:
31
                routes6.append(IPNetwork(m.group('subnets')))
32
    final_routes = []
33
    if len(routes4):
34
        final_routes4 = collapse_address_list(routes4)
35
    if len(routes6):
36
        final_routes6 = collapse_address_list(routes6)
37
    final_routes = final_routes4 + final_routes6
38
    return final_routes
39

    
40
def whois(queryas):
41
    routes = query(queryas,GRNETWHOIS, None)
42
    if not routes:
43
        routes = query(queryas,RIPEWHOIS, None)
44
    return routes