0

I've recently been following through the book, "Black hat python", it's been alright but when I try the get_mac function the console spits out an error, specifically:

NameError: name 'get_mac' is not defined". 

I am not sure if this just is a Linux based issue.

Code:

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)
Jason
  • 2,278
  • 2
  • 17
  • 25
Boris
  • 75
  • 8

2 Answers2

1

You have transcribed only part of the script from the book. The text explains that the get_mac function will be defined later on in the exposition.

On page 53, right after the code, the author explains:

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.

The function is defined later on page 53, and amounts to

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

This code has multiple problems; I had to fix an indentation error where the first return statement was not indented, and of course, you can only return once, so the loop and the final return are effectively dead code. Perhaps the author is trying to make sure you are awake.

The book is available from Google Books, so that's what I used. If they have a different edition or something, maybe yours is slightly different.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

I just try on my ubuntu 14.04 this code:

from uuid import getnode as get_mac
mac = get_mac()

All doing excellent.

If you need an arp-address from selected ip try the recipe from this stackoverflow question: Obtain MAC Address from Devices using Python

Community
  • 1
  • 1
Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
  • That does not work when i insert it into my current code, I receive this error: "TypeError: getnode() takes no arguments (1 given)" – Boris Mar 20 '16 at 05:13
  • I add a link to similar question on stackoverflow with another recipe. Try this one. – Vasyl Moskalov Mar 20 '16 at 05:19