Skip to content

Instantly share code, notes, and snippets.

@dbrgn
Last active January 30, 2016 23:24
Show Gist options
  • Select an option

  • Save dbrgn/802de98379c54719c8e7 to your computer and use it in GitHub Desktop.

Select an option

Save dbrgn/802de98379c54719c8e7 to your computer and use it in GitHub Desktop.
A small script to block Samsung IPs from a Mikrotik Router.
"""
A small script to block Samsung IPs from a Mikrotik Router.
"""
import re
import subprocess
# File containing hostnames, one per line
INPUT_FILE = 'dns-lookups.txt'
# File containing block commands
OUTPUT_FILE = 'block.txt'
# Name of firewall address list
LIST_NAME = 'samsung'
# SmartTV MAC
MAC = 'aa:bb:cc:dd:ee:ff'
ipv4_re = re.compile(r'^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$')
def resolve(host):
"""
Resolve the host, return list of IPs.
"""
response = subprocess.check_output(['dig', '+short', 'A', host]).decode('utf8')
return [r for r in response.split() if ipv4_re.match(r)]
# Collect IPs
ips = set()
with open(INPUT_FILE, 'r') as f:
for line in f:
host = line.strip()
if 'samsung' in host.lower():
print("%s" % host)
for ip in resolve(host):
print(" %s" % ip)
ips.add(ip)
# Write output file
with open(OUTPUT_FILE, 'w') as f:
for ip in ips:
f.write('/ip firewall address-list add list=%s address=%s\n' % (LIST_NAME, ip))
f.write('/ip firewall filter add chain=forward src-mac-address=%s dst-address-list=%s action=drop' % (MAC, LIST_NAME))
print('\nWrote blacklist to %s.' % OUTPUT_FILE)
print('Paste it into a Mikrotik shell session.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment