2020-03-01 14:47:31 +01:00
|
|
|
from pymysql import connect
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from os.path import isfile
|
|
|
|
from json import dump, load
|
|
|
|
from whmcs.get_whmcs import get_whmcs_ipv4, get_whmcs_mac
|
|
|
|
from whmcs.insert_whmcs import insert_whmcs_ipv4
|
2020-03-01 17:48:44 +01:00
|
|
|
from whmcs.remove_whmcs import remove_whmcs_ipv4
|
|
|
|
from router.insert_router import insert_router_ipv4, insert_router_ipv6
|
|
|
|
from router.remove_router import remove_router_ipv4, remove_router_ipv6
|
2020-03-01 14:47:31 +01:00
|
|
|
from ip.ipv4 import ipv4
|
|
|
|
|
|
|
|
if not isfile("config.json"):
|
|
|
|
with open("config.json", "w") as config:
|
2020-03-01 17:48:44 +01:00
|
|
|
data = {"database": {"host": "", "user": "", "password": "", "name": ""},
|
|
|
|
"ssh": {"host": "", "port": 22, "user": "", "key": ""},
|
|
|
|
"interface": {"default": ""},
|
|
|
|
"IPv6": {"template": ""}}
|
2020-03-01 14:47:31 +01:00
|
|
|
dump(data, config)
|
|
|
|
print("Config file created, please fill it")
|
|
|
|
exit()
|
|
|
|
with open("config.json", "r") as config:
|
|
|
|
conf = load(config)
|
|
|
|
DB_HOST = conf["database"]["host"]
|
|
|
|
DB_USER = conf["database"]["user"]
|
|
|
|
DB_PASS = conf["database"]["password"]
|
|
|
|
DB_NAME = conf["database"]["name"]
|
|
|
|
|
|
|
|
SSH_HOST = conf["ssh"]["host"]
|
|
|
|
SSH_PORT = conf["ssh"]["port"]
|
|
|
|
SSH_USER = conf["ssh"]["user"]
|
|
|
|
SSH_KEY = conf["ssh"]["key"]
|
|
|
|
|
2020-03-01 17:48:44 +01:00
|
|
|
IPV6_TEMPLATE = conf["IPv6"]["template"]
|
|
|
|
|
2020-03-01 14:47:31 +01:00
|
|
|
DEFAULT_INTERFACE = conf["interface"]["default"]
|
|
|
|
|
|
|
|
pars = ArgumentParser()
|
|
|
|
pars.add_argument("interface", help="Interface of IPs")
|
|
|
|
pars.add_argument("prefix", help="IPs prefix")
|
2020-03-01 17:48:44 +01:00
|
|
|
pars.add_argument("-d", "--debug", help="Any consequence and verbose", action="store_true")
|
|
|
|
pars.add_argument("-v", "--verbose", help="More output", action="store_true")
|
|
|
|
pars.add_argument("--delete", help="Delete IPv4 and v6", action="store_true")
|
2020-03-01 14:47:31 +01:00
|
|
|
args = pars.parse_args()
|
|
|
|
|
|
|
|
debug = False
|
|
|
|
if args.debug:
|
|
|
|
debug = True
|
|
|
|
print("DEBUG MOD ACTICATED !")
|
2020-03-01 17:48:44 +01:00
|
|
|
if args.verbose:
|
|
|
|
print("Verbose enabled")
|
2020-03-01 14:47:31 +01:00
|
|
|
|
|
|
|
# DB connection
|
|
|
|
db = connect(DB_HOST, DB_USER, DB_PASS, DB_NAME)
|
|
|
|
|
|
|
|
ipl = get_whmcs_ipv4(db)
|
|
|
|
macl = get_whmcs_mac(db)
|
|
|
|
|
|
|
|
# Get list of ip, mac and subnet_mask to add
|
|
|
|
out = ipv4(args.prefix, ipl, macl)
|
|
|
|
|
|
|
|
# Insert the list
|
2020-03-01 17:48:44 +01:00
|
|
|
if not args.delete:
|
|
|
|
insert_whmcs_ipv4(out, args.interface, db, debug, args.verbose)
|
|
|
|
insert_router_ipv4(out, args.interface, SSH_HOST, SSH_PORT, SSH_USER, SSH_KEY, debug, args.verbose)
|
|
|
|
insert_router_ipv6(out, IPV6_TEMPLATE, args.interface, SSH_HOST, SSH_PORT, SSH_USER, SSH_KEY, debug, args.verbose)
|
|
|
|
else:
|
|
|
|
remove_whmcs_ipv4(out, db, debug, args.verbose)
|
|
|
|
remove_router_ipv4(out, SSH_HOST, SSH_PORT, SSH_USER, SSH_KEY, debug, args.verbose)
|
|
|
|
remove_router_ipv6(out, IPV6_TEMPLATE, SSH_HOST, SSH_PORT, SSH_USER, SSH_KEY, debug, args.verbose)
|