开发者

Hostname scanning in C#

开发者 https://www.devze.com 2023-01-05 12:36 出处:网络
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!

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 开发者_如何学Goa 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


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 }
}


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消