scapy :: traceroute设置源IP地址

时间:2015-11-05 18:14:01

标签: python scapy traceroute

我有一个设置,我的源服务器使用任播地址连接到网络,因此每次我ping或traceroute我连接的网络中的任何目的地时,我都需要使用源IP。

我目前正在尝试scapy并使用sr方法,但scapy中的traceroute具有一些我需要使用的强大功能。 scapy中的traceroute不会像sr方法那样获取任何源地址。

有解决方法吗?或者在scapy :: traceroute之上是否有任何包装允许我这样做?

1 个答案:

答案 0 :(得分:0)

检查Scapy's traceroute function's code表明它非常简单,而它的大部分强大功能都在于它使用从简单TracerouteResult调用接收到的结果实例化的sr类,如可以看到的那样目前的实施:

@conf.commands.register
def traceroute(target, dport=80, minttl=1, maxttl=30, sport=RandShort(), l4 = None, filter=None, timeout=2, verbose=None, **kargs):
    """Instant TCP traceroute
traceroute(target, [maxttl=30,] [dport=80,] [sport=80,] [verbose=conf.verb]) -> None
"""
    if verbose is None:
        verbose = conf.verb
    if filter is None:
        # we only consider ICMP error packets and TCP packets with at
        # least the ACK flag set *and* either the SYN or the RST flag
        # set
        filter="(icmp and (icmp[0]=3 or icmp[0]=4 or icmp[0]=5 or icmp[0]=11 or icmp[0]=12)) or (tcp and (tcp[13] & 0x16 > 0x10))"
    if l4 is None:
        a,b = sr(IP(dst=target, id=RandShort(), ttl=(minttl,maxttl))/TCP(seq=RandInt(),sport=sport, dport=dport),
                 timeout=timeout, filter=filter, verbose=verbose, **kargs)
    else:
        # this should always work
        filter="ip"
        a,b = sr(IP(dst=target, id=RandShort(), ttl=(minttl,maxttl))/l4,
                 timeout=timeout, filter=filter, verbose=verbose, **kargs)

    a = TracerouteResult(a.res)
    if verbose:
        a.show()
    return a,b

因此,您可以以与此处执行的方式类似的方式调用sr,但也使用源地址,并将结果传递给TracerouteResult类以便使用它强大的功能。

注意:可以将同等方法应用于Scapy's traceroute6 function's code for IPv6