pynetgear
pynetgear copied to clipboard
Sorry to create an issue for this but how about a walk-through of all the features.
Wanting to either block or allow a couple of kids access to the internet at the run of a script. I can not seem to figure it out. I can authenticate, get_attached_devices. but I am failing to toggle Allow or Block for allow_block_device(mac='00:00:00:00:00:00', device_status=Block
I was looking to do the exact same thing with this library. So from one dad to another, below is a working script.
Basically, run the script with -d first to get the MAC addresses of attached devices and then run it again with -a (for allow) or -b (for block) and then pass the MAC of the device you want to allow/block.
Try this:
#!/usr/local/bin/python3
import pynetgear
import argparse
argparser = argparse.ArgumentParser(description='Router Tool')
argparser.add_argument('-d', '--devices', dest='attached',
action='store_true', help='Get attached devices')
argparser.add_argument('-a', '--allow', dest='allow', help='Allow device')
argparser.add_argument('-b', '--block', dest='block', help='Block device')
args = argparser.parse_args()
# Create a Netgear object
netgear = pynetgear.Netgear(password='Your_Password_Here')
mac_allow = args.allow
mac_block = args.block
# Get Devices and print the tuple in an easy to read format
def get_devices(netgear):
devices = netgear.get_attached_devices_2()
for device in devices:
print("Name: {0}".format(device.name))
print("Type: {0}".format(device.type))
print("Device Model: {0}".format(device.device_model))
print("Mac: {0}".format(device.mac))
print("IP: {0}".format(device.ip))
print("Allow/Block: {0}".format(device.allow_or_block))
print("")
# Get Devices
if args.attached:
get_devices(netgear)
# Allow Device
if args.allow:
netgear.allow_block_device(mac_addr=mac_allow, device_status="Allow")
# Block Device
if args.block:
netgear.allow_block_device(mac_addr=mac_block, device_status='Block')
Note you could use HomeAssistant for this, but of course a simple python script will also do:
To print a list of all devices to figure out the mac of the device you want to block
from pynetgear import Netgear
netgear = Netgear(password="PASSWORD", user="USERNAME", host="192.168.1.IP")
netgear.login_try_port()
# depending on the model of the router use get_attached_devices or get_attached_devices_2
for device in netgear.get_attached_devices_2():
print(device)
To block a device:
from pynetgear import Netgear, BLOCK, ALLOW
netgear = Netgear(password="PASSWORD", user="USERNAME", host="192.168.1.IP")
netgear.login_try_port()
mac_addr = "AA:00:04:00:XX:YY"
netgear.allow_block_device(mac_addr, device_status=BLOCK):
To allow a device:
from pynetgear import Netgear, BLOCK, ALLOW
netgear = Netgear(password="PASSWORD", user="USERNAME", host="192.168.1.IP")
netgear.login_try_port()
mac_addr = "AA:00:04:00:XX:YY"
netgear.allow_block_device(mac_addr, device_status=ALLOW):