diff --git a/PingM.sln b/PingM.sln new file mode 100644 index 0000000..89ab9f1 --- /dev/null +++ b/PingM.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PingM", "PingM\PingM.csproj", "{98EFB8A7-4BF8-4FA8-82E7-DCD622318D20}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {98EFB8A7-4BF8-4FA8-82E7-DCD622318D20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98EFB8A7-4BF8-4FA8-82E7-DCD622318D20}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98EFB8A7-4BF8-4FA8-82E7-DCD622318D20}.Release|Any CPU.ActiveCfg = Release|Any CPU + {98EFB8A7-4BF8-4FA8-82E7-DCD622318D20}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/PingM/PingM.csproj b/PingM/PingM.csproj new file mode 100644 index 0000000..9110027 --- /dev/null +++ b/PingM/PingM.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {98EFB8A7-4BF8-4FA8-82E7-DCD622318D20} + Exe + Properties + PingM + PingM + v3.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/PingM/Program.cs b/PingM/Program.cs new file mode 100644 index 0000000..2c4a413 --- /dev/null +++ b/PingM/Program.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Text; + +namespace PingM +{ + class Program + { + static void Main(string[] args) + { + // make sure all args are lowercase + string[] commandArgs = args; + for(int i = 0; i < args.Length; i++) + { + commandArgs[i] = args[i].ToLower(); + } + int hostIndex = Array.IndexOf(commandArgs, "-h"); + string[] ipMinMax; + if (args == null || args.Count() == 0 || args[0] == "" || commandArgs[0] == "/?" || commandArgs[0] == "help") + { + Console.WriteLine(""); + Console.WriteLine("PingM provides command-line ping packets that are targeted at multiple Ip Addresses"); + Console.WriteLine("Usage:"); + Console.WriteLine("PingM [/?] [Ip Address] [Ip Address]"); + Console.WriteLine(""); + Console.WriteLine("Options:"); + Console.WriteLine("\t /? or help | This page"); + Console.WriteLine("\t [IpAddress] [IpAddress] | Pings the provided range of Ip\'s"); + Console.WriteLine("\t -h | Display returned packets only"); + Console.WriteLine(""); + Console.WriteLine("Note that large ranges of Ip addresses can take a long time"); + + } + else + { + + + Console.WriteLine("A range has been supplied, verifying Ip Addresses..."); + if (Array.IndexOf(args, '-') == -1) + { + ipMinMax = args; + } + else + { + ipMinMax = args[0].Split('-'); + } + IPAddress firstIp; + IPAddress secondIp; + IPAddress.TryParse(ipMinMax[0], out firstIp); + IPAddress.TryParse(ipMinMax[1], out secondIp); + Console.WriteLine("Input addresses {0} and {1} verified successfully..", firstIp.ToString(), secondIp.ToString()); + string[] firstSegments = firstIp.ToString().Split('.'); + string[] lastSegments = secondIp.ToString().Split('.'); + + int[] seg1 = new int[2]; + int[] seg2 = new int[2]; + int[] seg3 = new int[2]; + int[] seg4 = new int[2]; + + seg1[0] = Convert.ToInt32(firstSegments[0]); + seg2[0] = Convert.ToInt32(firstSegments[1]); + seg3[0] = Convert.ToInt32(firstSegments[2]); + seg4[0] = Convert.ToInt32(firstSegments[3]); + seg1[1] = Convert.ToInt32(lastSegments[0]); + seg2[1] = Convert.ToInt32(lastSegments[1]); + seg3[1] = Convert.ToInt32(lastSegments[2]); + seg4[1] = Convert.ToInt32(lastSegments[3]); + + int seg1diff = Math.Abs(seg1[0] - seg1[1]); + int seg2diff = Math.Abs(seg2[0] - seg2[1]); + int seg3diff = Math.Abs(seg3[0] - seg3[1]); + int seg4diff = Math.Abs(seg4[0] - seg4[1]); + + long rawNumber = powerOf(seg1diff, 3) + powerOf(seg2diff, 2) + powerOf(seg3diff, 1) + seg4diff; + Console.WriteLine("Total number of addressess between {0} and {1}: {2}", firstIp, secondIp, rawNumber); + Ping controlPing = new Ping(); + PingOptions options = new PingOptions(); + string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + byte[] buffer = Encoding.ASCII.GetBytes(data); + int timeout = 120; + for (int i = 0; i < rawNumber; i++) + { + // Dns Lookup Logic + string ipCheck = seg1[0] + "." + seg2[0] + "." + seg3[0] + "." + seg4[0]; + try + { + PingReply replyData = controlPing.Send(ipCheck, timeout, buffer, options); + if (hostIndex != -1) + { + if(replyData.Status == IPStatus.Success) + { + Console.WriteLine("Pinging {0} was {1}", ipCheck, replyData.Status); + } + } + else + { + Console.WriteLine("Pinging {0} was {1}", ipCheck, replyData.Status); + } + + } + catch (ArgumentException e) + { + Console.WriteLine(e.Message); + seg4[0]++; + continue; + } + + // Increment the Ip + if (seg4[0] < 255) + { + seg4[0]++; + } + else + { + seg4[0] = 0; + if (seg3[0] < 255) + { + seg3[0]++; + } + else + { + seg3[0] = 0; + if (seg2[0] < 255) + { + seg2[0]++; + } + else + { + seg2[0] = 0; + if (seg1[0] < 255) + { + seg1[0]++; + } + else + { + throw new Exception("Segment 1 exceeded maximum value"); + } + } + } + + + } + } + } + Console.ReadLine(); + } + private static long powerOf(int inDiff, int totalSuffixBytes) + { + long returnTotal; + double tempTotal; + double tempPower; + tempPower = Math.Log(inDiff, 2); + tempTotal = Math.Pow(2, (Convert.ToDouble(totalSuffixBytes) * 8) + tempPower); + returnTotal = Convert.ToInt64(tempTotal); + return returnTotal; + } + } + +} diff --git a/PingM/Properties/AssemblyInfo.cs b/PingM/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ceb98a9 --- /dev/null +++ b/PingM/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("PingM")] +[assembly: AssemblyDescription("Ping Multiple IP Addresses")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Precipice Projects")] +[assembly: AssemblyProduct("PingM")] +[assembly: AssemblyCopyright("Copyright © Precipice Projects 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("98efb8a7-4bf8-4fa8-82e7-dcd622318d20")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]