Created
January 26, 2016 17:57
-
-
Save ayende/a30cb555a47892c866a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text.RegularExpressions; | |
using System.Threading; | |
namespace ConsoleApplication8 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Timer timer = new Timer(async state => | |
{ | |
try | |
{ | |
var addresses = await Dns.GetHostAddressesAsync("time.nasa.gov"); | |
var endPoint = new IPEndPoint(addresses[0], 123); | |
using (var udpClient = new UdpClient()) | |
{ | |
udpClient.Connect(endPoint); | |
udpClient.Client.ReceiveTimeout = 100; | |
udpClient.Client.SendTimeout = 100; | |
await udpClient.SendAsync(new byte[] {12, 32, 43}, 3); | |
await udpClient.ReceiveAsync(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
Environment.Exit(-1); | |
} | |
}); | |
timer.Change(500, 500); | |
int i = 0; | |
while (true) | |
{ | |
Thread.Sleep(500); | |
var grouped = GetNetStatPorts().GroupBy(x=>x.process_name) | |
.Select(x=>new {Process = x.Key , Count = x.Count()}) | |
.OrderBy(x=>x.Process) | |
.ToArray(); | |
Console.Clear(); | |
foreach (var port in grouped) | |
{ | |
Console.WriteLine(port); | |
} | |
Console.WriteLine(++i); | |
} | |
} | |
public static List<Port> GetNetStatPorts() | |
{ | |
var Ports = new List<Port>(); | |
try | |
{ | |
using (Process p = new Process()) | |
{ | |
ProcessStartInfo ps = new ProcessStartInfo(); | |
ps.Arguments = "-anop udp"; | |
ps.FileName = "netstat.exe"; | |
ps.UseShellExecute = false; | |
ps.WindowStyle = ProcessWindowStyle.Hidden; | |
ps.RedirectStandardInput = true; | |
ps.RedirectStandardOutput = true; | |
ps.RedirectStandardError = true; | |
p.StartInfo = ps; | |
p.Start(); | |
StreamReader stdOutput = p.StandardOutput; | |
StreamReader stdError = p.StandardError; | |
string content = stdOutput.ReadToEnd() + stdError.ReadToEnd(); | |
string exitStatus = p.ExitCode.ToString(); | |
if (exitStatus != "0") | |
{ | |
// Command Errored. Handle Here If Need Be | |
} | |
//Get The Rows | |
string[] rows = Regex.Split(content, "\r\n"); | |
foreach (string row in rows) | |
{ | |
//Split it baby | |
string[] tokens = Regex.Split(row, "\\s+"); | |
if (tokens.Length > 4 && (tokens[1].Equals("UDP") || tokens[1].Equals("TCP"))) | |
{ | |
string localAddress = Regex.Replace(tokens[2], @"\[(.*?)\]", "1.1.1.1"); | |
Ports.Add(new Port | |
{ | |
protocol = localAddress.Contains("1.1.1.1") ? String.Format("{0}v6", tokens[1]) : String.Format("{0}v4", tokens[1]), | |
port_number = localAddress.Split(':')[1], | |
process_name = tokens[1] == "UDP" ? LookupProcess(Convert.ToInt16(tokens[4])) : LookupProcess(Convert.ToInt16(tokens[5])) | |
}); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
return Ports; | |
} | |
public static string LookupProcess(int pid) | |
{ | |
string procName; | |
try { procName = Process.GetProcessById(pid).ProcessName; } | |
catch (Exception) { procName = "-"; } | |
return procName; | |
} | |
// =============================================== | |
// The Port Class We're Going To Create A List Of | |
// =============================================== | |
public class Port | |
{ | |
public override string ToString() | |
{ | |
return string.Format("{0} ({1} port {2})", this.process_name, this.protocol, this.port_number); | |
} | |
public string port_number { get; set; } | |
public string process_name { get; set; } | |
public string protocol { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment