-
-
Save warvariuc/dfb7ec59d6cbd1dabc7a705666bd733a to your computer and use it in GitHub Desktop.
Filter a MAC addr via API on an OpenWRT router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://github.com/openwrt/luci/blob/master/docs/JsonRpcHowTo.md | |
opkg update | |
opkg install luci-mod-rpc luci-lib-ipkg luci-compat | |
/etc/init.d/uhttpd restart | |
""" | |
import httpx | |
class Router: | |
def __init__(self, username, password): | |
self.session = httpx.Client( | |
base_url="https://192.168.10.1/cgi-bin/luci/rpc/", verify=False | |
) | |
self.login(username, password) | |
def login(self, username, password): | |
self.request( | |
"auth", {"id": 1, "method": "login", "params": [username, password]} | |
) | |
def request(self, path: str, payload: dict): | |
response = self.session.post(path, json=payload) | |
assert response.status_code == 200 | |
def uci_request(self, payload: dict): | |
# UCI (Unified Configuration Interface) | |
return self.request("uci", payload) | |
def disable_mac_filtering(self): | |
# Disable "MAC Address Filter" | |
self.uci_request( | |
{"method": "delete", "params": ["wireless", "default_radio0", "macfilter"]} | |
) | |
def enable_mac_filtering(self, mac_addr): | |
self.uci_request( | |
{ | |
"method": "set", | |
"params": [ | |
"wireless", | |
"default_radio0", | |
"macfilter", | |
"deny", # allow all except | |
], | |
} | |
) | |
self.uci_request( | |
{ | |
"method": "set", | |
"params": [ | |
"wireless", | |
"default_radio0", | |
"maclist", | |
[mac_addr], | |
], | |
}, | |
) | |
def apply_changes(self): | |
self.uci_request( | |
{ | |
"method": "apply", | |
"params": [False], # rollback=False | |
}, | |
) | |
router = Router("root", "########") | |
router.enable_mac_filtering("C4:35:D9:9F:74:BF") | |
# router.disable_mac_filtering() | |
router.apply_changes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment