I am trying to write a program that scans the surround WiFi networks and dumps the info into an array containing the SSID and Encryption type. The SSID dynamic array would then be compared with a static array trying to match SSIDs together then outputing results.
I am having trouble trying create the dynamic array putting just the SSID and Encryption in using Regex. The output of the Network dump looks like this:
Interface name : Wireless Network Connection
There are 8 networks currently visible.
SSID 1 : TheChinaClub-5G
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
I've tried to use SSID as the key with the following number as a wildcard (but don't know the syntax) and grab the data after the colon excluding the space. As of right now nothing works except for the network dump. The regex doesn't seem to be finding anything. I've been using this post for the model of the regex.
Here is the code I have thus far:
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);
}
}
}
}