Tuesday, August 5, 2008

c#: nmap in c#

nmap is a very useful network discovery tool. In a recent project I needed to enumerate all windows computers in a subnet. After searching through a couple of sites, I couldn't find anything that suited my need. So I decided to build a library to use in my project.

[If all your computers are in a domain, you can very easily enumerate all computers by DirectoryServices class. However when you have domains and workgroups in your network, this may be the only way to get the machine list]

Here is how I tackled the problem.
1. Scan the entire subnet and enumerate active devices.


2. Resolve ip address to host names by our trusted System.Net.Dns namespace


3. telnet to 3389 on these enumerated devices. This will give all windows machines in the network. (check msdn for other standard windows port in case you want to use a different port)

4. Use multi-thread in the enumeration process to speed up the whole thing.I am using a utility class to hold the data for multi threading


5. Populate a struct with the name and ip value and return as a List<>
If you are going to use this a production environment, monitor the thread usage and adjust it either pulling from a config file or from registry/database.

P.S. I am using a wrong coding convention for method name here because I am extending a library that was already standardized in this coding convention

download

That's it!

-Paul