import networkx as nx
data = """traceroute to www.vancouver.ca (199.175.219.246), 64 hops max, 52 byte packets
1 192.168.1.254 (192.168.1.254) 2.381 ms 2.554 ms 0.841 ms
2 10.31.196.1 (10.31.196.1) 27.991 ms 19.714 ms 17.100 ms
3 clgrab31gr01.bb.telus.com (154.11.10.170) 46.593 ms 44.800 ms 270.093 ms
4 154.11.2.218 (154.11.2.218) 43.418 ms 42.816 ms 43.113 ms
5 core3-calgaryqa_gi6-0-0.net.bell.ca (64.230.77.250) 61.020 ms 64.224 ms 61.376 ms
6 * * *
7 * core3-vancouverbg_pos10-1-0.net.bell.ca (64.230.77.205) 62.431 ms 62.479 ms
8 * agg1-vancouverbg_xe5-0-0.net.bell.ca (64.230.77.229) 61.626 ms 128.529 ms
9 dis4-vancouverbg_10-0-0.net.bell.ca (64.230.122.241) 132.342 ms 60.667 ms 62.148 ms
10 10.1.7.2 (10.1.7.2) 61.920 ms 61.926 ms 60.517 ms
11 204.101.186.34 (204.101.186.34) 61.635 ms 62.415 ms 61.575 ms
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * * """
data = data.split("\n")
data = data[1:]
cleaned_ip = [ ",".join(item.split()[1:4]) for item in data if "* * *" not in item ]
import re
pattern = r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
path_list = [re.findall(pattern,line)[0] for line in cleaned_ip]
node_node = zip(path_list,path_list[1:])
node_node
[('192.168.1.254', '10.31.196.1'), ('10.31.196.1', '154.11.10.170'), ('154.11.10.170', '154.11.2.218'), ('154.11.2.218', '64.230.77.250'), ('64.230.77.250', '64.230.77.205'), ('64.230.77.205', '64.230.77.229'), ('64.230.77.229', '64.230.122.241'), ('64.230.122.241', '10.1.7.2'), ('10.1.7.2', '204.101.186.34')]
trace = nx.Graph()
[trace.add_edge(a,b) for a,b in node_node]
[None, None, None, None, None, None, None, None, None]
trace.edges()
[('64.230.77.229', '64.230.122.241'), ('64.230.77.229', '64.230.77.205'), ('10.31.196.1', '154.11.10.170'), ('10.31.196.1', '192.168.1.254'), ('154.11.10.170', '154.11.2.218'), ('10.1.7.2', '64.230.122.241'), ('10.1.7.2', '204.101.186.34'), ('64.230.77.250', '154.11.2.218'), ('64.230.77.250', '64.230.77.205')]
trace.nodes()
['64.230.77.229', '10.31.196.1', '154.11.10.170', '10.1.7.2', '64.230.77.250', '192.168.1.254', '154.11.2.218', '64.230.122.241', '204.101.186.34', '64.230.77.205']
nx.draw(trace)