Hostname and domain name in Discovery

anders9
ServiceNow Employee
ServiceNow Employee

There were some changes made recently to the way Discovery handles hostname and domain name for a discovered device. Some customers wanted to revert back to the original way of splitting hostname and domain name so I wanted to share the part of the code in the HostnameJS script include that handles this. This code also handles hosts which have an IP address as a hostname.

In the not so distant future we will be handle this using a regular expression - so this should make it simpler.



_parseFQDN: function(sysName) {
               var parts = sysName.split('.');
               // In order to parse FQDN, the name must have at least 2 dots in it, such as mac1.snc.com <http://mac1.snc.com/>
               if (parts.length < 3)
                       return sysName;

               var hnArr = [];

               //Added to detect hostnames with IP address (not unusual for ESX hosts)
               var re = /(\d+\.\d+\.\d+\.\d+)/im;
               var matched = sysName.match(re);


               if (JSUtil.notNil(matched)) {
                       this.hostname   = sysName;
                       hostname             = sysName;
                       this.dnsDomain = "";
               } else {

                       // Removed original code due to issues where hostname includes part of the domain name
                       //for (var i=0; i <parts.length-2; i++)
                       // hnArr.push(parts<i>);
                       //var hostname = hnArr.join(".");
                       hnArr.push(parts[0]);
                       var hostname = hnArr.join(".");


                       var dnsdArr = [];
                       // Removed original line which sets the domain to the last two parts only
                       //for (var j=(parts.length-2); j<parts.length; j++)
                       for (var j=1; j<parts.length; j++)
                                 dnsdArr.push(parts[j]);

                       this.dnsDomain = dnsdArr.join(".");
               }

               return hostname;
       },


Regards,
Anders
3 REPLIES 3

doug_schulze
ServiceNow Employee
ServiceNow Employee

Nice Anders! I've always taken the "low road" and just set the trusted name source (discovery definition > properties) to WMI.. which would not include sub-domains or IP addresses.. But your awesome work above might be useful too 🙂 🙂


anders9
ServiceNow Employee
ServiceNow Employee

The script change described above is no longer required (as of Aspen) - since you are now able to determine the split between hostname and domain name using the Discovery Definition properties

The original regex:



^(.+)\.(.+?\..+?)$


To use the first part of the name (up to the first dot) as the hostname - use this regex:


^([^\.]+)\.(\S+)$


Here's a simpler version of the regex:


  1. ^(.+?)\.(.*)$

You can test that here Regex 101 Javascript Regex Tester)