微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在CMD上运行traceroute命令得到的结果和使用scapy SR函数得到的结果不一样?

如何解决为什么在CMD上运行traceroute命令得到的结果和使用scapy SR函数得到的结果不一样?

enter image description here

  1. 这是运行 traceroute 110.242.68.3 的命令行 enter image description here
  2. 这是使用scapy库编写的traceroute
def tcptraceroute(host,timeout,maxttl=30):
    """tcptraceroute sends TCP messages with SYN flag set
    to host with increasing TTL until it reaches destination
    host or reaches maxttl
    :host: traceroute's destination
    """

    ip = host

    print("tcptraceroute to '{}' ({}),{} hops max".format(host,ip,maxttl))
    for i in range(1,maxttl + 1):
        for j in range(3):
            packet = IP(dst=ip,ttl=i) / TCP(flags='S')
            resp,_ = sr(packet,verbose=0,timeout=timeout)
            src = "*"
            duration = ""
            if len(resp) > 0:
                src = resp[0][1].src
                duration = "{:.3f} ms".format(
                    1000 * (resp[0][1].time - resp[0][0].sent_time))
            print("{:2d}. {:>15}\t{}".format(i,src,duration))
            if src == ip:
                return
    print("Couldn't traceroute {},reached max TTL '{}'".format(ip,maxttl))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。