Reputation: 62826
This question is related to Given a Uri value how can one check whether it refers the local machine?, which did not see a satisfactory resolution.
My need is simple, I want to discover the IP addresses (may be more than one if there are multiple network cards) as well as the host names of the local machine without making any network round-trips. Meaning only the information appearing in the local tables, no DNS queries. BTW, the domain names must be discovered as well.
On second thought, I can do with the following method:
bool IsLocalAddress(string address);
Which accepts IP, host name or host + domain name and returns true iff the address refers the local machine. Again, please take into account multiple NICs and domains.
Thanks a lot in advance.
P.S.
I prefer C# code samples.
Upvotes: 0
Views: 510
Reputation: 32392
Using Python you could do this:
import socket
socket.gethostname() # hostname
socket.gethostbyname(gethostname()) # host ip addr
A bit of googling shows that the Windows API contains two functions of the same name so any language could use similar code.
If you are writing Windows specific code, then you should use the WMI class, for instance Win32_NetworkAdapterConfiguration provides information on multiple interfaces and multiple ip addresses per interface.
Upvotes: 0