Skip to content

Instantly share code, notes, and snippets.

@iam-py-test
Created January 29, 2022 13:34
Show Gist options
  • Save iam-py-test/ba35ce681e195c690ea3590f79479a3b to your computer and use it in GitHub Desktop.
Save iam-py-test/ba35ce681e195c690ea3590f79479a3b to your computer and use it in GitHub Desktop.
A Python script to turn domains/HOSTs style blocklists into dnsmasq
import os
import sys
# check if we have the right amount of args
if len(sys.argv) != 3:
# we don't, so print a help message and exit
print("Wrong number of arguments.\nHelp: {} {} [input file] [output file]".format(sys.executable,sys.argv[0]))
sys.exit()
else:
print("Generating the dnsmasq version from domains file {}...".format(sys.argv[1]))
domainsfile = open(sys.argv[1],"r")
outputfile = open(sys.argv[2],"w")
lines = domainsfile.read().split("\n")
for line in lines:
if line.startswith("#"):
outputfile.write(line)
elif line == "":
continue
elif line.startswith("127.0.0.1") or line.startswith("0.0.0.0"):
try:
outputfile.write("server=/{}/".format(line.split(" ")[1]))
except:
# this may fail, don't crash
pass
else:
outputfile.write("server=/{}/".format(line))
outputfile.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment