From 351a1e8afd722e6638050006a2e90c7a2fc38e2f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zolt=C3=A1n=20Felleg?= Date: Wed, 12 Jun 2024 12:32:52 +0200 Subject: [PATCH] Added scripts/nn2ip.py. --- scripts/nn2ip.py | 282 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100755 scripts/nn2ip.py diff --git a/scripts/nn2ip.py b/scripts/nn2ip.py new file mode 100755 index 0000000..4a217d4 --- /dev/null +++ b/scripts/nn2ip.py @@ -0,0 +1,282 @@ +#!/usr/bin/env python +# name and network to ip address + + +import sys + + +# fully qualified hostname, ip modification, network, description +fqhnimnd_tuples = [('ac.in.useribm.hu', None, 'aap', 'automation controller'), + ('ac922.in.useribm.hu', None, 'pwr', 'ac922 os port (bottom)'), + ('ac922bmc.in.useribm.hu', None, 'pwr', 'ac922 bmc port (top)'), + ('accounting.in.useribm.hu', None, 'sv', 'accounting desktop/server'), + ('ah.in.useribm.hu', None, 'aap', 'automation hub'), + ('aka2pp.in.useribm.hu', None, 'ak', 'afp2pdf plus'), + ('apcups1.in.useribm.hu', None, 'up', 'apc ups 1'), + ('apcups2.in.useribm.hu', None, 'up', 'apc ups 2'), + ('archer-ax1800-fin.in.useribm.hu', None, 'wr', 'archer ax1800 finance'), + ('archer-ax1800-tec.in.useribm.hu', None, 'wr', 'archer ax1800 techies'), + ('archer-c6-fini.in.useribm.hu', None, 'wr', 'archer c6 finance'), + ('archer-c6-teci.in.useribm.hu', None, 'wr', 'archer c6 techies'), + ('csm.in.useribm.hu', None, 'zf', 'afp2pdf plus'), + ('db2.in.useribm.hu', None, 'is', 'db2 server'), + ('db2svr.in.useribm.hu', None, 'sv', '(old) db2 server'), + ('dev.in.useribm.hu', None, 'dv', 'dvasary development server'), + ('dhcp.in.useribm.hu', None, 'is', 'dhcp server'), + ('dorado3kca.in.useribm.hu', None, 'im', 'dorado 3000 controller a'), + ('dorado3kcb.in.useribm.hu', None, 'im', 'dorado 3000 controller b'), + ('dorado3k-nfs.10g.useribm.hu', None, '10g', 'dorado 3000 nfs server'), + ('dorado3k-samba.in.useribm.hu', None, 'is', 'dorado 3000 samba server'), + ('efg.pm.useribm.hu', ('fixed', 254), 'pm', 'external firewall/gateway'), + ('ens.pm.useribm.hu', None, 'pm', 'external name server'), + ('fdc.in.useribm.hu', None, 'is', 'fedora directory client'), + ('fds.in.useribm.hu', None, 'is', 'fedora directory server'), + ('frtsmf39.in.useribm.hu', None, 'fr', 'fritter tsm fedora 39'), + ('git.pm.useribm.hu', None, 'pm', 'git server'), + ('gitea.in.useribm.hu', ('extend', 1), 'is', 'gitea server'), + ('hmc10a.in.useribm.hu', None, 'pwr', 'hmc a'), + ('hmc10b.in.useribm.hu', None, 'pwr', 'hmc b'), + ('ifg.in.useribm.hu', ('fixed', 254), 'is', 'internal firewall/gateway'), + ('ifg.pm.useribm.hu', ('fixed', 1), 'pm', 'internal firewall/gateway'), + ('kea.in.useribm.hu', None, 'is', 'dhcp server'), + ('khredmi.in.useribm.hu', None, 'mp', 'khorvath redmi mobile'), + ('kms.in.useribm.hu', None, 'is', 'key management server'), + ('log.in.useribm.hu', None, 'is', 'log server'), + ('lpar11.in.useribm.hu', ('fixed', 11), 'pwr', 'lpar 11'), + ('lpar12.in.useribm.hu', ('fixed', 12), 'pwr', 'lpar 12'), + ('lpar13.in.useribm.hu', ('fixed', 13), 'pwr', 'lpar 13'), + ('lpar14.in.useribm.hu', ('fixed', 14), 'pwr', 'lpar 14'), + ('lpar15.in.useribm.hu', ('fixed', 15), 'pwr', 'lpar 15'), + ('lpar16.in.useribm.hu', ('fixed', 16), 'pwr', 'lpar 16'), + ('lpar17.in.useribm.hu', ('fixed', 17), 'pwr', 'lpar 17'), + ('lpar18.in.useribm.hu', ('fixed', 18), 'pwr', 'lpar 18'), + ('lpar19.in.useribm.hu', ('fixed', 19), 'pwr', 'lpar 19'), + ('lpar20.in.useribm.hu', ('fixed', 20), 'pwr', 'lpar 20'), + ('lxchost.in.useribm.hu', None, 'is', 'lxc host'), + ('mx.in.useribm.hu', None, 'is', 'mail server'), + ('mx.pm.useribm.hu', None, 'pm', 'mail server'), + ('netlock.in.useribm.hu', None, 'av', 'avon netlock server'), + ('nim.in.useribm.hu', ('fixed', 3), 'pwr', 'nim server'), + ('ns1.in.useribm.hu', ('fixed', 159), 'is', 'name server 1'), + ('ns2.in.useribm.hu', ('fixed', 159), 'ib', 'name server 2'), + ('olvmengine.in.useribm.hu', None, 'olvm', 'oracle linux virtualization manager engine host'), + ('olvmhost1.in.useribm.hu', None, 'olvm', 'oracle linux virtualization manager kvm host 1'), + ('pki.in.useribm.hu', None, 'is', 'public key infrastructure server'), + ('quorum.in.useribm.hu', None, 'is', '(cluster) quorum server'), + ('pns.pm.useribm.hu', None, 'pm', 'perimeter name server'), + ('s6730-s24x6q.in.useribm.hu', None, 'ns', 'huawei s6730-s24x6q'), + ('slycpx.in.useribm.hu', None, 'is', 'sly crm proxy'), + ('slycrm.in.useribm.hu', None, 'is', 'sly crm server'), + ('sns.in.useribm.hu', None, 'is', 'source name server'), + ('stgworksa.in.useribm.hu', None, 'ss', 'storage works a'), + ('stgworksb.in.useribm.hu', None, 'ss', 'storage works b'), + ('store.10g.useribm.hu', None, '10g', 'store'), + ('store.in.useribm.hu', None, 'is', 'store'), + ('storeilo.in.useribm.hu', None, 'im', 'store ilo'), + ('t1600g-28ts-ext.in.useribm.hu', None, 'ns', 't1600g-28ts external'), + ('t1600g-28ts-mgt.in.useribm.hu', None, 'ns', 't1600g-28ts management'), + ('t1600g-28ts-nrl.in.useribm.hu', None, 'ns', 't1600g-28ts network rack lower'), + ('t1600g-28ts-nru.in.useribm.hu', None, 'ns', 't1600g-28ts network rack upper'), + ('t1600g-28ts-pwm.in.useribm.hu', None, 'ns', 't1600g-28ts power management'), + ('t1600g-28ts-pwr.in.useribm.hu', ('extend', 1), 'ns', 't1600g-28ts power'), + ('t1600g-52ts-int.in.useribm.hu', None, 'ns', 't1600g-52ts internal'), + ('time-machine.in.useribm.hu', ('extend', 1), 'is', 'time machine'), + ('ts3310.in.useribm.hu', None, 'tl', 'tape library 3310'), + ('ts4300.in.useribm.hu', None, 'tl', 'tape library 4300'), + ('tsm.10g.useribm.hu', None, '10g', 'tsm server'), + ('tsm.in.useribm.hu', None, 'is', 'tsm server'), + ('tsmilo.in.useribm.hu', None, 'im', 'tsm ilo'), + ('vcsa80.in.useribm.hu', ('fixed', 80), 'hm', 'vcenter server appliance'), + ('vhost1.10g.useribm.hu', ('fixed', 1), '10g', 'vhost1'), + ('vhost1.in.useribm.hu', ('fixed', 1), 'hm', 'vhost1'), + ('vhost1imm.in.useribm.hu', ('fixed', 1), 'im', 'vhost1 imm'), + ('vhost2.10g.useribm.hu', ('fixed', 2), '10g', 'vhost2'), + ('vhost2.in.useribm.hu', ('fixed', 2), 'hm', 'vhost2'), + ('vhost2imm.in.useribm.hu', ('fixed', 2), 'im', 'vhost2 imm'), + ('vhost3.in.useribm.hu', ('fixed', 3), 'hm', 'vhost3'), + ('vhost3imm.in.useribm.hu', ('fixed', 3), 'im', 'vhost3 imm'), + ('vhost4.10g.useribm.hu', ('fixed', 4), '10g', 'vhost4'), + ('vhost4.in.useribm.hu', ('fixed', 4), 'hm', 'vhost4'), + ('vhost4xcc.in.useribm.hu', ('fixed', 4), 'im', 'vhost4 xclarity controller'), + ('vhost5.10g.useribm.hu', ('fixed', 5), '10g', 'vhost5'), + ('vhost5.in.useribm.hu', ('fixed', 5), 'hm', 'vhost5'), + ('vhost5xcc.in.useribm.hu', ('fixed', 5), 'im', 'vhost5 xclarity controller'), + ('vios1.in.useribm.hu', ('fixed', 1), 'pwr', 'vios 1'), + ('vios2.in.useribm.hu', ('fixed', 2), 'pwr', 'vios 2'), + ('vpn.in.useribm.hu', None, 'is', 'vpn server'), + ('wdt.in.useribm.hu', None, 'is', 'webdav test server'), + ('wiki.in.useribm.hu', None, 'is', 'wiki server'), + ('ws.pm.useribm.hu', None, 'pm', 'web server'), + ('xfr.pm.useribm.hu', None, 'pm', 'transfer server'), + ('scdm.in.useribm.hu', None, 'rs', 'transfer server'), + ('zfrhel7.in.useribm.hu', None, 'zf', 'zfrhel7'), + ('zffc830.10g.useribm.hu', None, '10g', 'zffc830'), + ('zfredmi.in.useribm.hu', None, 'mp', 'zfelleg redmi mobile')] + +# network, subnet modification, description +nfsd_tuples = [('10g', None, '10g'), + ('aap', None, 'ansible automation platform'), + ('ak', None, 'akosztolanyi'), + ('av', None, 'avon'), + ('dv', None, 'dvasary'), + ('fr', None, 'fritter'), + ('fs', None, 'fschnell'), + ('hm', None, 'hypervisor management'), + ('ib', None, 'infrastructure backup servers'), + ('im', None, 'infrastructure management'), + ('is', None, 'infrastructure servers'), + ('mp', None, 'mobile phones'), + ('ms', None, 'mszabo'), + ('ns', None, 'network switches'), + ('olvm', None, 'oracle linux virtualization manager'), + ('pm', None, 'perimeter'), + ('pwr', None, 'power'), + ('rr', None, 'rrendek'), + ('rs', None, 'red hat satellite'), + ('ss', None, 'san switches'), + ('sv', None, 'servers'), + ('tl', ('extend', 1), 'tape libraries'), + ('up', None, 'upses'), + ('uv', None, 'user vpn'), + ('wr', None, 'wireless routers'), + ('zf', None, 'zfelleg')] + + +def get_ip_of_name(name, modulo=256): + name_value = 0 + for position in range(len(name)): + name_value *= 10 + name_character = name[position].lower() + if name_character in '0123456789': + character_value = int(name_character) + else: + character_value = ord(name_character) + 1 - ord('a') + name_value += character_value + return name_value % modulo + +def get_networks(nfsd_tuples): + networks = {} + subnet_addresses = set() + for (network, network_modification, description) in nfsd_tuples: + if network in networks: + raise + networks[network] = {'description': description, + 'name': network, + 'hosts': {}} + if network_modification is None: + subnet_address = get_ip_of_name(network) + else: + (modify_type, modify_value) = network_modification + if modify_type == 'fixed': + subnet_address = modify_value + elif modify_type == 'extend': + network_extension = description[:modify_value - 1] + extended_network = '.'.join((network, network_extension)) + print('Extended network: {}'.format(extended_network)) + subnet_address = get_ip_of_name(extended_network) + else: + print('Unknown network modification {}'.format(network_modification)) + sys.exit(1) + if subnet_address in subnet_addresses: + raise + subnet_addresses.add(subnet_address) + networks[network]['subnet address'] = subnet_address + return networks + +def list_ip_addresses(nfsd_tuples, fqhnimnd_tuples): + networks = get_networks(nfsd_tuples) + for (fully_qualified_hostname, + ip_modification, + network, + description) in fqhnimnd_tuples: + hostname = fully_qualified_hostname.split('.')[0] + if network not in networks: + raise + network_ip_addresses = set() + network_hosts = networks[network]['hosts'] + for network_hostname in network_hosts: + network_ip_address = network_hosts[network_hostname][0] + network_ip_addresses.add(network_ip_address) + if ip_modification is None: + ip_address = get_ip_of_name(hostname) + if ip_address in network_ip_addresses: + print('{} ({}) is a duplicate ({})'.format(hostname, + network, + ip_address)) + sys.exit(1) + else: + (modify_type, modify_value) = ip_modification + if modify_type == 'fixed': + ip_address = modify_value + elif modify_type == 'extend': + domainname = fully_qualified_hostname[len(hostname):] + hostname_extension = domainname[:modify_value] + extended_hostname = ''.join((hostname, hostname_extension)) + print('Extended hostname: {}'.format(extended_hostname)) + ip_address = get_ip_of_name(extended_hostname) + else: + print('Unknown ip modification {}'.format(ip_modification)) + sys.exit(1) + if ip_address in network_ip_addresses: + print('{} ({}) is a duplicate ({})'.format(hostname, + network, + ip_address)) + sys.exit(1) + networks[network]['hosts'][hostname] = (ip_address, description) + max_hd_length = 0 + for network in sorted(networks): + subnet_address = networks[network]['subnet address'] + hex_subnet_address = hex(subnet_address) + network_name = networks[network]['name'] + network_description = networks[network]['description'] + print('{} ({} network) ({}, {}):'.format(network_name, + network_description, + subnet_address, + hex_subnet_address)) + # ip, name, description + ipnd_list = [] + for hostname in networks[network]['hosts']: + ipnd_list.append((networks[network]['hosts'][hostname][0], + hostname, + networks[network]['hosts'][hostname][1])) + for (ip_address, hostname, description) in sorted(ipnd_list): + if description: + hd = '{} ({})'.format(hostname, description) + else: + hd = hostname + if len(hd) > max_hd_length: + max_hd_length = len(hd) + print(' {:60}: {:3} ({})'.format(hd, + ip_address, + hex(ip_address))) + print('Max HD length: {}'.format(max_hd_length)) + + +if __name__ == '__main__': + if len(sys.argv) == 1: + list_ip_addresses(nfsd_tuples, fqhnimnd_tuples) + elif len(sys.argv) == 3: + fqhnimnd_tuples = [(sys.argv[1], None, sys.argv[2], '')] + nfsd_tuples = [(sys.argv[2], None, '')] + list_ip_addresses(nfsd_tuples, fqhnimnd_tuples) + else: + print(len(sys.argv)) + + print('----------------') + net_ip = get_ip_of_name('10g', 65536) + print('10g:', net_ip, hex(net_ip)) + net_ip = get_ip_of_name('external', 65536) + print('external:', net_ip, hex(net_ip)) + net_ip = get_ip_of_name('internal', 65536) + print('internal:', net_ip, hex(net_ip)) + net_ip = get_ip_of_name('pm', 65536) + print('pm:', net_ip, hex(net_ip)) + net_ip = get_ip_of_name('perimeter', 65536) + print('perimeter:', net_ip, hex(net_ip)) + print('----------------') + vlan_ip = get_ip_of_name('hmc10a', 4096) + print('hmc10a VLAN:', vlan_ip, hex(vlan_ip)) + vlan_ip = get_ip_of_name('hmc10b', 4096) + print('hmc10b VLAN:', vlan_ip, hex(vlan_ip)) + vlan_ip = get_ip_of_name('restore test', 4096) + print('restore test VLAN:', vlan_ip, hex(vlan_ip)) -- 2.54.0