DNS Lookup from ServiceNow

deesha
Tera Contributor

I have a script include where I am assigning a name to server before it gets to vRA for provisioning. I want to do a DNS lookup to see if a host with that name already exists or not. If not existing then only wants to move forward with provisioning a server. What options are available to do DNS lookup from SN within script include.

1 REPLY 1

Amit Gujarathi
Giga Sage
Giga Sage

Hi @deesha ,
I trust you are doing great.
To perform a DNS lookup from a ServiceNow (SN) script include, you can use the "GlideRecord" API to query a DNS server. Here's an example code snippet to achieve this:

// Create a new GlideRecord object to query the DNS server
var dnsGR = new GlideRecord('discovery_dns_server');
dnsGR.addQuery('name', 'YOUR_DNS_SERVER_NAME'); // Replace with your DNS server name
dnsGR.query();

// If DNS server found, query it for the hostname
if (dnsGR.next()) {
  var ip = gs.lookupHost(hostname);
  if (ip == null) {
    gs.log("Hostname not found in DNS");
  } else {
    gs.log("Hostname found in DNS");
  }
} else {
  gs.log("DNS server not found");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi