【问题标题】:How to handle request timed out in python - traceroute script using scapy如何在 python 中处理请求超时 - 使用 scapy 的 traceroute 脚本
【发布时间】:2019-08-28 16:51:01
【问题描述】:

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

【问题讨论】:

    标签: python scapy traceroute


    【解决方案1】:

    使用sr1(tracert_packet, verbose=0, timeout=TIMEOUT_VAL)。如果没有答案,则返回值将是None,因此在与ICMP 连接之前请检查...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多