Get Network properties of machine - c#

i am looking for class that can show me all the Network properties of machine:
Ip's,
Default Gateway,
Mac Address,
Default Gateway,
Adapter Speed,
etc..
thanks

You can get this information using the NetworkInformation class.
Getting Information about IP addresses is demonstrated here: IPInterfaceProperties
To get Link speed, you can need to use Windows Management Interfaces (WMI). As a starting point for your research, you'll want to run this WMI query:
SELECT NdisLinkSpeed FROM MSNdis_LinkSpeed
Then convert it to MB/s with:
SpeedInMbps = NdisLinkSpeed / 10000

Related

Determine private resolvable Bluetooth LE address

I have written an application that uses the UWP classes to offer a GATT-Server (i. e. my laptop functions as a BLE peripheral).
I can successfully advertise, connect, and use it, but there's one thing I cannot figure out: how can I find out the current private resolvable address that the central is seeing?
My bluetooth adapter appears to have the internal address 9C:B6:D0:XX:YY:ZZ. I can find and confirm that address in various ways:
BluetoothAdapter.GetDefaultAsync().AsTask().Result.BluetoothAddress.ToString("X12") returns "9CB6D0XXYYZZ" as result in the direct window of Visual Studio.
If I add a breakpoint in any read request handler, then GattReadRequestedEventArgs.Session.DeviceId.Id is "BluetoothLE#BluetoothLE9c:b6:d0:xx:yy:zz-gg:hh:ii:jj:kk:ll".
Device Manager > Bluetooth > {Device Name} > Properties > Advanced shows 9c:b6:d0:xx:yy:zz.
I'm using the "nRF Connect" app for Android to connect to my artificial peripheral and operate on its BLE services and characteristics, and it works great. However, the address that nRF Connect shows is a different one, for example "5F:69:8A:DD:C9:ED". It also changes every time I restart the GATT-Server.
I suppose this is a private resolvable address, used to hide the actual adapter's address for privacy reasons. But I cannot seem to find a way to determine that address from the code in the GATT server. All I know is that it is neither the BluetoothAdapter.BluetoothAddress, nor part of the GattReadRequestedEventArgs.Session.DeviceId.Id, because both contain only the internal address that never changes, and the gg:hh:ii:jj:kk:ll part mentioned above is yet another address (but not the one nRF Connect shows).
Determine private resolvable Bluetooth LE address
For API Address and the Private Rotating Address. The API returns a Static Address and the advertisement contains the private Rotating Address.
There is no way in UWP to get the private rotating address and this is by design.

WiFi Direct in Windows 10 says "UnsupportedHardware" althought it should be supported

I'll try to keep it short. I've been running in Windows 10 (10130) Microsoft's WiFi Direct Services example available on GitHub , the C# one in Visual Studio 2015 RC. Now, in their Build conference they said you can run in CMD a command to know if your WiFi adapter is compatible with WiFi Direct
netsh wlan show wirelesscap
Which gives me a big YES, your wireless adapter is compatible.
Wi-Fi Direct Device : Supported
Wi-Fi Direct GO : Supported
Wi-Fi Direct Client : Supported
But when I run the mentioned sample, everything goes OK until the WiFiDirectServiceWrapper.OnAdvertisementStatusChanged(. . .) is triggered, almost inmediatly after creating the advertiser. While running the sample with the profiler attached I get this:
private void OnAdvertisementStatusChanged(WiFiDrectServiceAdvertiser sender, object args)
--
sender | {Windows.Devices.WiFiDirect.Services.WiFiDirectServiceAdvertiser}
AdvertisementStatus: Aborted
AutoAcceptSession: true
CustomServiceStatusCode: 0
DeferredSessionInfo: null
PreferGroupOwnerMode: true
PreferredConfigurationMethods : _native, can't see_
ServiceError: UnsupportedHardware
ServiceInfo: null
ServiceName: "myservice"
ServiceNamePrefixes: {System.__comObject}
ServiceStatus: Available
I really don't know how I'm supposed to depurate this, as I can't find where this Event is raised (the only references to that method are
this.advertiser.AdversitementStatusChanged += OnAdvertisementStatusChanged
this.advertiser.AdversitementStatusChanged -= OnAdvertisementStatusChanged
^ those), and I can't see the WifiDirectServiceAdvertiser implementation.
My WiFi Card is a Qualcomm Atheros AR9485, and I'm using the latest drivers available through Windows Update. In the device manager I get two Wi-Fi Direct Virtual adapters:
Microsoft Wi-Fi Direct Virtual Adapter
Microsoft Wi-Fi Direct Virtual Adapter #2
The second one is disabled sometimes (the UnsupportedHardware event is the same, enabled or disabled). For all of them I've disabled the "Allow the PC to shut down this device to save energy" option.
I've tried to compile the code to x86 and x64, but I get the same result at the same point. I've tried another two generic wireless thumbs (those USB WiFi adapters) which Windows says yes, they're compatible too with no luck.
I know Windows 10 is not final, but any help is appreciated.
It seems that the WiFiDirect capabilities are not the only ones you need to look at if the thing you want to do is advertise a service (which is actually the "new in Windows 10 thing" concerning Direct).
The command you must execute is exactly the same as before:
netsh wlan show wirelesscap
but if you want to advertise a service, the relevant capabilities are the following:
P2P Device Discovery : Supported
P2P Service Name Discovery : Supported
P2P Service Info Discovery : Supported
P2P Background Discovery : Supported
What WiFiDirect does in Windows since 8 and 8.1 is advertising the whole computer, but since 10, WiFiDirectServices advertises one or more services running simultaneously in your machine.
Lets say you are the developer of com.boardgames. If you want to host a board games competition somewhere without a WiFi hard AP, that could be the prefix of the name of all your services, so each client connects only to the one they want: for example, com.boardgames.solitaire or com.boardgames.chess instead of connecting to your machine.
Sadly, none of the computers nor WiFi antennas I've tried are compatible with P2P discoveries.

How do I associate an IP address to a network name in Windows 8 Metro

I need to get a list of all the network names that the user is connected to and the IP address of the computer on that network. I can get a list of network names using NetworkInformation.GetConnectionProfiles() and I can get a list of IP addresses using NetworkInformation.GetHostNames(), but I cannot figure out how to associate the two lists. I thought it would be through the NetworkAdapters but the IDs for those are different between the two different calls.
Does anyone know how to do this?
UPDATE: Just to clarify, I'm asking how to do this in a Metro app on Windows 8.
UPDATE 2: It turns out it was a bug in Release Preview. Now that we've switched to RTM everything worked without a single change to the existing code.
EDIT: Just found this... Query Local IP Address Looks like a cleaner way. Take your pick...
I'm gonna take a stab... hopefully gets you close. I only have one adapter that is connected on my test machine, but I think (hope) this would work in a machine with multiple adapaters.
// Get all profiles
var profiles = NetworkInformation.GetConnectionProfiles();
// filter out profiles that are not currently online
var connected = from p in profiles where p.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.None select p;
// find all hosts
var hosts = NetworkInformation.GetHostNames();
// find hosts that have an IP Address
var online = from h in hosts where h.IPInformation != null select h;
// Now loop there each online connection and match network adapter ids with hosts
foreach (var c in connected)
{
var matches = from o in online where o.IPInformation.NetworkAdapter.NetworkAdapterId == c.NetworkAdapter.NetworkAdapterId select o;
}
The only problem here is that a single physical adapter will actually show up once for its IPv4 address and once for its IPv6 address. You will have to go the extra step and correlate them together. Hopefully this is what you are looking for.
well, I don't know about c# or the .net framework, but from a windows command prompt, the command netstat -f is what you need. Perhaps you could parse that.
Update
I think I understand what you need. If you have a list of IP addresses, you can convert them to hostnames with Dns.GetHostEntry
Have a look on this article, which discusses the issue in C#.

Get mac address from IP using DHCP?

I am trying to create scripts/services that allow for waking PCs in a windows domain via WOL. Now i want to give the user the option to select an AD container as a starting point for the waking of PCs contained within. My initial thought is using DHCP as a repository to query for MAC addresses given the hostnames (which i can easily enough pull from AD given the container).
Is there a way to programmatically query the DHCP service/server, passing hostnames and recover the associated MAC addresses?
Or, is there a better/easier way to solve my problem?
This is a little bit wacky it seems that there's no way to query the DHCP server programmatically. Thanks cottsak for asking the question. I understand that the DHCP protocol doesn't have such a query, but I thought mayb the executable from Microsoft might have some way you can address it from the command line. I haven't heard anybody anywhere say that there is no such case, but it must be so.
WHOA, wait a minute... I think I found what we're looking for: NETSH. cf:
http://social.technet.microsoft.com/Forums/en/ITCG/thread/afb4be16-09bd-4260-b515-8323d85d4ccb
Where it says if you open a command prompt on the DHCP server you can run this command:
netsh dhcp server scope 192.168.1.0 show clients
and get a report such as this:
10.10.98.53 - 255.255.255.0 -00-0c-29-02-a4-09 - NEVER EXPIRES -D
10.10.98.54 - 255.255.255.0 - 00-22-19-10-29-75 -1/21/2012 8:39:25 AM -D
Yippeee! Thanks for the thread!! If it wasn't for this one, I enver would have narrowed my search to technet adn found that one.
Try dhcpexim.exe from microsoft.
or, if you prefer using pure C. DhcpEnumSubnetClientsV4
No problem; because all of the machines are in your domain you can put together a VBScript that will get the MACAddress(es) from the local machine and store it as an attribute of the computer object in Active Directory.
Here's a quick hack on how to do that (save this as a .vbs-file):
Option Explicit
Const ADS_PROPERTY_UPDATE = 2
Const COMPUTERLOCATION = "ou=Member Servers,dc=yourdomain,dc=com"
Const ATTRIBUTETOUSE = "otherTelephone"
Dim wshNetwork, strComputerName
Set wshNetwork = WScript.CreateObject("WScript.Network")
strComputerName = wshNetwork.ComputerName
Dim objWMIService, colNetCards, objComputer, objNetCard
Set objWMIService = GetObject("winmgmts:\\" & strComputerName & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Set objComputer = GetObject("LDAP://cn=" & strComputerName & "," & COMPUTERLOCATION)
For Each objNetCard in colNetCards
objComputer.PutEx ADS_PROPERTY_APPEND, ATTRIBUTETOUSE, Array(objNetCard.MACAddress)
objComputer.SetInfo
Next
Because your clients aren't all in the "Member Servers" OU above you'll need to modify the above script to include a directory search for the strComputerName do get the COMPUTERLOCATION.
When you have a working script, ask your domain administrator to put the script as a start-up script targetting the computers you need to monitor; that way it'll execute whenever a computer boots up. You can also run the script as a scheduled task to get your data from any clients that haven't rebooted or use psexec or some other way you can think of to get the data immediately. Or you can rewrite the script entirely to remote connect to all of your machines and get the data that way (which might not be possible due to local firewalls). Or you could write a small .NET console application which does the same thing, it's up to you...
Also, although there is a networkAddress-attribute defined for computer objects; by default the computer object itself does not have access to write to this property. Because start up-scripts run in the context of the SYSTEM account on the particular machine the easiest thing is to use an attribute that the computer object (SELF) has write access to. The otherTelephone-attribute is multivalued and part of the Personal-Information Property Set which all computer objects has write access to by default. If you want to use the networkAddress-attribute you need to set explicit write access to that attribute for all of your computers.
Also you need to bear in mind that storing the the MAC address in Active Directory means that all of the users in your domain will have read access to it which in turn might possibly (depending on your environment) pose a small security risk.
To do it the way the network does.
Grab SharpPcap (Pcap wrapper for C#) and WinPcap (Windows) or libpcap (*nix). Write an application that creates SNMP packets to query the ARP table on the router.
Note: The ARP (Address Resolution Protocol) table is the table containing the mapping of IP address to MAC address.
I've been thinking about implementing an example that does this lately but I don't have one to show yet. Once I do, I'll make sure it gets added to the SharpPcap examples found in the project's source tree.
You can't do that with DHCP. DHCP attributes IP from MAC, not the other way around.
ARP is what converts IP into MAC but it's the machine itself that answers ARP requests so if it's off it's obviously not gonna answer ...
I suggest you store the MAC in your AD directly (I guess AD supports custom attributes ?)
you need to use arp to get a mac adress and doing so In C is a long process.
Mac adresses are hard coded, so if you have X computers go and get X mac addresses and tie them to the AD.
Note that the computer will have to be on to request its mac address.
Finding MAC address from IP address
Yeah dun worry about it, you can pull this info directly from DHCP if the PC has a lease.
Know how you right click and add a reservation in DHCP?
Look in DHCP for the 'unique ID'. It's the MAC address, sans the colons.

Get a list of all computers on a network w/o DNS

Greetings,
I need a way (either via C# or in a .bat file) to get a list of all the computers on a given network. Normally, I use "net view", but this tends to work (from my understanding) only within your domain. I need the names (or at least the IP Addresses) of all computers available on my network.
Being able to get all computers on a domain that isn't mine (in which case I'd use WORKGROUP, or whatever the default is) would also work.
Nmap is good for this - use the -O option for OS fingerprinting and -oX "filename.xml" for output as xml that you can then parse from c#.
A suitable commandline would be (where 192.168.0.0/24 is the subnet to scan):
nmap -O -oX "filename.xml" 192.168.0.0/24
leave out the -O if you aren't interested in guessing the OS - if you just want a ping sweep use -sP, or read the docs for the myriad other options.
To expand on what Unkwntech has said -
You can also do a "broadcast" ping to avoid having to ping each IP address individually.
Immediately after than you can use "arp" to examine the ARP cache and get a list of which IP addresses are on which MAC address.
Ping everything in the rage, then you can get netbios info from the systems that respond to identify it's name.
In one of my web app I used the NetApi32 function for network browsing.
Code:
http://gist.github.com/11668

Categories