mac/whmcs/insert_whmcs.py
2020-03-01 14:47:31 +01:00

53 lines
1.5 KiB
Python

from ipaddress import IPv4Address
from sys import stderr
from pymysql import Connect
def insert_whmcs_ipv4(insert: [(IPv4Address, str, IPv4Address, int)], interface: str, db: Connect, debug: bool = False):
"""
This function insert given IPs and MACs to WHMCS
:param insert: The list of IPs, MACs to insert
:type insert: [(IPv4Address, str, IPv4Address, int)]
:param interface: The interface of IPs
:type interface: str
:param db: The database connection of WHMCS
:type db: pymysql.Connect
:param debug: Disable commit on database
:type debug: bool
"""
cursor = db.cursor()
# Get gateway
gateway = insert[0][0]
del insert[0]
# Get vlan if given
if interface[:5] == "vlan":
try:
vlan = int(interface[4:])
except ValueError:
raise ValueError("Invalid vlan !")
else:
vlan = "null"
# For every IP to insert
for i in insert:
cmd = f"INSERT INTO mg_proxmox_addon_ip (ip, type, mac_address, subnet_mask, cidr, gateway, tag) " \
f"VALUES ('{i[0]}', 'IPv4', {i[1]}, '{i[2]}', {i[3]}, '{gateway}', {vlan})"
try:
cursor.execute(cmd)
except Exception as e:
print(cmd, file=stderr)
raise e
cursor.close()
# Commit to the DB
if not debug:
try:
print("Commit to DB...")
db.commit()
except Exception as e:
raise e
else:
print("Commited to DB")