C# 仅从arp获取IP地址
我正在制作一个应用程序,对于它的一个功能,我需要获得我网络的ip地址C# 仅从arp获取IP地址,c#,bash,cmd,C#,Bash,Cmd,我正在制作一个应用程序,对于它的一个功能,我需要获得我网络的ip地址 ping 192.168.1.255&arp-a将以表格的形式列出我需要的每个ip地址: Internet Address Physical Address Type 192.168.x.x xx-xx-xx-xx-xx-xx dynamic 192.168.x.x xx-xx-xx-xx-xx-xx dynamic 代码如下所示:
ping 192.168.1.255&arp-a将以表格的形式列出我需要的每个ip地址:
Internet Address Physical Address Type
192.168.x.x xx-xx-xx-xx-xx-xx dynamic
192.168.x.x xx-xx-xx-xx-xx-xx dynamic
代码如下所示:
private void button_Click(object sender, EventArgs e)
{
string strCmdText3;
strCmdText3 = "/c ping 192.168.1.255 & arp -a";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = strCmdText3;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
//Console.WriteLine(output);
//string err = process.StandardError.ReadToEnd();
//Console.WriteLine(err);
process.WaitForExit();
show.Text = output;
}
现在我不知道的是如何只获取IP地址(将它们放入数组或其他东西中),以便以后在不同的命令中使用它们
这是怎么回事
"\r\nPinging 192.168.1.255 with 32 bytes of data:\r\nRequest timed out.\r\nRequest timed out.\r\nRequest timed out.\r\nRequest timed out.\r\n\r\nPing statistics for 192.168.1.255:\r\n Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),\r\n\r\nInterface: 10.10.10.74 --- 0x7\r\n Internet Address Physical Address Type\r\n 10.10.80.1 10-da-43-72-3a-99 dynamic \r\n 10.10.80.335 00-e0-4c-c7-44-0c dynamic \r\n 10.10.80.290 e0-ac-cb-60-88-c4 dynamic \r\n 10.10.80.25 34-02-86-a0-79-72 dynamic \r\n 10.10.80.27 00-1c-c0-8a-59-e7 dynamic \r\n
- 删除了
PING
命令,您可以重新添加,但以后可以跳过更多行
- 将返回的文本拆分为新行字符上的字符串[]
- 抓取并修剪每行的3-18个字符并放入列表
在大多数情况下,几乎不需要什么,下面是更新的代码
static void Main() {
string strCmdText3;
strCmdText3 = "/c arp -a";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = strCmdText3;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
//Console.WriteLine(output);
//string err = process.StandardError.ReadToEnd();
//Console.WriteLine(err);
process.WaitForExit();
//show.Text = output;
string[] LinesReturned = output.Split( '\n' );
List<string> ipAddresses = new List<string>();
for (int i = 3; i < LinesReturned .GetUpperBound(0); i++) {
ipAddresses.Add(LinesReturned [i].Substring(2, 15).Trim());
}
static void Main(){
字符串strCmdText3;
strCmdText3=“/c arp-a”;
流程=新流程();
process.StartInfo.FileName=“cmd.exe”;
process.StartInfo.Arguments=strCmdText3;
process.StartInfo.UseShellExecute=false;
process.StartInfo.RedirectStandardOutput=true;
process.StartInfo.RedirectStandardError=true;
process.StartInfo.CreateNoWindow=true;
process.Start();
//*读取输出(或错误)
字符串输出=process.StandardOutput.ReadToEnd();
//控制台写入线(输出);
//字符串err=process.StandardError.ReadToEnd();
//控制台写入线(错误);
process.WaitForExit();
//show.Text=输出;
字符串[]LinesReturn=output.Split('\n');
列表IP地址=新列表();
for(int i=3;i
一个确切的“字符串输出”变量示例是heplful。