Created
March 17, 2024 18:33
-
-
Save nukdokplex/ca7d554a61cdf95b292ba0115a4744ef to your computer and use it in GitHub Desktop.
Generates dnsmasq config file that consists of lines like "ipset=/example.com/ipset", bases on text file you providing. Basically adds "ipset=/" + your line + "/" + $ipsetName. Supports comments "#"
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
import argparse | |
def main(input_filename: str, output_filename: str, ipset_name: str, is_file_append: bool) -> None: | |
input_file = open(input_filename, "r", encoding="utf-8") | |
output_file = open(output_filename, "a" if is_file_append else "w", encoding="utf-8") | |
while True: | |
line = input_file.readline() | |
if not line: | |
break | |
comment_start = line.find("#") | |
if comment_start != -1: | |
line = line[0:comment_start] | |
line = line.strip() | |
line = line.lower() | |
if not line: | |
continue | |
output_file.write(f"ipset=/{line}/{ipset_name}\n") | |
input_file.close() | |
output_file.close() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
prog="dnsmasq_gen_ipsets", | |
description=( | |
"Generates dnsmasq config file that consists of lines like " | |
'"ipset=/example.com/ipset", bases on text file you providing. ' | |
'Basically adds "ipset=/" + your line + "/" + $ipsetName.\n\n' | |
'Supports comments "#"' | |
), | |
epilog="Made for you by NukDokPlex" | |
) | |
parser.add_argument( | |
"-i", "--input", | |
dest="input_filename", | |
required=True, | |
help="Input file" | |
) | |
parser.add_argument( | |
"-o", "--output", | |
dest="output_filename", | |
required=True, | |
help="Output file" | |
) | |
parser.add_argument( | |
"-s", "--ipset", | |
dest="ipset_name", | |
required=True, | |
help="ipset name" | |
) | |
parser.add_argument( | |
"-a", "--append", | |
dest="is_file_append", | |
default=False, | |
action="store_true" | |
) | |
args = parser.parse_args() | |
main(**args.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment