def saveToNetdot(self,netdotURL,assignedIPNet,service_description=''): ''' Saves the assigned IP to netdot database''' # login to netdot pynetdot.setup(url=netdotURL,username=self.username,password=self.password) record = pynetdot.Ipblock() record.address = assignedIPNet record.description = service_description record.status = 5 # status options # container = 0 # container = 1 # dicvovered = 2 # reserved = 4 # subnet = 5 try: saved_status = record.save() if saved_status == True: return True else: return False except requests.exceptions.HTTPError: return AttributeError except requests.exceptions.MissingSchema: return AttributeError # if the URL is not valid except AttributeError: return AttributeError # if credentials are wrong except Exception as e: return str(type(e))+'\n'+str(e)
def getFromNetdot(self,netdot_url,residentblocks): # login to netdot pynetdot.setup(url=netdot_url,username=self.username,password=self.password) # search netdot for resident block and get its used subnets usedSubnetList = [] # list to record used ip subnets from netdot try: for residentblock in residentblocks: for block in pynetdot.Ipblock.search(address=residentblock): for ip in block.children: usedSubnetList.append(ip.address+"/"+str(ip.prefix)) except requests.exceptions.MissingSchema: return AttributeError # if the URL is not valid except AttributeError: return AttributeError # if credentials are wrong except requests.exceptions.ConnectionError: return AttributeError # used to raise a dialog for connection issues except Exception as e: return e #create a list of all /30 from the resident blocks allSlash30List = [ip for residentblock in residentblocks for ip in ipaddr.IPNetwork(residentblock).subnet(new_prefix=30)] #convert list elements to ip network objects usedSubnetList = [ipaddress.ip_network(item) for item in usedSubnetList] allSlash30List = [ipaddress.ip_network(item) for item in allSlash30List] # iterate through allSlash30List to check if an item in allSlash30List # is a subnet of any item in usedSubnetList # if not, the item becomes the assigned ip for proposedIPNet in allSlash30List: for usedIPNet in usedSubnetList: if proposedIPNet.subnet_of(usedIPNet): # move to the next proposed_net break else: if str(usedIPNet) != str(usedSubnetList[-1]): # skip the used_net until the last used_net, makes sure we have scanned the proposed_net against all used_net continue else: return proposedIPNet return False # no ip address found from netdot
def __init__(self, *args, **kwargs): pynetdot.setup(*args, **kwargs)
import logging """ Insertion UPS devices into the netdot via API source file in csv format The header of the csv file must be like this: site_name;name;snmp_target;snmp_version;snmp_community;snmp_managed;entity_id pip install pynetdot ip = str(netaddr.IPAddress(ip)) """ logging.basicConfig(filename='main.log', level=logging.INFO) # Connect to netdot server pynetdot.setup( url='http://127.0.0.1/netdot', username='******', password='******') with open('ups_file', 'r') as ibp: reader = csv.DictReader(ibp, delimiter=";") for row in reader: ups_name = row['name'] snmp_ver = row['snmp_version'] snmp_target = row['snmp_target'] snmp_target = snmp_target + '/32' snmp_com = row['snmp_community'] snmp_manag = row['snmp_managed'] site_name = row['site_name'].decode('utf-8') entity_id = row['entity_id'] site = pynetdot.Site.get_first(name=site_name) site_id = site.id
#!/usr/bin/env python import pynetdot pynetdot.setup(url='https://myserver.mydomain.com/netdot/', username='******', password='******') # Get interface by name on specific device device = pynetdot.Device.get_first(name='myrouter') ifaces = pynetdot.Interface.search(name='Vl400', device=device) for i in ifaces: print i.dump() print repr(i) # Get products for Entity entity = pynetdot.Entity.get_first(name='Cisco') for p in entity.products: print p # Get IP address info ip=pynetdot.Ipblock.get_first(address='10.33.1.66') print ip.dump()
#!/usr/bin/env python import pynetdot pynetdot.setup(url='https://myserver.mydomain.com/netdot/', username='******', password='******') # Get interface by name on specific device device = pynetdot.Device.get_first(name='myrouter') ifaces = pynetdot.Interface.search(name='Vl400', device=device) for i in ifaces: print i.dump() print repr(i) # Get products for Entity entity = pynetdot.Entity.get_first(name='Cisco') for p in entity.products: print p # Get IP address info ip = pynetdot.Ipblock.get_first(address='10.33.1.66') print ip.dump()