Commit c2a8d515 authored by detraz's avatar detraz

Service pour nommage des ap unifi

parent 072eb642
from configparser import ConfigParser
import socket
import subprocess
import importlib
import requests
from pymongo import MongoClient
from re2oapi import Re2oAPIClient
config = ConfigParser()
......@@ -10,40 +15,31 @@ api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username')
def regen_dhcp(api_client):
host_mac_ip = {}
for hmi in api_client.list_hostmacip():
if hmi['extension'] not in host_mac_ip.keys():
host_mac_ip[hmi['extension']] = []
host_mac_ip[hmi['extension']].append((hmi['hostname'],
hmi['mac_address'],
hmi['ipv4']))
template = ("host {hostname}{extension} {{\n"
" hardware ethernet {mac_address};\n"
" fixed-address {ipv4};\n"
"}}")
for extension, hmi_list in host_mac_ip.items():
dhcp_leases_content = '\n\n'.join(template.format(
hostname=hostname,
extension=extension,
mac_address=mac_address,
ipv4=ipv4
) for hostname, mac_address, ipv4 in hmi_list)
filename = 'dhcp{extension}.list'.format(extension=extension)
with open(filename, 'w+') as f:
f.write(dhcp_leases_content)
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
def connect_mongo():
## Connexion mongodb
client = MongoClient("mongodb://localhost:27117")
db = client.ace
device = db['device']
return device
def regen_unifi(api_client):
"""Replace name in unifi controler"""
connection = connect_mongo()
for device in api_client.list_hostmacip():
connection.find_one_and_update({'ip': str(device['ipv4'])}, {'$set': {'name': str(device['hostname'])}})
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
client_hostname = socket.gethostname().split('.', 1)[0]
for arg in sys.argv:
if arg == "--force":
regen_unifi(api_client)
for service in api_client.list_servicesregen():
if service['hostname'] == client_hostname and \
service['service_name'] == 'dhcp' and \
service['service_name'] == 'unifi-ap-names' and \
service['need_regen']:
regen_dhcp(api_client)
regen_unifi(api_client)
api_client.patch(service['api_url'], data={'need_regen': False})
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment