Add snmp-scan.py option to only add host by IP

Goal is to allow SNMP autodiscovery to only add devices by IP, and not attempt to do reverse DNS lookup.

Proof of concept tested by commenting below python code from snmp-scan.py (starting line 118 on current build 2019-07-26):

try:
    # attempt to convert IP to hostname, if anything goes wrong, just use the IP
    tmp = gethostbyaddr(ip)[0]
    if gethostbyname(tmp) == ip:  # check that forward resolves
        hostname = tmp
except (herror, gaierror):
    pass

I have little python experience, but am willing to try to implement. Steps I will be taking:

  1. add snmp-scan.py arg/switch to stop reverse DNS lookup / set “only IP” mode
  2. add config to defaults.inc.php and config.php.default to set “only IP” mode
  3. update autodiscovery scripts to use “IP mode” config (not sure where to look for this / also not sure how to handle this in crontab vs. poller service)

EDIT: If anyone has suggestions on what to name the snmp-scan.py switches, please share! I’m thinking --iponly and -i currently. -i is already used in compatibility arguments - I’m now thinking --onlyip and -o.

I’m not sure if your tried this first.
https://docs.librenms.org/Extensions/Auto-Discovery/

Discovering devices by IP

By default we don’t add devices by IP address, we look for a reverse dns name to be found and add with that. If this fails and you would like to still add devices automatically then you will need to set $config['discovery_by_ip'] = true;

I did, but from documentation and testing, that only allows adding by IP if reverse DNS fails. If reverse DNS is successful (which most of our devices have), we still only want devices to be added by IP.
I figured updating the source of the data is better than neutering our DNS network.

I have snmp-scan.py patched with the new --onlyip switch, but it seems that devices added via discovery-protocols are still added by hostname, if available. For example, when the CDP discovery module runs, the discover_new_device function (defined in ./includes/discovery/functions.inc.php) is passed the hostname of the device, and the device is added by hostname, not IP.

I’ll try to work on a way to force IP resolution for all types of discovery.

EDIT: I added a check in the discover_new_device function to see if $config['force_discovery_by_ip'] is set. If set to true, the function will set hostname to the IP, then send to addHost(). I’ll send a PR after some more testing.

You should submit a pull request in git hub help everyone. :slight_smile: