File: //etc.defaults/ppp/pppoe-generate-config.py
#!/usr/bin/python2
# Copyright (c) 2000-2020 Synology Inc. All rights reserved.
from os import path
import ConfigParser
import StringIO
import os
import sys
import argparse
DUMMPY_SECTION="ROOT"
DEFAULT_MTU=1492
DEFAULT_MRU=1492
def parse_arguments():
parser = argparse.ArgumentParser(description="Generate PPPoE Config.")
parser.add_argument("-i", "--input", dest="input_file", required=True,
help="Input file name")
parser.add_argument("-o", "--output", dest="output_file", required=True,
help="Output file name")
parser.add_argument("-r", "--override", dest="overrides", nargs=2,
default=[], action="append", metavar=("key", "value"),
help="override value in input file.")
return parser.parse_args()
def read_config(filename):
with open(filename) as f:
fp = StringIO.StringIO()
fp.write("[{0}]\n".format(DUMMPY_SECTION))
fp.write(f.read())
fp.seek(0, os.SEEK_SET)
cp = ConfigParser.ConfigParser()
cp.optionxform = str
cp.readfp(fp)
return dict(cp.items(DUMMPY_SECTION))
def override_config(config, overrides):
override_config = dict(overrides[i][0:2] for i in range(0, len(overrides)))
config.update(override_config)
return config
def write_config(config, output_file):
with open(output_file, "w+") as f:
# default arguments.
f.write("noipdefault noauth default-asyncmap hide-password nodetach "
"noaccomp nodeflate nopcomp novj novjccomp\n")
# optional arguments
if config.get("LINUX_PLUGIN"):
f.write("plugin {0} nic-{1}".format(config["LINUX_PLUGIN"],
config["ETH"]))
if config.get("SERVICENAME"):
f.write(" rp_pppoe_service {0}".format(config["SERVICENAME"]))
if config.get("ACNAME"):
f.write(" rp_pppoe_ac {0}".format(config["ACNAME"]))
f.write("\n")
if config.get("UNIT"):
f.write("unit {0}\n".format(config["UNIT"]))
if config.get("DNSTYPE") == "SERVER":
f.write("usepeerdns\n")
if config.get("MTU"):
f.write("mtu {0}\n".format(config["MTU"]))
else:
f.write("mtu {0}\n".format(DEFAULT_MTU))
if config.get("MRU"):
f.write("mru {0}\n".format(config["MRU"]))
else:
f.write("mru {0}\n".format(DEFAULT_MRU))
if config.get("LCP_INTERVAL"):
f.write("lcp-echo-interval {0}\n".format(config["LCP_INTERVAL"]))
if config.get("LCP_FAILURE"):
f.write("lcp-echo-failure {0}\n".format(config["LCP_FAILURE"]))
if config.get("USER"):
f.write("user {0}\n".format(config["USER"]))
if config.get("DEFAULTROUTE") != "no":
f.write("defaultroute\n")
if config.get("DEMAND") != "no":
f.write("demand persist idle {0} 10.112.112.112:10.112.112.113 "
"ipcp-accept-remote ipcp-accept-local connect true "
"noipdefault ktune\n".format(config["DEMAND"]))
# extra arguments
if config.get("PPPD_EXTRA"):
f.write("{0}\n".format(config["PPPD_EXTRA"].strip("'\"")))
def generate(input_file, output_file, overrides):
config = read_config(input_file)
config = override_config(config, overrides)
write_config(config, output_file)
def main():
try:
args = parse_arguments()
generate(args.input_file, args.output_file, args.overrides)
except ValueError as e:
print('Parse values failed: {0}'.format(e))
sys.exit(1)
except Exception as e:
print("Operation failed: {0}".format(e))
sys.exit(1)
if __name__ == "__main__":
main()