2

I'm trying to make a project that scans for MAC addresses in my network and either allows them or ban them from my local network. I wrote this script to get familiar with the re and pynetgear module to accomplish this because I plan to use these addresses to store data to determine which device is which. Everything I've tried doesn't work or I tried looking up the problem on here and other resources but no one has helped me so far.

I've tried looking at documentation of regex but couldn't figure out what this problem is. The first code all the way up to Devices work its just MAC down that calls the TypeError.

from pynetgear import Netgear
import re

netgear = Netgear(password='password')

devices = netgear.get_attached_devices()

MAC = re.search(r"mac=..:..:..:..:..:..", devices)
print(MAC.group(0))

Traceback (most recent call last):
  File "/home/z33k/Desktop/python/adhdResearch.py", line 8, in <module>
    MAC = re.search(r"mac=..:..:..:..:..:..", devices)
  File "/usr/lib/python2.7/re.py", line 146, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
4
  • 1
    What Python type is devices? Commented Jul 14, 2019 at 17:57
  • devices is a variable that stores the results of the netgear. get_attached_devices. Do I need to convert it into a string? Commented Jul 14, 2019 at 17:59
  • First I would examine the returned devices. Maybe the data you seek is already in a form you could use. Commented Jul 14, 2019 at 18:02
  • Yes, it needs to be a string. You still haven't said what its type is however. You can do print(type(devices)) to see this Commented Jul 14, 2019 at 18:03

2 Answers 2

2

I don't think you need regex here at all.

According to the PyNetgear docs, get_attached_devices returns a list of named tuples. You will need to iterate through the list and print the mac value:

for item in devices:
    print(item.mac)
Sign up to request clarification or add additional context in comments.

Comments

0

re.search requires a string or buffer, but devices is a list containing namedtuples.

You can see for yourself in the source code:

https://github.com/MatMaul/pynetgear/blob/247d6b9524fcee4b2da0e65ca12c52ebdd3676b2/pynetgear/init.py#L46-L49

https://github.com/MatMaul/pynetgear/blob/247d6b9524fcee4b2da0e65ca12c52ebdd3676b2/pynetgear/init.py#L224-L241

Printing out the MAC Addresses:

for device in devices: print(device.mac)

As for allowing or banning the individual addresses, I unfortunately don't know.

1 Comment

Thank you for still helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.