文章内容

2021/2/24 18:12:57,作 者: 黄兵

python 解析linuxtraceroute结果

最近需要调用Linux系统的traceroute命令,但是读取了结果,还要对结果进行解析,下面是代码:

def scapy_traceroute(self):
traceroute_str = """
traceroute to www.materialtools.com (150.109.42.16), 30 hops max, 60 byte packet s
1 * * *
2 te-2-0-6.cr2.lax2.us.zenlayer.net (192.254.81.134) 4.954 ms 4.999 ms 5.04 9 ms
3 192.254.84.246 (192.254.84.246) 1.569 ms 1.622 ms 1.781 ms
4 192.254.81.31 (192.254.81.31) 0.641 ms 192.254.81.27 (192.254.81.27) 1.265 ms 0.930 ms
5 nextgen-multimedia.as9305.any2ix.coresite.com (206.72.210.114) 0.599 ms 0. 584 ms 0.552 ms
6 218.189.5.178 (218.189.5.178) 0.521 ms 218.189.5.146 (218.189.5.146) 0.450 ms 218.189.5.141 (218.189.5.141) 0.495 ms
7 d1-1-224-143-118-on-nets.com (118.143.224.1) 189.220 ms d1-133-224-143-118- on-nets.com (118.143.224.133) 189.157 ms d1-49-224-143-118-on-nets.com (118.143 .224.49) 189.170 ms
8 218.189.5.24 (218.189.5.24) 188.987 ms 188.792 ms 188.732 ms
9 global.hgc.com.hk (218.189.23.194) 188.687 ms 188.442 ms 188.372 ms
10 10.196.95.229 (10.196.95.229) 188.191 ms 10.196.95.225 (10.196.95.225) 185 .124 ms 185.072 ms
11 * * *
12 * 10.200.175.218 (10.200.175.218) 189.898 ms *
13 9.13.116.251 (9.13.116.251) 189.808 ms 9.13.118.35 (9.13.118.35) 189.729 m s 189.722 ms
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
"""
hops = self.get_hops(traceroute_str)
self.get_formatted_hops(hops)

@staticmethod
def get_hops(traceroute):
"""
Returns hops from traceroute output in an array of dicts each
with hop number and the associated hosts data.
"""
hops = []
regex = r'^(?P<hop_num>\d+)(?P<hosts>.*?)$'
lines = traceroute.split("\n")
for line in lines:
line = line.strip()
if not line:
continue
try:
hop = re.match(regex, line).groupdict()
except AttributeError:
continue
hops.append(hop)
return hops

@staticmethod
def get_formatted_hops(hops):
"""
Hosts data from get_hops() is represented in a single string.
We use this function to better represent the hosts data in a dict.
"""
formatted_hops = []
regex = r'(' \
r'(?P<i1>[\d.]+) \((?P<h1>[\w.-]+)\)' \
r'|' \
r'(?P<h2>[\w.-]+) \((?P<i2>[\d.]+)\)' \
r')' \
r' (?P<r>\d{1,4}.\d{1,4}\s{0,1}ms)'
for hop in hops:
hop_num = int(hop['hop_num'].strip())
hosts = hop['hosts'].replace(" ", " ").strip()
# Using re.finditer(), we split the hosts, then for each host,
# we store a tuple of hostname, IP address and the first RTT.
hosts = re.finditer(regex, hosts)
for host in hosts:
hop_context = {
'hop_num': hop_num,
'hostname': host.group('h1') or host.group('h2'),
'ip_address': host.group('i1') or host.group('i2'),
'rtt': host.group('r'),
}
formatted_hops.append(hop_context)
print(formatted_hops)
return formatted_hops

执行之后的结果如下:

[{'hop_num': 2, 'hostname': 'te-2-0-6.cr2.lax2.us.zenlayer.net', 'ip_address': '192.254.81.134', 'rtt': '4.954 ms'}, {'hop_num': 3, 'hostname': '192.254.84.246', 'ip_address': '192.254.84.246', 'rtt': '1.569 ms'}, {'hop_num': 4, 'hostname': '192.254.81.31', 'ip_address': '192.254.81.31', 'rtt': '0.641 ms'}, {'hop_num': 5, 'hostname': 'nextgen-multimedia.as9305.any2ix.coresite.com', 'ip_address': '206.72.210.114', 'rtt': '0.599 ms'}, {'hop_num': 6, 'hostname': '218.189.5.178', 'ip_address': '218.189.5.178', 'rtt': '0.521 ms'}, {'hop_num': 6, 'hostname': '218.189.5.141', 'ip_address': '218.189.5.141', 'rtt': '0.495 ms'}, {'hop_num': 7, 'hostname': 'd1-1-224-143-118-on-nets.com', 'ip_address': '118.143.224.1', 'rtt': '189.220 ms'}, {'hop_num': 7, 'hostname': 'on-nets.com', 'ip_address': '118.143.224.133', 'rtt': '189.157 ms'}, {'hop_num': 8, 'hostname': '218.189.5.24', 'ip_address': '218.189.5.24', 'rtt': '188.987 ms'}, {'hop_num': 9, 'hostname': 'global.hgc.com.hk', 'ip_address': '218.189.23.194', 'rtt': '188.687 ms'}, {'hop_num': 10, 'hostname': '10.196.95.229', 'ip_address': '10.196.95.229', 'rtt': '188.191 ms'}, {'hop_num': 12, 'hostname': '10.200.175.218', 'ip_address': '10.200.175.218', 'rtt': '189.898 ms'}, {'hop_num': 13, 'hostname': '9.13.116.251', 'ip_address': '9.13.116.251', 'rtt': '189.808 ms'}]

返回一个列表之后就很好操作了。

上面解析代码来自于此处:traceroute


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - python 解析linuxtraceroute结果

分享到:

发表评论

评论列表