-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.py
More file actions
73 lines (62 loc) · 2.2 KB
/
trace.py
File metadata and controls
73 lines (62 loc) · 2.2 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import socket
from argparse import ArgumentParser
import sys
import re
import json
import urllib.request
def trace(dist_ip):
icmp_packet = b'\x08\x00\xf7\x4a\x00\x01\x00\xb4'
connetion = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
ttl = 1
cur_ip = None
connetion.settimeout(5)
while ttl != 30 and cur_ip != dist_ip:
connetion.setsockopt(socket.SOL_IP, socket.IP_TTL, int(ttl))
connetion.sendto(icmp_packet, (dist_ip, 33434))
try:
packet, ipPort = connetion.recvfrom(1024)
cur_ip = ipPort[0]
message = '%d) %s' % (ttl, cur_ip)
if public_ip(cur_ip):
message += ' ' + str(simple_whois(cur_ip))
yield message
else:
yield message + " its not public ip"
except socket.timeout:
yield '***** TimeOUT *****'
ttl += 1
connetion.close()
def public_ip(ip):
local_ip_addresses_diapasons = (
('10.0.0.0', '10.255.255.255'),
('127.0.0.0', '127.255.255.255'),
('172.16.0.0', '172.31.255.255'),
('192.168.0.0', '192.168.255.255'))
for diapason in local_ip_addresses_diapasons:
if diapason[0] <= ip <= diapason[1]:
return False
return True
def init_parser():
parser = ArgumentParser(prog="trace.py")
parser.add_argument("-ip", action="store", help="ip to check.")
return parser
def simple_whois(addr):
data = json.loads(
urllib.request.urlopen(
'https://stat.ripe.net/data/prefix-overview/data.json?max_related=50&resource=%s' % addr).read())
if len(data['data']['asns']) == 0:
return '', '', ''
as_name = data['data']['asns'][0]['asn']
provider = data['data']['asns'][0]['holder']
data = json.loads(
urllib.request.urlopen('https://stat.ripe.net/data/rir/data.json?resource=%s&lod=2' %
addr).read())
country = data['data']['rirs'][0]['country']
return as_name, country, provider
if __name__ == '__main__':
parse = init_parser()
args = parse.parse_args()
if args.ip is not None:
for message in trace(args.ip):
print(message)