Link to home
Start Free TrialLog in
Avatar of Reddgum
Reddgum

asked on

Enumeration of remote system NIC on same lan, same user id - VMWARE / Windows 2003 Server

I have a simple need to enumerate and check the status of a NIC that is hosted on a different machine from where the code is executing. The environment is Windows 2003 server - there are 4 virtual machines running on a WinXP host under VMWARE on Workstation 6.02.  

The setup is fairly straightforward - the application box (svr-03) has an app that checks the status of a named  NIC (see attached code snippet) using the System.Net.NetworkInformation name space. I actually had the code using WMI previously, but felt the native C# class is a bit cleaner than using WMI. (Of course, a working WMI example will suffice as well...)

My app checks not only ping status and response times, it also looks to see the status of the card, and it will report the number of bytes transmitted or received etc, on demand.  Trouble is, we set up the external facing NIC on a different virtual machine to act as a proxy/firewall for security reasons. My app of course can't 'see' the physical card now like it used to - so I'm asking if any experts out there have accomplished this kind of task - calling a remote server (svr-01) from svr-03 and asking for physical NIC status? The same login / password is used across all 4 machines, so permissions shouldn't be an issue.

Please just don't schlep links to MSDN - I'd like to see a _working_ code snippet.

Thanks!
protected bool isNICReady(string name) // name = winOS reported NIC name
        {
            try
            {
                NetInterfaces = NetworkInterface.GetAllNetworkInterfaces(); // must refresh to catch changes in static class - use event handler for dynamic update
                foreach (NetworkInterface NI in NetInterfaces)
                {
                    if (NI.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        if (NI.Name == name)
                        {
                            if (NI.OperationalStatus == OperationalStatus.Up)
                                return true;
                            else
                                return false;
                        }
                    }
                }
            }
            catch (Exception ex) // catch-all for debugging
            {
                MessageBox.Show("Network interface problem:" + ex.Message.ToString(),"NSTAT Error");
            }
            return false;
        }

Open in new window

Avatar of favoretti
favoretti
Flag of Netherlands image

You could use VMWare API, although as far as I know it's only Java/Perl. If I recall correctly you could poll the VMWare server for the physical server NICs and their status. Would that be an option?
Oh, forget it, I missed the fact you're running on VMWare workstation. I was talking about ESX API. My apologies. I have no clue if Workstation has a comparable API.
Avatar of Reddgum
Reddgum

ASKER

Nope - I've posted a request on the VMWARE programmers board for this problem - ESX does have the API and SDK available, but apparently the lowly Workstation users are left out in the cold. :-\
Avatar of Reddgum

ASKER

And before anyone suggests it - I've considered writing a Windows service on the remote box or using a web service, but this approach is wildly inefficient insofar as it'll take a lot more time to write and debug than if there's something native in the .NET frameworks somewhere, or in WMI that can do the same thing without building such a bridge.
ASKER CERTIFIED SOLUTION
Avatar of favoretti
favoretti
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And a practical usage scenario: :-)

//
 
// Create an instance of the class.
 
//
 
SystemInformation sysinfo = new SystemInformation();
//
 
// Set the output error stream to the standard console output.
 
//
 
sysinfo.stderr = Console.OpenStandardOutput();
//
 
// Populate the class and make sure it didn't fail.
 
//
 
if (sysinfo.Get("127.0.0.1") != 0)
{
  MessageBox.Show("Error getting system information.", "System Information");
}
else
{
  MessageBox.Show("The current OS version is " + sysinfo.OSVersion.description, 
                                                        "System Information");
}

Open in new window

Avatar of Reddgum

ASKER

Favoretti:

I'm working on a stub app to test your code - looks like a cross between native Win32 and some WMI calls :-) Personally, I love the closer to the hardware stuff myself, but what can you expect from an old school firmware hack?  :-)
Hehe, yeah, it's far away from being close to the hardware, but well... You didn't want a windows service :-D
Reddgum,

Any luck on testing this out? :) I'm curious if it worked for you.

favoretti
Avatar of Reddgum

ASKER

Hey Favoretti,

Short answer: Yes, it looks like you'll get the points on this one. Good job.

The only issue I have with WMI is that the WQL query is OS-Sensitive - different OS versions produce different results at times. So in looking at my old code, I had three different WQL queries depending on the outcome of the OS Ver query. Otherwise, once I'm done with the status byte portion, I'll be back to award and grade.
Avatar of Reddgum

ASKER

Favoretti :  

Your code got me on the right track. While not the most elegant solution (WMI & .NET aren't really designed for each other) it was what I needed and fit 85% of the problem statement. The remaining work to do was track down what query to use to get the actual NIC connection status.

In the interests of the whole developer community, I've posted the changed code that will provide the status of the network card, along with the other most important items I needed, in case someone else needs the answer at some point in time. (Which, if I keep forgetting where my %#$@ keys and cellphone are, will likely be me in the near future, because I'll probably forget!)

Anyway, thanks for your very prompt response! You get an A for that, even though I needed to look up the rest.

Cheers & Beers!

Change in method GetNetworkStatus()
 
            ManagementObjectCollection adapters;
            ManagementObjectSearcher search;
 
            //search = new ManagementObjectSearcher(scope, new ObjectQuery("Select Description, DHCPEnabled, IPAddress, DatabasePath, IPSubnet from Win32_NetworkAdapterConfiguration Where IPEnabled = True"));
            search = new ManagementObjectSearcher(scope, new ObjectQuery("select * from win32_NetworkAdapter where NetConnectionID is not null"));
            adapters = search.Get();
 
            p_adapters = new NetworkAdapter[adapters.Count];
            int i = 0;
 
            foreach (ManagementObject adapter in adapters)
            {
                p_adapters[i].description = adapter["Description"].ToString(); // System reported NIC name
                p_adapters[i].name = adapter["NetConnectionID"].ToString(); // User-defined name
                p_adapters[i].statVal = Convert.ToInt16(adapter["NetConnectionStatus"]); // Status int 0 - 12
                // convert status int to matching enum entry, then to string.
                p_adapters[i++].status = ((NetworkAdapter.statName)p_adapters[i].statVal).ToString();
            }
 
Change to struct NetworkAdapter:
 
       public struct NetworkAdapter
        {
            public IPAddresses[] networkaddress;
            public Boolean DHCPEnabled;
            public String description;
            public String name;
            public enum statName
            { Disconnected, Connecting, Connected, Disconnecting, 
                HardwareNotPresent, HardwareDisabled, HardwareMalfunction, 
                MediaDisconnected, Authenticating, AuthenticationSucceeded, 
                AuthenticationFailed, InvalidAddress, CredentialsRequired };
                                
            public String status;
            public int statVal;
        }

Open in new window

Avatar of Reddgum

ASKER

Good job, thank you for your quick response. So many little items like this one eat up much of my design and development time. You saved me a lot of searching and got me on the right track. Thanks!
Lovely! Glad I could help :)
I thought for a split second of actually searching for the right methods for you, but I'm sorry, I was at work at that time and got swamped. So I thought, better this than nothing :)