added master node config parse

This commit is contained in:
2021-11-29 11:06:46 -05:00
parent bd0fd22075
commit 33be97b5e8

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import os
import configparser
import requests
import urllib3
@@ -10,7 +11,8 @@ def request(resource, api_key, method='GET', data=None):
data = ''
else:
data = json.dumps(data)
url = 'https://127.0.0.1/api/v2.0/{}'.format(resource)
# https://opnsense.local/api/<module>/<controller>/<command>/[<param1>/[<param2>/...]]
url = 'https://127.0.0.1/api/{}'.format(resource)
logger.debug('Request URL: {}'.format(url))
logger.debug('Request Data: {}'.format(data))
r = requests.request(
@@ -34,3 +36,40 @@ def request(resource, api_key, method='GET', data=None):
parser = argparse.ArgumentParser(description='Get DHCP WAN after reboot if backup is down and update CARP and Gateway')
parser.add_argument('-f', '--force', action='store_true', help='Force script to run, even if backup router is online')
args = parser.parse_args()
logger = logging.getLogger(__name__)
logFormatter = logging.Formatter('%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logger.setLevel(logging.INFO)
#fileHandler = logging.FileHandler("/tmp/seafile-ldap.log")
#fileHandler.setFormatter(logFormatter)
#logger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
# get directory of script
cwd = os.path.dirname(os.path.realpath(__file__))
configPath = os.path.join(cwd, 'config.ini')
# import the config file
logger.info("Starting to read the config.ini file.")
logger.info("Using file {0}.".format(configPath))
if os.path.exists(configPath):
config = configparser.ConfigParser()
config.read(configPath)
else:
logger.critical("Unable to find/read config file. Please ensure the config.ini is in the same directory as this script and readable.")
exit(1)
masterHostname = config['Master']['Hostname']
masterIP = config['Master']['IP']
masterKey = config['Master']['Key']
masterSecret = config['Master']['Secret']
logger.info("Master Hostname: {0}".format(masterHostname))
logger.info("Master Hostname: {0}".format(masterIP))
logger.info("Master Hostname: {0}".format(masterKey))
logger.info("Master Hostname: {0}".format(masterSecret))
logger.info("Finished reading the config.ini file.")