-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestbgp.py
More file actions
executable file
·57 lines (44 loc) · 1.58 KB
/
testbgp.py
File metadata and controls
executable file
·57 lines (44 loc) · 1.58 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
#!/usr/bin/python
import testttl
import sys
import random
import socket
import struct
'''
Input: "1.2.3.0/24"
Output: "1.2.3.87"
'''
def get_random_host_in_network(network):
# parse 1.2.3.0/24 into prefix (0x01020304) and netmask (0xffffff00)
prefix_str, prefix_len = network.split('/')
prefix, = struct.unpack('!I', socket.inet_aton(prefix_str))
netmask = ~(2**(32 - int(prefix_len)) - 1)
# pick a random host
return socket.inet_ntoa(struct.pack('!I',
prefix | (~netmask & random.randint(0, 0xffffffff))))
def get_hops(prefixes_file=sys.stdin, num_routers=5):
hosts = []
# For each network, pick a random host in it
lines = prefixes_file.readlines()
best_hops = {}
while len(best_hops.keys()) < num_routers:
index = random.randint(0, len(lines))
rand_host = get_random_host_in_network(lines[index])
hops = testttl.test_dest(rand_host)
print '-----'
print '%s' % rand_host
testttl.print_hops(hops)
payload_len_hop, payload_len = testttl.get_max_payload_len(hops)
if (payload_len >= 64):
best_hops[(rand_host, payload_len_hop)] = payload_len
#print '-----'
#print 'Using:'
using_hops = []
for host, hop in best_hops.keys():
real_mtu = testttl.confirm_max_mtu(host, hop)
if real_mtu > 64:
using_hops.append( (host, hop, real_mtu) )
return using_hops
if __name__ == "__main__":
for host, hop, real_mtu in get_hops():
print '%s hop %d (MTU %d)' % (host, hop, real_mtu)