Statistics
| Branch: | Tag: | Revision:

root / utils / whois.py @ 1534e9c2

History | View | Annotate | Download (1.3 kB)

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

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

    
38
def whois(queryas):
39
    routes = query(queryas,settings.PRIMARY_WHOIS, None)
40
    if not routes:
41
        routes = query(queryas,settings.ALTERNATE_WHOIS, None)
42
    return routes