All IP addresses for a hostname

H

HiChetu

I have a host which has two NIC cards; one configured for LAN
connections, and another for external connections.

Say the ip address are
1) x.x.x.x - for LAN
2) y.y.y.y - for external

hostname = 'hichetu'

With the following code snippet...

public void TestDNS(string hostname)
{
try
{
IPHostEntry hostInfo = Dns.Resolve(hostname);
IPAddress[] address = hostInfo.AddressList;
Console.WriteLine("Host name : " + hostInfo.HostName);
Console.WriteLine("\nIP Address list :");
for(int index=0; index < address.Length; index++)
{
Console.WriteLine(address[index]);
}
}
}



....I always get


Host name : hichetu.a.b

IP Address list
x.x.x.x



Now, How do i get the other ip address y.y.y.y ?
 
P

Pete Davis

I'm not sure that you can do it the way you're trying to do it. I could be
wrong.

I actually just wrote code to do this last night, though, using WMI. I'm not
sure if this is the "best" way, but it seems to work. You can also do it
through unmanaged code, but I prefer to stick with managed code wherever
possible.

A few notes about the code below. The NetworkAdapter class simply stores the
IP address list, description, and MAC Address properties. You can save
whatever information you'd like. Search the MSDN for
Win32_NetworkAdapterConfiguration to see the available data. My code only
returns adapters that have IP addresses assigned to them.

My code is as follows:

public NetworkAdapterVector GetAdapterList()
{
ManagementClass mgmt = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mgmt.GetInstances();
NetworkAdapterVector adapters = new NetworkAdapterVector();

// Search for adapters with IP addresses
foreach(ManagementObject mob in moc)
{

string[] addresses = (string[])mob.Properties["IPAddress"].Value;
if (null == addresses)
{
continue;
}

NetworkAdapter na = new NetworkAdapter();
na.Description = (string) mob.Properties["Description"].Value;
na.MacAddress = (string) mob.Properties["MACAddress"].Value;
na.IPAddresses = addresses;
adapters.Add(na);
}
return adapters;
}
 
H

HiChetu

hi Pete Davis,

First of all, thanks for your code. It provided me with more options
to get around my problem.

But, your code returns the ip addresses of the local machine on which
the code runs. I want to get the ipaddresses of any host present in my
domain.

How do i get the ipaddresses of any hosts in my domain??


-- HiChetu


Pete Davis said:
I'm not sure that you can do it the way you're trying to do it. I could be
wrong.

I actually just wrote code to do this last night, though, using WMI. I'm not
sure if this is the "best" way, but it seems to work. You can also do it
through unmanaged code, but I prefer to stick with managed code wherever
possible.

A few notes about the code below. The NetworkAdapter class simply stores the
IP address list, description, and MAC Address properties. You can save
whatever information you'd like. Search the MSDN for
Win32_NetworkAdapterConfiguration to see the available data. My code only
returns adapters that have IP addresses assigned to them.

My code is as follows:

public NetworkAdapterVector GetAdapterList()
{
ManagementClass mgmt = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mgmt.GetInstances();
NetworkAdapterVector adapters = new NetworkAdapterVector();

// Search for adapters with IP addresses
foreach(ManagementObject mob in moc)
{

string[] addresses = (string[])mob.Properties["IPAddress"].Value;
if (null == addresses)
{
continue;
}

NetworkAdapter na = new NetworkAdapter();
na.Description = (string) mob.Properties["Description"].Value;
na.MacAddress = (string) mob.Properties["MACAddress"].Value;
na.IPAddresses = addresses;
adapters.Add(na);
}
return adapters;
}

--
http://www.petedavis.net
HiChetu said:
I have a host which has two NIC cards; one configured for LAN
connections, and another for external connections.

Say the ip address are
1) x.x.x.x - for LAN
2) y.y.y.y - for external

hostname = 'hichetu'

With the following code snippet...

public void TestDNS(string hostname)
{
try
{
IPHostEntry hostInfo = Dns.Resolve(hostname);
IPAddress[] address = hostInfo.AddressList;
Console.WriteLine("Host name : " + hostInfo.HostName);
Console.WriteLine("\nIP Address list :");
for(int index=0; index < address.Length; index++)
{
Console.WriteLine(address[index]);
}
}
}



...I always get


Host name : hichetu.a.b

IP Address list
x.x.x.x



Now, How do i get the other ip address y.y.y.y ?
 
A

Arild Bakken

You do it the same way - you just connect to the remote computer. From the
top of my head, try something like:

ManagementClass mgmt = new
ManagementClass(\\\\servername\\root\\cimv2:Win32_NetworkAdapterConfiguratio
n);


Arild

HiChetu said:
hi Pete Davis,

First of all, thanks for your code. It provided me with more options
to get around my problem.

But, your code returns the ip addresses of the local machine on which
the code runs. I want to get the ipaddresses of any host present in my
domain.

How do i get the ipaddresses of any hosts in my domain??


-- HiChetu


"Pete Davis" <[email protected]> wrote in message
I'm not sure that you can do it the way you're trying to do it. I could be
wrong.

I actually just wrote code to do this last night, though, using WMI. I'm not
sure if this is the "best" way, but it seems to work. You can also do it
through unmanaged code, but I prefer to stick with managed code wherever
possible.

A few notes about the code below. The NetworkAdapter class simply stores the
IP address list, description, and MAC Address properties. You can save
whatever information you'd like. Search the MSDN for
Win32_NetworkAdapterConfiguration to see the available data. My code only
returns adapters that have IP addresses assigned to them.

My code is as follows:

public NetworkAdapterVector GetAdapterList()
{
ManagementClass mgmt = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mgmt.GetInstances();
NetworkAdapterVector adapters = new NetworkAdapterVector();

// Search for adapters with IP addresses
foreach(ManagementObject mob in moc)
{

string[] addresses = (string[])mob.Properties["IPAddress"].Value;
if (null == addresses)
{
continue;
}

NetworkAdapter na = new NetworkAdapter();
na.Description = (string) mob.Properties["Description"].Value;
na.MacAddress = (string) mob.Properties["MACAddress"].Value;
na.IPAddresses = addresses;
adapters.Add(na);
}
return adapters;
}

--
http://www.petedavis.net
HiChetu said:
I have a host which has two NIC cards; one configured for LAN
connections, and another for external connections.

Say the ip address are
1) x.x.x.x - for LAN
2) y.y.y.y - for external

hostname = 'hichetu'

With the following code snippet...

public void TestDNS(string hostname)
{
try
{
IPHostEntry hostInfo = Dns.Resolve(hostname);
IPAddress[] address = hostInfo.AddressList;
Console.WriteLine("Host name : " + hostInfo.HostName);
Console.WriteLine("\nIP Address list :");
for(int index=0; index < address.Length; index++)
{
Console.WriteLine(address[index]);
}
}
}



...I always get


Host name : hichetu.a.b

IP Address list
x.x.x.x



Now, How do i get the other ip address y.y.y.y ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top