#!/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 if not "." in fqdn: fqdn += ".liebchen.fkp" 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)