Skip to content

Instantly share code, notes, and snippets.

@mnjstwins
Created October 30, 2018 14:18
Show Gist options
  • Save mnjstwins/09a775c127ab29bceefbb2b35c3f47e3 to your computer and use it in GitHub Desktop.
Save mnjstwins/09a775c127ab29bceefbb2b35c3f47e3 to your computer and use it in GitHub Desktop.
Example code to show how to use Division42.NetworkTools.
class Program
{
static void Main(string[] args)
{
DemoPing();
//DemoTraceRoute();
//DemoPortScan();
//DemoWhois();
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
private static void DemoPing()
{
String hostName = "www.yahoo.com";
IPingManager pingManager =
new PingManager(hostName, TimeSpan.FromSeconds(2));
// Wire up the event that will be fired when either:
// we get a response, or get an error.
pingManager.PingResult += (sender, e) =>
{
String responseString;
if (e.Success)
{
responseString = String.Format(
"Reply from {0}: bytes={1} time={2}ms TTL={3}", e.Reply.Address,
e.Reply.Buffer.Length, e.Reply.RoundtripTime, e.Reply.Options.Ttl);
}
else
{
responseString = e.LastException.InnerException.Message;
}
Console.WriteLine(responseString);
};
Console.WriteLine("Pinging {0}...", hostName);
Console.WriteLine();
pingManager.Start();
// Ping for 5 seconds.
Thread.Sleep(5000);
pingManager.Stop();
}
private async static Task DemoTraceRoute()
{
String hostName = "www.yahoo.com";
TraceRouteManager traceRouteManager = new TraceRouteManager();
traceRouteManager.TraceRouteNodeFound += (sender, e) =>
{
Console.WriteLine(e.Detail.ToString());
};
traceRouteManager.TraceRouteComplete += (sender, e) =>
{
Console.WriteLine("Trace complete.");
};
Console.WriteLine("Tracing route to {0}:", hostName);
Console.WriteLine();
Console.Write(TraceRouteHopDetail.FormattedTextHeader);
Task<IEnumerable<TraceRouteHopDetail>> traceEntries =
traceRouteManager.ExecuteTraceRoute(hostName);
traceEntries.Wait();
// We dumped the results to the screen as we got them, but
// they are also available in "traceEntries.Result" now, too.
}
private static void DemoPortScan()
{
String hostName = "localhost";
PortScanManager portScanManager = new PortScanManager(hostName);
portScanManager.PortScanResult += (sender, e) =>
{
Console.WriteLine("Found port {0} ({1})", e.Port, e.PortType);
};
Console.WriteLine("Scanning ports on {0}", hostName);
Console.WriteLine();
portScanManager.Start(1, 200, PortTypes.Tcp);
Task.WaitAll(portScanManager.Tasks.ToArray());
Console.WriteLine();
Console.WriteLine("Port scan complete.");
}
private static void DemoWhois()
{
String domainName = "division42.com";
WhoisManager whoisManager = new WhoisManager();
Console.WriteLine("Looking up whois information for " + domainName);
Console.WriteLine();
String result = whoisManager.ExecuteWhoisForDomain("=" + domainName);
IEnumerable<String> whoisServers = whoisManager.FindWhoisServerInOutput(result);
if (whoisServers != null && whoisServers.Count() > 0)
{
String actualResult = whoisManager.ExecuteWhoisForDomain(domainName, whoisServers.First());
Console.WriteLine(actualResult);
}
else
{
Console.WriteLine(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment