views:

67

answers:

2

Heya guys

Iv'e recently started a new job as an ICT Technician and im creating an Console application which will consists of stuff that will help our daily tools!

My first tool is a Network Scanner, Our system currently runs on Vanilla and Asset tags but the only way we can find the hostname / ip address is by going into the Windows Console tools and nslookup which to me can be improved

I want to create an application in which I enter a 6 digit number and the application will search the whole DNS for a possible match!

Our hostsnames are like so

ICTLN-D006609-edw.srv.internal the d 006609 would be the asset tag for that computer.

I wish to enter that into the Console Application and it search through every hostname and the ones that contain the entered asset tag within the string will be returned along with an ip and full computer name ready for VNC / Remote Desktop.

Firstly how would I go about building this, shall i start the project of as a console app or a WPF. can you provide an example of how I can scan the hostnames via C#, or if there's an opensource C# version can you provide a link.

Any information would be a great help as it will take out alot of issues in the workpalce as we have to ask the customer to go into there My Computer adn properties etc and then read the Computer name back to use which I find pointless.

Regards.

Updates: *1 C# Version I made: http://pastebin.com/wBWxyyuh

+3  A: 

I would actually go about this with PowerShell, since automating tasks is kinda its thing. In fact, here's a PowerShell script to list out all computers visible on the network. This is easily translatable into C# if you really want it there instead.

function Find-Computer( [string]$assetTag ) {

    $searcher = New-Object System.DirectoryServices.DirectorySearcher;
    $searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry;
    $searcher.SearchScope = 'Subtree';
    $searcher.PageSize = 1000;
    $searcher.Filter = '(objectCategory=computer)';

    $results = $searcher.FindAll();
    $computers = @();
    foreach($result in $results) {
        $computers += $result.GetDirectoryEntry();
    }
    $results.Dispose(); #Explicitly needed to free resources.

    $computers |? { $_.Name -match $assetTag }
}
jdmichal
I do wish to keep it C# because firstly ive started to study it's a great little project for me to learn about the System.net namespaces. I dont think i would go with PowerShells due to the face i dont want to install them on all administrators consoles, but mearly have an exe in the shared server.
RobertPitt
Like I said, this is easily translatable. It is using .NET classes (the `System.DirectoryServices` namespace specifically) to do the actual searching. These can be used exactly the same way in .NET, and a simple translation like this would be a good way to introduce yourself to C#.
jdmichal
Yea thanks ive just built something that seems to work but i have to wait to get in work before i really test it. but can you just comment and tell me iff thi is the best way to accomplish it? Updated my thread.
RobertPitt
The code you posted in update 1 doesn't work at all for me. It only detects my own machine (when I put in a *), and it lists it 4 times. Also, it seems like the DNS lookups would be generally less efficient than the Active Directory search.
jdmichal
A: 

Here's a way you can accomplish this, although it's not the best. You might consider hitting Active Directory to find the legitimate machines on your network. The code below shows how you might resolve a machine name, and shows how to ping it:

static void Main()
{

    for (int index = 0; index < 999999; index++)
    {
        string computerName = string.Format("ICTLN-D{0:000000}-edw.srv.internal", index);
        string fqdn = computerName;

        try
        {
            fqdn = Dns.GetHostEntry(computerName).HostName;
        }
        catch (SocketException exception)
        {
            Console.WriteLine(">>Computer not found: " + computerName + " - " + exception.Message);
        }

        using (Ping ping = new Ping())
        {
            PingReply reply = ping.Send(fqdn);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine(">>Computer is alive: " + computerName);
            }
            else
            {
                Console.WriteLine(">>Computer did not respond to ping: " + computerName);
            }
        }
    }
}

Hope that helps...

Robert Seder
The problem here is that the hostnames vary such as Section-D(id)-Server-ProtectionLevel etc and they do vary alot sometimes so we need to make it more broad.
RobertPitt
You could use a similar technique to find all active machines in one subnet then, search from 192.168.1.1 to 192.168.1.254, for example. That would just be more the same above. For/Next through the range, see if you can resolve a name, and try to ping it. This isn't sure-fire, but should get you some decent results.
Robert Seder