我最近一直在阅读“Black hat python”这本书,一切都很好,但是当我尝试 get_mac
函数时,控制台吐出一个错误,具体来说:
NameError: name 'get_mac' is not defined".
我不确定这是否只是基于 Linux 的问题。
代码:
from scapy.all import *
import os
import sys
import threading
import signal
interface="en1"
target_ip="10.0.0.17"
gateway_ip="10.0.0.138"
packet_count=1000
# set up our interface
conf.iface=interface
# turn off input
conf.verg=0
print"[*] Setting up %s"%interface
gateway_mac=get_mac(gateway_ip)
最佳答案
您只转录了书中的部分脚本。文本说明 get_mac
函数将在后面的说明中定义。
在代码之后的第 53 页,作者解释道:
We start by resolving the gateway (1) and target IP (2) address's corresponding MAC addresses using a function called
get_mac
that we'll plumb in shortly.
该函数稍后在第 53 页定义,相当于
def get_mac(ip_address):
responses, unanswered =
srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst_ip_address),
timeout=2, retry=10)
for s, r in responses:
return r[Ether].src
return None
这段代码有多个问题;我不得不修复第一个 return
语句没有缩进的缩进错误,当然,你只能 return
一次,所以循环和最后的 return
是有效的死代码。也许作者是想确保你醒着。
The book is available from Google Books ,所以这就是我使用的。如果他们有不同的版本或其他东西,也许你的略有不同。
关于python - Ubuntu Python : "get_mac" function not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110403/