Created
March 19, 2014 23:43
-
-
Save darkoperator/9654103 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Management.Automation; | |
| using System.Net; | |
| namespace IPHelper | |
| { | |
| [Cmdlet(VerbsCommon.Get,"IPList", DefaultParameterSetName = "CIDR")] | |
| public class GetIPList: PSCmdlet | |
| { | |
| private string cidr; | |
| private string startIP; | |
| private string endIP; | |
| [Parameter(Mandatory = true, | |
| Position = 0, | |
| ParameterSetName = "CIDR", | |
| ValueFromPipelineByPropertyName = true)] | |
| public string CIDR | |
| { | |
| get { return this.cidr; } | |
| set { cidr = value; } | |
| } | |
| [Parameter(Mandatory = true, | |
| Position = 0, | |
| ParameterSetName = "Range", | |
| ValueFromPipelineByPropertyName = true)] | |
| public string StartIP | |
| { | |
| get { return this.startIP; } | |
| set { startIP = value; } | |
| } | |
| [Parameter(Mandatory = true, | |
| Position = 1, | |
| ParameterSetName = "Range", | |
| ValueFromPipelineByPropertyName = true)] | |
| public string EndIP | |
| { | |
| get { return this.endIP; } | |
| set { endIP = value; } | |
| } | |
| protected override void ProcessRecord() | |
| { | |
| switch (ParameterSetName) | |
| { | |
| case "CIDR": | |
| this.GetIPRangeByCIDR(cidr); | |
| break; | |
| case "Range": | |
| this.GetIPRange(startIP, endIP); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| protected void GetIPRange(string StartIp, string EndIp) | |
| { | |
| int start = IPToInt(StartIp); | |
| int end = IPToInt(EndIp); | |
| for (int i = start; i <= end; i++) | |
| { | |
| byte[] bytes = BitConverter.GetBytes(i); | |
| WriteObject(new IPAddress(new[] { bytes[3], bytes[2], bytes[1], bytes[0] })); | |
| } | |
| } | |
| protected void GetIPRangeByCIDR(string CIDRNet) | |
| { | |
| string[] cidr = CIDRNet.Split('/'); | |
| // Parse values for network. | |
| int ip = IPToInt(cidr[0]); | |
| int bits = Convert.ToInt32(cidr[1]); | |
| int mask = ~((1 << (32 - bits)) - 1); | |
| int network = ip & mask; | |
| int broadcast = network + ~mask; | |
| int startIP = network + 1; | |
| int endIP = broadcast - 1; | |
| int usableIps = (bits > 30) ? 0 : (broadcast - network - 1); | |
| // Generate list | |
| for (int i = startIP; i <= endIP; i++) | |
| { | |
| byte[] bytes = BitConverter.GetBytes(i); | |
| WriteObject(new IPAddress(new[] { bytes[3], bytes[2], bytes[1], bytes[0] })); | |
| } | |
| } | |
| protected static int IPToInt(String IPString) | |
| { | |
| byte[] octets = IPAddress.Parse(IPString).GetAddressBytes(); | |
| int ipint = BitConverter.ToInt32(new byte[] { octets[3], octets[2], octets[1], octets[0] }, 0); | |
| return ipint; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment