switchs/main.py

44 lines
1.3 KiB
Python
Raw Normal View History

2018-05-24 17:43:42 +02:00
import time
t1 = time.time()
2018-05-24 15:27:42 +02:00
from configparser import ConfigParser
from re2oapi import Re2oAPIClient
config = ConfigParser()
config.read('config.ini')
hostname = config.get('Re2o', 'hostname')
password = config.get('Re2o', 'password')
username = config.get('Re2o', 'username')
2018-05-25 00:59:34 +02:00
host_mac_ip = {}
2018-05-24 15:27:42 +02:00
api_client = Re2oAPIClient(hostname, username, password)
2018-05-25 00:59:34 +02:00
for hmi in api_client.list_hostmacip(params={'page_size': 'all'}):
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']))
2018-05-24 17:43:42 +02:00
2018-05-25 00:59:34 +02:00
template = ("host {hostname}{extension} {{\n"
" hardware ethernet {mac_address};\n"
" fixed-address {ipv4};\n"
"}}")
for extension, hmi_list in host_mac_ip.items():
2018-05-24 17:43:42 +02:00
dhcp_leases_content = '\n\n'.join(template.format(
hostname=hostname,
2018-05-25 00:59:34 +02:00
extension=extension,
mac_address=mac_address,
ipv4=ipv4
) for hostname, mac_address, ipv4 in hmi_list)
2018-05-24 17:43:42 +02:00
2018-05-25 00:59:34 +02:00
filename = 'dhcp{extension}.list'.format(extension=extension)
2018-05-24 17:43:42 +02:00
with open(filename, 'w+') as f:
f.write(dhcp_leases_content)
print(time.time() - t1)