【问题标题】:Ubuntu Python: "get_mac" function not definedUbuntu Python:未定义“get_mac”函数
【发布时间】:2016-07-06 17:51:03
【问题描述】:

我最近一直在阅读《黑帽蟒》这本书,一切正常,但是当我尝试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)

【问题讨论】:

  • 没有这个功能。它可能在本书的前面定义过吗?

标签: python linux ubuntu


【解决方案1】:

你只抄录了书中的部分剧本。文中解释了get_mac函数将在后面的说明中定义。

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

我们首先使用一个名为 get_mac 的函数解析网关 (1) 和目标 IP (2) 地址的对应 MAC 地址,我们将很快介绍该函数。

函数在第 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,这就是我使用的。如果他们有不同的版本或其他东西,也许你的会略有不同。

【讨论】:

  • 我还注意到您抄写了conf.verb(如verbose)错误。
  • import * 显然风格也很差。
【解决方案2】:

我只是在我的 ubuntu 14.04 上试试这个代码:

from uuid import getnode as get_mac
mac = get_mac()

一切都做得很好。

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

【讨论】:

  • 当我将它插入到我当前的代码中时它不起作用,我收到这个错误:“TypeError: getnode() 没有参数(1 个给定)”
  • 我在 stackoverflow 上用另一个食谱添加了一个类似问题的链接。试试这个。
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 2018-07-31
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多