Verify IP Address and Host Name (VB.NET)

Robert Harris
Robert Harris used Ask the Experts™
on
Hi.  There are times when DNS and a PC name do not match, and can determine this with PING -a, for example, I ping Computer 1 and returns an IP address, then PING -a the IP address and find the IP is associated with Computer2. I once wrote a simple batch that pinged the PC by name, captured the IP, then PING -a and if the PC names don't match, there's a conflict. That worked well.

Is there an elegant way to do this type of verification in VB.net?   I have an app that depends on connectivity to a remote PC host (on an AD domain), but it fails if there's a DNS conflict.  I'd like to verify there's an actual DNS issue in VB, instead of calling on CMD to check.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Better to do an actual DNS lookup, rather than use ping.

Or if VB has no native lookup capability, run nslookup or dig for DNS lookups.

Keep in mind, if DNS ever returns incorrect results, there's likely no way to take any recovery action in your code.

This is a DNS problem which should be fixed, else your code will always work oddly... when trying to take action using the bad IP... or just trying to die when wrong IP comes in. Far better to fix the related DNS entries.
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Robert,

Welcome to EE.

Now, to answer your question, you can use the built-in DNS class : https://docs.microsoft.com/en-us/dotnet/api/system.net.dns?view=netframework-4.8

And to get all the information that you need w.r.t. to a host
Dim hostInfo As IPHostEntry = Dns.GetHostEntry("www.contoso.com")

Open in new window


Other methods you would like to check are
GetHostByAddress
GetHostByName

Let me know if you run into any issues.

Regards,
Chinmay.
Robert HarrisSERVICE DESK TECH ANALYST

Author

Commented:
Thank you both for your responses! I will look into the DNS class...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial