Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sholland opened this issue Jan 19, 2021 · 1 comment
Open

Comments

@sholland
Copy link

sholland commented Jan 19, 2021

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants