【问题标题】:OS Information of networkclient using C#使用 C# 的网络客户端的操作系统信息
【发布时间】:2012-03-01 10:03:14
【问题描述】:

我正在开发一个扫描给定网络范围内的计算机的应用程序。从找到的客户端中,我需要获取 IP、主机名、Mac 地址、操作系统信息等。

现在,我拥有以上所有内容,但操作系统版本除外。有没有人知道我如何做到这一点?

我被困住了。

提前致谢,克里斯托夫

【问题讨论】:

    标签: c# networking operating-system


    【解决方案1】:

    使用 WMI,添加对 - System.Management dll 的引用并提供命名空间,使用以下代码和适当的参数 -

     ManagementScope scope = new ManagementScope();    
                try
                {                      
    
                    ConnectionOptions conOptions = new ConnectionOptions();  
    
                    options.Username = "<Provide username>";    
                    options.Password = "<Provide password>";    
                    options.EnablePrivileges = true;    
                    options.Authority = "ntlmdomain:<domianname>";                        
    
                    scope = new ManagementScope(@"\\<IP address/machine name>\root\CIMV2", options);
    
                    scope.Connect();                      
    
                    SelectQuery query = new SelectQuery("SELECT * FROM Win32_OperatingSystem");
    
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    
                    using (ManagementObjectCollection queryCollection = searcher.Get())
                    {    
                        foreach (ManagementObject m in queryCollection)
                        {                            
    
                            Console.WriteLine(string.Format("Computer Name : {0}", m["csname"]));    
                             Console.WriteLine(string.Format("Windows Directory : {0}", m["WindowsDirectory"]));    
                             Console.WriteLine(string.Format("Operating System: {0}", m["Caption"]));    
                             Console.WriteLine(string.Format("Version: {0}", m["Version"]);
    
                             Console.WriteLine(string.Format("Manufacturer : {0}", m["Manufacturer"]));
    
                        }
    
                    }
    
                }    
    
                catch (Exception ex)
                {
    
    
                }
    

    您必须具有访问权限才能窃取此信息,否则您将获得访问权限异常。

    【讨论】:

    • 感谢大家的解决方案,但 Nmap 并没有那么顺利。WMI 解决方案也没有,因为它需要凭据才能登录机器。重点是可以从插入网络的笔记本电脑上运行的应用程序,该应用程序应该扫描给定范围并显示在网络中找到的计算机。所以我认为没有解决方案?因为获得凭证是不可能的。不管怎么说,还是要谢谢你!克里斯托夫,
    【解决方案2】:

    您可以使用来自System.DiagnosticsProcess 类运行Nmap 并解析结果:

    var process = new Process()
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "cmd.exe",
            Arguments = "/c nmap -O -v targethost",
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardOutput = true
        }
    };
    process.Start();
    
    while (!process.StandardOutput.EndOfStream)
    {
        string line = process.StandardOutput.ReadLine();
        // here you can parse to obtain the operating system
    }
    

    在 C# 中创建自己的操作系统检测器会很困难,但如果您对它的工作原理有兴趣,可以在此 Nmap 章节中找到它:Chapter 8. Remote OS Detection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2013-05-17
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      相关资源
      最近更新 更多