From e822b2646f7ecf798f67a8d2c2cfeac7e44ec5b2 Mon Sep 17 00:00:00 2001 From: Markus Rosenstihl Date: Sat, 10 Feb 2024 16:45:44 +0100 Subject: [PATCH] added bacula-conf.py --- bacula-conf.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 bacula-conf.py diff --git a/bacula-conf.py b/bacula-conf.py new file mode 100644 index 0000000..5ced316 --- /dev/null +++ b/bacula-conf.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +import requests +import socket +import platform +import argparse + +parser = argparse.ArgumentParser(description='Process some integers.') +parser.add_argument('-d', '--debug', dest='debug', action='store_true', + help='debug: show what would be sent') + +args = parser.parse_args() + +hostname = socket.gethostname() +hostname = socket.getfqdn() +fqdn = socket.getfqdn() +ip = socket.gethostbyname(hostname) + +API_ENDPOINT = "http://192.168.0.140:5000/register" + + +def get_ip(): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + # doesn't even have to be reachable + s.connect(('10.255.255.255', 1)) + IP = s.getsockname()[0] + except Exception: + IP = '127.0.0.1' + finally: + s.close() + return IP + +ip = get_ip() + +if ip.startswith("127.0"): + raise ValueError("you need a fully qualified hostname") + +if platform.system() == "Windows": + os = "win" +elif platform.system() == "Linux": + os = "linux" +else: + raise ValueError + + +data = { + "client":hostname, + "os":os, + "ip":fqdn + } + +if args.debug: + print(data, "sent to:", API_ENDPOINT) +else: + r = requests.post(url = API_ENDPOINT, data = data) + print(r.text) + + if platform.system() == "Windows": + with open("C:/Program Files/Bacula/bacula-fd.conf","w") as f: + f.write(r.text) + elif platform.system() == "Linux": + with open("/etc/bacula/bacula-fd.conf","w") as f: + f.write(r.text) \ No newline at end of file