我正在尝试使用Python和Scapy (使用ICMP)编写traceroute程序。我有一个问题,有时我得不到目的地的回复,而我的程序只是停留在等待它。(在cmd上使用traceroute时,这相当于“请求超时”的情况)。我认为sr1函数在等待答案,但它从未得到答案。我怎么才能修复它?
我也不确定我是否处理了到达目的地的情况,我检查了类型是否等于3,但我不确定它是否正确。我也很高兴得到关于这个问题的答案。
import sys
i, o, e = sys.stdin, sys.stdout, sys.stderr
from scapy.all import *
sys.stdin, sys.stdout, sys.stderr = i, o, e
def main():
hostname = input("Enter the destination")
done = False
distance = 1
while not done:
tracert_packet = IP(dst=hostname, ttl=distance)/ICMP()
# Send the packet and get a reply
tracert_resopnse = sr1(tracert_packet, verbose=0)/ICMP()
if tracert_resopnse.type == 3:
# We've reached our destination
print("Done!" + tracert_resopnse[IP].src)
done = True
else:
# We haven't got to the destination yet
print(str(distance) +" hops away: "+ str(tracert_resopnse.src))
distance += 1
if __name__ == '__main__':
main()发布于 2019-08-28 16:51:02
使用sr1(tracert_packet, verbose=0, timeout=TIMEOUT_VAL)。如果没有答案,返回值将为None,因此在连接ICMP之前请检查...
https://stackoverflow.com/questions/57661283
复制相似问题