如何使用scapy处理python traceroute脚本中的请求超时

2024-06-16 11:09:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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()

Tags: the答案程序packetmainsys情况destination
1条回答
网友
1楼 · 发布于 2024-06-16 11:09:29

使用sr1(tracert_packet, verbose=0, timeout=TIMEOUT_VAL)。如果没有答案,返回值将是None,所以在连接ICMP之前请检查。。。你知道吗

相关问题 更多 >