Ubuntu Python:" get_mac"功能未定义

时间:2016-03-20 05:01:42

标签: python linux ubuntu

我最近一直在阅读这本书,#34; 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)

2 个答案:

答案 0 :(得分:1)

您只从书中转录了部分脚本。该文本解释了<window>函数将在后面的博览会中定义。

在第53页,在代码之后,作者解释说:

  

我们首先使用一个名为get_mac的函数来解析网关(1)和目标IP(2)地址的相应MAC地址。我们将很快探讨这个问题。

该功能稍后在第53页定义,相当于

get_mac

此代码存在多个问题;我必须修复缩进错误,其中第一个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实际上是死代码。也许作者试图确保你醒着。

The book is available from Google Books,这就是我用过的东西。如果他们有不同的版本或某些东西,也许你的版本略有不同。

答案 1 :(得分:0)

我只是试试我的ubuntu 14.04这段代码:

from uuid import getnode as get_mac
mac = get_mac()

一切都很出色。

如果您需要来自所选IP的arp-address,请尝试此stackoverflow问题中的配方:Obtain MAC Address from Devices using Python