lopes / netbox-scanner

A scanner util for NetBox
MIT License
164 stars 39 forks source link

Is there a way to have dns_name field populated instead of description? #28

Open sholland opened 3 years ago

sholland commented 3 years ago

Is there a way to have dns_name field populated instead of description? Or unless I am misreading this looks like its hard coded in https://github.com/lopes/netbox-scanner/blob/master/nbs/netxms.py#L37

florianow commented 3 years ago

hey, It would be better when the dns_name is set and the description is something like autodiscovered or you can decide by you own with relation you want.

i changed it in my fork but only for the nmap option and it´s also hard coded. I guess for the class NetXMS it could be similar. I extended the tuple for the possibility to add a word (i used the unknown value) as the description.

nmap.py class Nmap

import os
import xml.etree.ElementTree as ET

class Nmap(object):

    def __init__(self, path, unknown):
        self.unknown = unknown
        self.path = path
        self.hosts = list()

    def run(self):
        for f in os.listdir(self.path):
            if not f.endswith('.xml'):
                continue
            abspath = os.path.join(self.path, f)
            tree = ET.parse(abspath)
            root = tree.getroot()

            for host in root.findall('host'):
                try:
                    self.hosts.append((
                        host.find('address').attrib['addr'],
                        host.find('hostnames').find('hostname').attrib['name'],
                        self.unknown
                    ))
                except AttributeError:
                    self.hosts.append((
                        host.find('address').attrib['addr'],
                        self.unknown
                    ))

init.py function sync_host

     def sync_host(self, host):
         '''Syncs a single host to NetBox

         host: a tuple like ('10.0.0.1','Gateway')
         returns: True if syncing is good or False for errors
         '''
         try:
             nbhost = self.netbox.ipam.ip_addresses.get(address=host[0])
         except ValueError:
             logging.error(f'duplicated: {host[0]}/32')
             self.stats['errors'] += 1
             return False
         try: 
             hostindex=host[2] 
         except IndexError: 
             hostindex=host[1]

         if nbhost:
             if (self.tag in nbhost.tags):
                 if (host[1] != nbhost.description):
                     aux = nbhost.description
                     nbhost.description = hostindex
                     nbhost.dns_name=host[1]
                     nbhost.save()
                     logging.info(
                         f'updated: {host[0]}/32 "{aux}" -> "{host[1]}"')
                     self.stats['updated'] += 1
                 else:
                     logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
                     self.stats['unchanged'] += 1
             else:
                 logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
                 self.stats['unchanged'] += 1
         else:

             self.netbox.ipam.ip_addresses.create(
                 address=host[0],
                 tags=[{"name": self.tag}],
                 dns_name=host[1],
                 description=hostindex
             )
             logging.info(f'created: {host[0]}/32 "{host[1]}"')
             self.stats['created'] += 1

         return True

that's my result

grafik

@lopes I can change it for more individual Option for the nmap function, but I should extend the netbox-scanner.conf with new values for a clean style that's it also works with netxms.