Last active
September 8, 2021 18:13
-
-
Save speters/4240fa4da78473f882ce6c01e12583dd to your computer and use it in GitHub Desktop.
FritzBox HostFilter script to enable/disbale WAN access of lan devices
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
#!/usr/bin/env python3 | |
import tr064 | |
import argparse | |
import configparser | |
from os import path | |
from socket import gethostbyname_ex | |
from time import sleep | |
args = {} | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--url', metavar='URL', type=str, default='http://fritz.box', | |
help='The URL of your Fritz!Box; default: http://fritz.box') | |
parser.add_argument('--user', metavar='USER', type=str, default='', | |
help='Login username; default: empty') | |
parser.add_argument('--password', metavar='PASSWORD', type=str, default='', | |
help='Login password') | |
parser.add_argument('--inifile', metavar='INIFILE', type=str, default='~/.smtpclient.ini', | |
help='.ini config file holding username/password') | |
parser.add_argument('ipaddresses', nargs='+', type=str, metavar='ipaddress[=0|=1]') | |
args = parser.parse_args() | |
config = configparser.ConfigParser({'host': 'fritz.box', 'username':'', 'password':''}) | |
config.read(['/etc/smtpclient.ini',path.expanduser(args.inifile)]) | |
if config.has_section('fritz.box'): | |
section = 'fritz.box' | |
else: | |
try: | |
section=config.sections()[0] | |
except IndexError: | |
section = 'DEFAULT' | |
host = config.get(section, 'host') | |
if host != '': | |
if host[0:7] == 'http://' or host[0:8] == 'https://': | |
args.url = host | |
else: | |
args.url = 'http://' + host | |
if args.user == '': | |
args.user = config.get(section, 'username') | |
if args.password == '': | |
args.password = config.get(section, 'password') | |
if args.password == '': | |
raise argparse.ValueError("password is required") | |
client = tr064.Client(args.user, args.password, args.url+':49000') | |
for i in args.ipaddresses: | |
try: | |
d = i.split('=',1) | |
if d[1] == '0': | |
d[1] = 'deny' | |
else: | |
d[1] = 'grant' | |
except IndexError: | |
d = [i, 'query'] | |
addresses = [] | |
clienthost=d[0] | |
try: | |
res = gethostbyname_ex(clienthost) | |
addresses = res[2] | |
except: | |
pass | |
for address in addresses: | |
if d[1] == 'query': | |
pass | |
else: | |
if d[1] == 'deny': | |
disallow = '1' | |
else: | |
disallow = '0' | |
o = client.InternetGatewayDevice.X_AVM_DE_HostFilter.DisallowWANAccessByIP(NewIPv4Address=address, NewDisallow=disallow) | |
o = client.InternetGatewayDevice.X_AVM_DE_HostFilter.GetWANAccessByIP(NewIPv4Address=address) | |
print('{0}[{1}] = {2}' .format(clienthost, address, o)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment