1

In a domain environment i have several clients which reference a server via an alias in the local hosts file. I need to obtain the real hostname of the server. When using Net.Dns.GetHostEntry or Net.Dns.Resolve only the alias name from the hosts file is returned.

var addr = System.Net.Dns.GetHostAddresses("FileServer")[0];  // assume at least one entry
var fqdn = System.Net.Dns.GetHostEntry(addr).HostName;

The reverse lookup using NSLOOKUP works as expected. Is it possible to force the Dns methods to ignore the hosts file?

matandra
  • 91
  • 4
  • Does this answer your question? [Is it possible to set custom DNS resolver in C#'s HttpClient](https://stackoverflow.com/questions/58547451/is-it-possible-to-set-custom-dns-resolver-in-cs-httpclient) – Charlieface Jan 25 '21 at 22:27
  • No, I do not think so. This solution allows to use a custom resolver but the resolving logic has still to be implemented. – matandra Jan 26 '21 at 15:21

1 Answers1

0

You can do this calling NSLOOKUP on C# Here is a method for c#.

        /// <summary>
        /// Get HostName from IPv4
        /// </summary>
        /// <param name="ipv4"></param>
        /// <returns></returns>
        public static string NslookupIP(string ipv4, string dnsServerIpv4)
        {

            if (IPAddress.TryParse(ipv4, out IPAddress ipaddress))
            {
                if (ipaddress.AddressFamily != AddressFamily.InterNetwork)
                {
                    throw new Exception("ipv4 is no a valid IPv4");
                }

            }
            else
            {
                throw new Exception("ipv4 is no a valid IPv4");
            }

            Process pProcess = new Process();
            pProcess.StartInfo.FileName = "nslookup";
            pProcess.StartInfo.Arguments = $"{ipv4}";
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.CreateNoWindow = true;
            pProcess.Start();
            string cmdOutput = pProcess.StandardOutput.ReadToEnd();
            string pattern = @"^Name:\s*(?<host>(.*))$";

            foreach (Match m in Regex.Matches(cmdOutput, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))
            {
                    return m.Groups["host"].Value;
            }

            return "";
        }
Rui Caramalho
  • 455
  • 8
  • 16