代码之家  ›  专栏  ›  技术社区  ›  RobertPitt

C中的主机名扫描#

  •  1
  • RobertPitt  · 技术社区  · 14 年前

    我最近开始了一项新的工作,作为一名ICT技术员,我正在创建一个控制台应用程序,该应用程序将包含一些有助于我们日常工具的东西!

    我的第一个工具是网络扫描器,我们的系统目前运行在普通标签和资产标签上,但是我们找到主机名/IP地址的唯一方法是进入Windows控制台工具和nslookup,这对我来说是可以改进的。

    我想创建一个应用程序,我在其中输入一个6位数字,应用程序将搜索整个DNS以查找可能的匹配!

    Our hostsnames are like so

    ICTLN-D006609-edw.srv.internal D 006609 将是该计算机的资产标签。

    我希望将其输入到控制台应用程序中,它将搜索每个主机名,并且包含字符串中输入的资产标记的主机名将与准备好用于VNC/远程桌面的IP和完整计算机名一起返回。

    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 开源

    任何信息都将是一个巨大的帮助,因为它将解决工作中的许多问题,因为我们必须要求客户去那里我的电脑和属性等,然后读回计算机名称,使用我觉得毫无意义。

    当做。

    更新: *1 C我制作的版本: http://pastebin.com/wBWxyyuh

    2 回复  |  直到 14 年前
        1
  •  3
  •   jdmichal    14 年前

    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 }
    }
    
        2
  •  0
  •   Robert Seder    14 年前

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

    推荐文章