我有一个执行以下任务的脚本: 从文件中读取以获取IP地址 执行Traceroute 将跳数,主机名和IP地址的结果存储到带时间戳的文件中。
问题是,结果显示重复的希望计数,当我与正常的traceroute结果进行比较时,跳数会更多。
以下是该脚本的一部分:
import socket, sys, os, ipList
from ipList import ip4
from datetime import datetime
icmp = socket.getprotobyname('icmp')
udp = socket.getprotobyname('udp')
port = 33434
max_hops = 30
ttl = 1
curr_addr = None
curr_name = None
def main(ips):
FORMAT = '%Y%m%d%H%M'
for ip in ips:
path = ip+'.txt'
new_path = 'results/%s_%s' % (datetime.now().strftime(FORMAT), path)
dest_addr_v4 = ip
m(dest_addr_v4, port, ttl, max_hops, new_path)
def writeFile(p, a):
# with open(new_path, 'a') as result:
result = open(p, 'a')
result.write(a)
result.close()
def create_sockets():
recv_s = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
send_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
send_s.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
return recv_s, send_s
def m(ip, p, t, mh, path):
while True:
recv_s, send_s = create_sockets()
recv_s.bind(("", p))
send_s.sendto("", (ip, p))
try:
_, curr_addr = recv_s.recvfrom(512)
curr_addr = curr_addr[0]
try:
curr_name = socket.gethostbyaddr(curr_addr)[0]
except socket.error:
curr_name = curr_addr
except socket.error:
pass
finally:
send_s.close()
recv_s.close()
if curr_addr is not None:
curr_host = "%s (%s)" % (curr_name, curr_addr)
else:
curr_host = "*"
r = "%d\t%s\n" % (t, curr_host)
if t <= mh:
writeFile(path, r)
t += 1
if curr_addr == ip or t > mh:
print "Reached end of max hop or reached Destination"
return
ipv4List = ip4()
main(ipv4List)
我有IP地址列表的文件,我正在尝试测量路径,所以理想情况下我希望结果尽可能准确。目前我得到的结果不正确,我假设是因为它正在发送多个UPD数据包并获得ICMP作为回报。
请问有什么方法可以微调这个脚本吗?
编辑: 嗨,谢谢你的回复。我很抱歉忘了添加结果。
以下是我上面提到的结果:
1 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
2 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
3 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
4 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
5 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
6 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
7 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
8 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
9 clifton3-gw-t2-1.emman.net (194.82.121.13)
10 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
11 so-0-0-0.lond-sbr1.ja.net (146.97.42.53)
12 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
13 ae13.londtn-sbr1.ja.net (146.97.33.134)
14 prs-bb2-link.telia.net (80.91.246.177)
15 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
16 vlan907-an-cat6k-ts2-r1.starhub.net.sg (203.118.3.217)
17 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
18 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
19 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
20 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
21 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
22 mailhost.lboro.ac.uk (158.125.160.55)
23 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
24 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
25 pinger-j2.ant.isi.edu (203.178.148.19)
26 mailhost.lboro.ac.uk (158.125.160.55)
27 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
28 pinger6.netsec.colostate.edu (129.82.138.44)
29 mailhost.lboro.ac.uk (158.125.160.55)
30 router.comp-sci-staff-dhcp.hsrp.lut.ac.uk (158.125.103.1)
正如你所看到的,一切都是重复的,我怀疑是因为我将max_hop设置为30,如果增加的话,它会继续这样。如果它从我的IPlist文件的第一个条目到达目的地IP:203.175.174.10,则不显示。
我真的很感激任何形式的投入和建议。对不起,如果我的帖子不符合标准。