1
我正在尝试编写一个扫描环绕WiFi网络并将信息转储到包含SSID和加密类型的数组中的程序。然后将SSID动态数组与静态数组进行比较,尝试将SSID匹配在一起,然后输出结果。创建一个动态数组,并与静态比较
我无法尝试创建动态数组只使用正则表达式的SSID和加密。网络转储的输出是这样的:
Interface name : Wireless Network Connection
There are 8 networks currently visible.
SSID 1 : TheChinaClub-5G
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
我试着使用SSID与下列号码作为通配符键(但不知道语法)和冒号后获取数据不包括空间。截至目前没有任何工作,除了网络转储。正则表达式似乎没有找到任何东西。我一直在使用this post作为正则表达式的模型。
下面是代码我迄今:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
namespace Rainbownetworks
{
public struct Networkarr
{
public string x, y;
public Networkarr(string SSID, string Encryption)
{
x = SSID;
y = Encryption;
}
}
class Program
{
static void Main(string[] args)
{
string[] StaticSSIDarr = { "network1", "network2", "network3" };
string[] Networkarr = { };
Process p = new Process();
p.StartInfo.FileName = "netsh.exe";
p.StartInfo.Arguments = "wlan show networks";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string results = p.StandardOutput.ReadToEnd();
string[] SSIDs = { "SSID *"};
FindSSIDs(SSIDs, results);
Console.WriteLine(results);
Console.ReadLine();
}
private static void FindSSIDs(IEnumerable<string> keywords, string source)
{
var found = new Dictionary<string, string>(10);
var keys = string.Join("|", keywords.ToArray());
var matches = Regex.Matches(source, @"(?<key>" + keys + "):",
RegexOptions.IgnoreCase);
foreach (Match m in matches)
{
var key = m.Groups["key"].ToString();
var start = m.Index + m.Length;
var nx = m.NextMatch();
var end = (nx.Success ? nx.Index : source.Length);
found.Add(key, source.Substring(start, end - start));
}
foreach (var n in found)
{
Networkarr newnetwork = new Networkarr(n.Key, n.Value);
Console.WriteLine("Key={0}, Value={1}", n.Key, n.Value);
}
}
}
}
这很好,唯一的问题是,如果SSID中有一个空格,字符串只显示网络的第一个字。 – Nick