1
1
#!/usr/bin/python
2
2
# -*- coding: UTF-8 -*-
3
+ import urllib
4
+ import urllib2
3
5
4
6
def change_to_list (filename ):
5
7
content = open (filename ,"r" ).read ().strip ()
@@ -17,16 +19,27 @@ def traceroute_to_dict(filename):
17
19
line = content [i ]
18
20
if line [1 ].isdigit ():
19
21
if line [4 ] != "*" :
20
- latency = line .strip ().split (" " )[2 ]
22
+ ip = line .strip ().split (" " )[1 ]
21
23
asn = line .strip ().split (" " )[3 ]
24
+ iptest = ip .strip ().split (" " )[0 ]
25
+ url = "http://ip-api.com/csv/" + iptest
26
+ req = urllib2 .Request (url )
27
+ res_data = urllib2 .urlopen (req )
28
+ res = res_data .read ()
29
+ if res .strip ().split ("," )[0 ] == "success" :
30
+ isp = res .strip ().split ("," )[11 ]
31
+ asn = res .strip ().split ("," )[12 ]
32
+ else :
33
+ isp = "*"
34
+ latency = line .strip ().split (" " )[2 ]
22
35
route = line .strip ().split (" " )[4 ]
23
- ip = line .strip ().split (" " )[1 ]
24
36
step = line [0 :2 ]
25
37
else :
26
38
latency = "*"
27
39
asn = "*"
28
40
route = "*"
29
41
ip = "*"
42
+ isp = "*"
30
43
step = line [0 :2 ]
31
44
32
45
d [int (step )]= dict ()
@@ -36,7 +49,7 @@ def traceroute_to_dict(filename):
36
49
d [int (step )]["latency" ]= latency
37
50
d [int (step )]["asn" ]= asn
38
51
d [int (step )]["route" ]= route
39
-
52
+ d [ int ( step )][ "isp" ] = isp
40
53
41
54
return dict (d )
42
55
@@ -52,9 +65,10 @@ def traceroute_to_table(filename):
52
65
<td>{}</td>
53
66
<td>{}</td>
54
67
<td>{}</td>
68
+ <td>{}</td>
55
69
</tr>
56
70
"""
57
- string = string + template .format (i ,x ["ip" ],x ["route" ],x ["asn" ],x ["latency" ]) + "\n "
71
+ string = string + template .format (i ,x ["ip" ],x ["route" ],x ["isp" ], x [ " asn" ],x ["latency" ]) + "\n "
58
72
59
73
writefile = open (filename + "_table" ,"w" )
60
74
writefile .write (string )
@@ -75,6 +89,7 @@ def dict_to_table(d,tab):
75
89
<tr><th>跳数</th>
76
90
<th>IP</th>
77
91
<th>路由</th>
92
+ <th>ISP</th>
78
93
<th>AS Number</th>
79
94
<th>延迟</th>
80
95
</tr></thead>
@@ -90,9 +105,10 @@ def dict_to_table(d,tab):
90
105
<td>{2}</td>
91
106
<td>{3}</td>
92
107
<td>{4}</td>
108
+ <td>{5}</td>
93
109
</tr>
94
110
95
- """ .format (step ,d [step ]["ip" ],d [step ]["route" ],d [step ]["asn" ],d [step ]["latency" ])
111
+ """ .format (step ,d [step ]["ip" ],d [step ]["route" ],d [step ]["isp" ], d [ step ][ " asn" ],d [step ]["latency" ])
96
112
97
113
table_html = table_html + """
98
114
</tbody>
@@ -479,6 +495,7 @@ def dict_to_table(d,tab):
479
495
<a class="item" data-tab="fourth">广东移动</a>
480
496
<a class="item" data-tab="fifth">广东电信</a>
481
497
<a class="item" data-tab="sixth">广东联通</a>
498
+ <a class="item" data-tab="seventh">所在地IP</a>
482
499
</div>
483
500
484
501
"""
@@ -627,6 +644,9 @@ def dict_to_table(d,tab):
627
644
traceroute_to_table ("/tmp/gdu.txt" )
628
645
gdu_html = dict_to_table (gdu ,"sixth" )
629
646
647
+ own = traceroute_to_dict ("/tmp/own.txt" )
648
+ traceroute_to_table ("/tmp/own.txt" )
649
+ own_html = dict_to_table (own ,"seventh" )
630
650
631
651
html = html .format (info [0 ],info [1 ],info [2 ],info [3 ],info [4 ],info [5 ],info [6 ],info [7 ],info [8 ],info [9 ],info [10 ],info [11 ],info [12 ],info [13 ],info [14 ], \
632
652
@@ -640,7 +660,7 @@ def dict_to_table(d,tab):
640
660
641
661
speed_cn [18 ],speed_cn [19 ],speed_cn [20 ],speed_cn [21 ],speed_cn [22 ],speed_cn [23 ])
642
662
643
- html = html + shm_html + sht_html + shu_html + gdm_html + gdt_html + gdu_html + footer
663
+ html = html + shm_html + sht_html + shu_html + gdm_html + gdt_html + gdu_html + own_html + footer
644
664
645
665
web = open ("/root/report.html" ,"w" )
646
666
0 commit comments