使用 cmd 或 C# 列出网络上的所有计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16139625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
list all computers on a network with cmd or C#
提问by user802599
I need to get a list of all the computers on the network
我需要获取网络上所有计算机的列表
The network is split into two local networks. An admin network and an education network, and then under each of the admin and education networks there are sub nets for each campus. So there is an Admin and an Education Domain controller for each campus.
该网络分为两个本地网络。一个管理网络和一个教育网络,然后在每个管理和教育网络下都有每个校园的子网。因此,每个校园都有一个管理员和一个教育域控制器。
I can run "net view" in command prompt on each sub net to get a list of all the computers but I would like to be able to just run this on one server, or if I need to two servers (one for the admin network and one for the education network)
我可以在每个子网上的命令提示符下运行“net view”以获取所有计算机的列表,但我希望能够在一台服务器上运行它,或者如果我需要两台服务器(一台用于管理网络)和一个用于教育网络)
I know there are applications like nmap that can do this but I want to be able to use built in windows feature like "net view" or write something my self.
我知道有像 nmap 这样的应用程序可以做到这一点,但我希望能够使用内置的 Windows 功能,如“网络视图”或自己编写一些东西。
Additional Details: I have a program (written by a previous developer) that runs as a background service and on each computer that launches a popup and plays an .mp3 file when a fire alarm is set off. I want to get ride of this program and use PsExecto launch the popup and play the message instead of having the service installed on every computer. I have PsExec working but need to get a list of all the computers.
其他详细信息:我有一个程序(由以前的开发人员编写)作为后台服务运行,并在每台计算机上启动弹出窗口并在火警响起时播放 .mp3 文件。我想使用这个程序并使用PsExec启动弹出窗口并播放消息,而不是在每台计算机上安装该服务。我有 PsExec 工作,但需要获取所有计算机的列表。
回答by Butters
You could have a batch file that runs Net Viewand dumps it to a text file. From that you can do whatever you want with that data. This could run daily as a scheduled task, or manually when new machines are added.
您可以有一个运行的批处理文件Net View并将其转储到文本文件中。从那以后,您可以对这些数据做任何想做的事情。这可以作为计划任务每天运行,或者在添加新机器时手动运行。
回答by Derek
I dont know how large the network is that you work on but you could try querying Active Directory. Check this link out below for more of an idea.
我不知道您工作的网络有多大,但您可以尝试查询 Active Directory。查看下面的链接以获取更多想法。
回答by Mehdi Bugnard
Can you try this ?
你能试试这个吗?
Code C#:
代码 C#:
// Get host name from current network
String strHostName = Dns.GetHostName();
List<string> machinesName = new List<string>();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses from current network
int nIP = 0;
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
host = Dns.GetHostByAddress(ipaddress);//ip Address is to be specified machineName
//Add machine name into the list
machinesName.Add(host.HostName);
}
回答by Mike H
Find All Active/Used IP Addresses on Your Network There is a really neat way that you can quite easily find all active/used IP Addresses on your network without the need for any third party applications or worse, pinging each IP Address manually.
查找网络上所有活动/使用的 IP 地址 有一种非常简洁的方法,您可以非常轻松地查找网络上的所有活动/使用的 IP 地址,而无需任何第三方应用程序,或者更糟的是,手动 ping 每个 IP 地址。
Open the Command Prompt and type in the following:
打开命令提示符并输入以下内容:
FOR /L %i IN (1,1,254) DO ping -n 1 192.168.10.%i | FIND /i "Reply">>c:\ipaddresses.txt
FOR /L %i IN (1,1,254) DO ping -n 1 192.168.10.%i | 查找 /i "回复">>c:\ipaddresses.txt
Change 192.168.10 to match you own network.
更改 192.168.10 以匹配您自己的网络。
回答by akash ujjwal
you can try this :- using C# code:- /* must include these libraries */
你可以试试这个:- 使用 C# 代码:- /* 必须包含这些库 */
using System.Net;
using System.Net.Sockets;
private void GetIP()
{
Process netUtility = new Process();
netUtility.StartInfo.FileName = "net.exe";
netUtility.StartInfo.CreateNoWindow = true;
netUtility.StartInfo.Arguments = "view";
netUtility.StartInfo.RedirectStandardOutput = true;
netUtility.StartInfo.UseShellExecute = false;
netUtility.StartInfo.RedirectStandardError = true;
netUtility.Start();
StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream, netUtility.StandardOutput.CurrentEncoding);
string line = "";
while ((line = streamReader.ReadLine()) != null)
{
if (line.StartsWith("\"))
{
listBox1.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper());
}
}
streamReader.Close();
netUtility.WaitForExit(1000);
}
For more details, I had made a Windows chatting App where i'am using this code to get list of the system names in my network. You can check there how it works. Link is https://github.com/akki9c/wifi-chating/blob/master/chatSample/chatSample/Form1.cs
有关更多详细信息,我制作了一个 Windows 聊天应用程序,我正在使用此代码获取网络中系统名称的列表。你可以在那里检查它是如何工作的。链接是https://github.com/akki9c/wifi-chating/blob/master/chatSample/chatSample/Form1.cs

