Skip to main content
edited tags
Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327
deleted 13 characters in body; edited tags; edited title; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Trying Script to make code clean, OOPget available IP automatically

So recently iRecently I made a script to get available IP automaticly, iautomatically. I was woundering if anyone can help me and give me tips on how to make this code look better, preferably in a class and OOP. I'm gonna show the code to my boss, but i want it to look clean and nice before i do so, and hopefully learn a thing or two about writing better code.

Trying to make code clean, OOP

So recently i made a script to get available IP automaticly, i was woundering if anyone can help me and give me tips on how to make this code look better, preferably in a class and OOP. I'm gonna show the code to my boss, but i want it to look clean and nice before i do so, and hopefully learn a thing or two about writing better code.

Script to get available IP automatically

Recently I made a script to get available IP automatically. I was woundering if anyone can give me tips on how to make this code look better, preferably in a class and OOP. I'm gonna show the code to my boss, but i want it to look clean and nice before i do so, and hopefully learn a thing or two about writing better code.

Source Link

Trying to make code clean, OOP

So recently i made a script to get available IP automaticly, i was woundering if anyone can help me and give me tips on how to make this code look better, preferably in a class and OOP. I'm gonna show the code to my boss, but i want it to look clean and nice before i do so, and hopefully learn a thing or two about writing better code.

Code:

import requests
from orionsdk import SwisClient
import getpass

# Brukerinfromasjon
npm_server = 'mw-solarwinds.yy.dd'
username = 'jnk'
password = getpass.getpass()

server_navn = input('Skriv inn DNS navn: ')
dns_ip = '10.96.17.4'  # 10.96.17.5 = Felles
dns_sone = 'yy.dd'

verify = False

if not verify:
    from requests.packages.urllib3.exceptions import InsecureRequestWarning

    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

swis = SwisClient(npm_server, username, password)  # Kjører mot IPAM med brukerinformasjon

subnets = {
    'ka-windows': ['10.112.12.0', '24'],
    'ka-linux': ['10.112.10.0', '24'],
    'ka-exa-mgmt': ['10.112.26.0', '28']
}

print("Tilgjengelige subnets: ")

for i in subnets:
    print(i)

print("--------------")

found = False

while not found:
    inp = input("Skriv in Subnet: ")
    if inp in subnets:
        '''                                                                                                   
        Finner ledig IP adresse i subnet                                                                      
        '''

        sub_ip = subnets[inp][0]
        sub_cdir = subnets[inp][1]
        ipaddr = swis.invoke('IPAM.SubnetManagement', 'GetFirstAvailableIp', sub_ip, sub_cdir)
        '''                                                                                                   
        Sette DNS > IP                                                                                        
        '''
        dns = swis.invoke('IPAM.IPAddressManagement', 'AddDnsARecord', server_navn, ipaddr, dns_ip, dns_sone)
        print("IP: {} > DNS: {}".format(ipaddr, server_navn))
        found = True
    else:
        print("Det er ikke et subnet, velg en fra listen.")