Statistics
| Branch: | Tag: | Revision:

root / utils / whois.py @ e9d46ce1

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
    s.send(" -i origin -r -K -T route -T route6 " + query + "\r\n")
12
    response = ''
13
    while True:
14
        d = s.recv(4096)
15
        response += d
16
        if not d:
17
            break
18
    s.close()
19
    query = response.splitlines()
20
    routes4 = []
21
    routes6 = []
22
    final_routes4 = []
23
    final_routes6 = []
24
    for line in query:
25
        m = re.match(r"(^route6?\:\s+)(?P<subnets>\S+)", line)
26
        if m:
27
            if IPNetwork(m.group('subnets')).version == 4:
28
                routes4.append(IPNetwork(m.group('subnets')))
29
            if IPNetwork(m.group('subnets')).version == 6:
30
                routes6.append(IPNetwork(m.group('subnets')))
31
    final_routes = []
32
    if len(routes4):
33
        final_routes4 = collapse_address_list(routes4)
34
    if len(routes6):
35
        final_routes6 = collapse_address_list(routes6)
36
    final_routes = final_routes4 + final_routes6
37
    return final_routes
38

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