-1

I don't know how I should ask this question. If I make any mistakes, I would appreciate it if someone could correct them.

I am writing a program in python on Ubuntu. In that program I am struggling to get Mac address of remote machine from its IP address (RaspberryPi), connected to network.

But In actual practice, it is giving me an error:

Traceback (most recent call last): File "Get_MacAddress_from_ip.py", line 9, in <module> mac = re.search(r"([a-fA-F0-9]{2}[:|\-]?){6}", s).groups()[0] AttributeError: 'NoneType' object has no attribute 'groups'

Can anybody guide me on how do I remove this error? My Coding is given below

from Tkinter import *
from subprocess import Popen, PIPE

ip = "192.168.2.34"
username = "pi"
remote_MAC="b8:27:eb:d2:84:ef"
pid = Popen(["arp", "-n", ip], stdout=PIPE)
s = pid.communicate()[0]
mac = re.search(r"([a-fA-F0-9]{2}[:|\-]?){6}", s).groups()[0]
mac = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s).groups()[0]
print mac
Irfan Ghaffar7
  • 1,143
  • 4
  • 11
  • 30

1 Answers1

1

re.search returns None if there are no matches. Try assigning the result and checking if it is None:

search = re.search(r"([a-fA-F0-9]{2}[:|\-]?){6}", s)
mac = None
if search:
    mac = search.groups()[0]
    # You can also do:
    #mac = search.group(0)
print mac
Jmac
  • 308
  • 1
  • 9