<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indented! &#187; subnet</title>
	<atom:link href="http://www.indented.co.uk/index.php/tag/subnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indented.co.uk</link>
	<description></description>
	<lastBuildDate>Mon, 17 Oct 2011 19:03:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>More IPv4 subnet maths with PowerShell</title>
		<link>http://www.indented.co.uk/index.php/2011/10/13/more-ipv4-subnet-maths-with-powershell/</link>
		<comments>http://www.indented.co.uk/index.php/2011/10/13/more-ipv4-subnet-maths-with-powershell/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 11:55:19 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[subnet]]></category>
		<category><![CDATA[Subnet Math]]></category>

		<guid isPermaLink="false">http://www.indented.co.uk/?p=1621</guid>
		<description><![CDATA[I&#8217;ve been extended and upgrading my library of network functions for some time now, I thought I&#8217;d post the functions that work directly with my original collection. These functions are also included in my NetShell module, I need to post an update to that very soon as well. ConvertTo-HexIP Sometimes being able to convert to [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been extended and upgrading my library of network functions for some time now, I thought I&#8217;d post the functions that work directly with my <a href='http://www.indented.co.uk/index.php/2010/01/23/powershell-subnet-math/'>original collection</a>.<br />
<span id="more-1621"></span><br />
These functions are also included in my NetShell module, I need to post an update to that very soon as well.</p>
<h3>ConvertTo-HexIP</h3>
<p>Sometimes being able to convert to a hexadecimal format is useful, this little function does exactly that. The body is very simple; it uses the built in formatters.</p>
<pre class="brush: powershell; title: ; notranslate">
Function ConvertTo-HexIP {
  &lt;#
    .Synopsis
      Converts a dotted decimal IP address into a hexadecimal string.
    .Description
      ConvertTo-HexIP takes a dotted decimal IP and returns a single hexadecimal string value.
    .Parameter IPAddress
      An IP Address to convert.
  #&gt;

  [CmdLetBinding()]
  Param(
    [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
    [Net.IPAddress]$IPAddress
  )

  Process {
    &quot;$($IPAddress.GetAddressBytes() | ForEach-Object { '{0:x2}' -f $_ })&quot; -Replace '\s'
  }
}
</pre>
<h3>ConvertFrom-HexIP</h3>
<p>And back again, using System.Convert.ToUInt32 with a base-16 value to get to UInt32, then a call to ConvertTo-DottedDecimalIP (from the original set).</p>
<pre class="brush: powershell; title: ; notranslate">
Function ConvertFrom-HexIP {
  &lt;#
    .Synopsis
      Converts a hexadecimal IP address into a dotted decimal string.
    .Description
      ConvertFrom-HexIP takes a hexadecimal string and returns a dotted decimal IP address.
    .Parameter IPAddress
      An IP Address to convert.
  #&gt;

  [CmdLetBinding()]
  Param(
    [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
    [ValidatePattern('^[0-9a-f]{8}$')]
    [String]$IPAddress
  )

  Process {
    ConvertTo-DottedDecimalIP ([Convert]::ToUInt32($IPAddress, 16))
  }
}
</pre>
<h3>Get-NetworkSummary</h3>
<p>Get-NetworkSummary gets a bit of an improvement.</p>
<pre class="brush: powershell; title: ; notranslate">
Function Get-NetworkSummary {
  &lt;#
    .Synopsis
      Generates a summary of a network range
    .Description
      Get-NetworkSummary uses most of the IP conversion CmdLets to provide a summary of a network
      range from any IP address in the range and a subnet mask.
    .Parameter IPAddress
      Any IP address within the network range.
    .Parameter Network
      A network description in the format 1.2.3.4/24
    .Parameter SubnetMask
      The subnet mask for the network.
  #&gt;

  [CmdLetBinding(DefaultParameterSetName = &quot;IPAndMask&quot;)]
  Param(
    [Parameter(Mandatory = $True, Position = 0, ParameterSetName = &quot;IPAndMask&quot;, ValueFromPipeline = $True)]
    [Net.IPAddress]$IPAddress,

    [Parameter(Mandatory = $True, Position = 1, ParameterSetName = &quot;IPAndMask&quot;)]
    [Alias(&quot;Mask&quot;)]
    [Net.IPAddress]$SubnetMask,

    [Parameter(Mandatory = $True, ParameterSetName = &quot;CIDRNotation&quot;, ValueFromPipeline = $True)]
    [ValidatePattern('^(\d{1,3}\.){3}\d{1,3}[\\/]\d{1,2}$')]
    [String]$Network
  )

  Process {
    If ($PsCmdLet.ParameterSetName -eq 'CIDRNotation') {
      $Temp = $Network.Split(&quot;\/&quot;)
      $IPAddress = $Temp[0]
      $SubnetMask = ConvertTo-Mask $Temp[1]
    }

    $DecimalIP = ConvertTo-DecimalIP $IPAddress
    $DecimalMask = ConvertTo-DecimalIP $SubnetMask
    $DecimalNetwork =  $DecimalIP -BAnd $DecimalMask
    $DecimalBroadcast = $DecimalIP -BOr ((-BNot $DecimalMask) -BAnd [UInt32]::MaxValue)

    $NetworkSummary = New-Object PSObject -Property @{
      &quot;NetworkAddress&quot;   = (ConvertTo-DottedDecimalIP $DecimalNetwork);
      &quot;NetworkDecimal&quot;   = $DecimalNetwork
      &quot;BroadcastAddress&quot; = (ConvertTo-DottedDecimalIP $DecimalBroadcast);
      &quot;BroadcastDecimal&quot; = $DecimalBroadcast
      &quot;Mask&quot;             = $SubnetMask;
      &quot;MaskLength&quot;       = (ConvertTo-MaskLength $SubnetMask);
      &quot;MaskHexadecimal&quot;  = (ConvertTo-HexIP $SubnetMask);
      &quot;HostRange&quot;        = &quot;&quot;;
      &quot;NumberOfHosts&quot;    = ($DecimalBroadcast - $DecimalNetwork - 1);
      &quot;Class&quot;            = &quot;&quot;;
      &quot;IsPrivate&quot;        = $False}

    If ($NetworkSummary.MaskLength -lt 31) {
      $NetworkSummary.HostRange = [String]::Format(&quot;{0} - {1}&quot;,
        (ConvertTo-DottedDecimalIP ($DecimalNetwork + 1)),
        (ConvertTo-DottedDecimalIP ($DecimalBroadcast - 1)))
    }

    Switch -RegEx ($(ConvertTo-BinaryIP $IPAddress)) {
      &quot;^1111&quot;              { $NetworkSummary.Class = &quot;E&quot; }
      &quot;^1110&quot;              { $NetworkSummary.Class = &quot;D&quot; }
      &quot;^11000000.10101000&quot; { $NetworkSummary.Class = &quot;C&quot;; $NetworkSummary.IsPrivate = $True }
      &quot;^110&quot;               { $NetworkSummary.Class = &quot;C&quot; }
      &quot;^10101100.0001&quot;     { $NetworkSummary.Class = &quot;B&quot;; $NetworkSummary.IsPrivate = $True }
      &quot;^10&quot;                { $NetworkSummary.Class = &quot;B&quot; }
      &quot;^00001010&quot;          { $NetworkSummary.Class = &quot;A&quot;; $NetworkSummary.IsPrivate = $True }
      &quot;^0&quot;                 { $NetworkSummary.Class = &quot;A&quot; }
    }   

    Return $NetworkSummary
  }
}
</pre>
<h3>Get-NetworkRange</h3>
<p>And so does Get-NetworkRange.</p>
<pre class="brush: powershell; title: ; notranslate">
Function Get-NetworkRange {
  &lt;#
    .Synopsis
      Generates IP addresses within the specified network.
    .Description
      Get-NetworkRange finds the network and broadcast address as decimal values then starts a
      counter between the two, returning Net.IPAddress for each.
    .Parameter IPAddress
      Any IP address within the network range.
    .Parameter Network
      A network description in the format 1.2.3.4/24
    .Parameter SubnetMask
      The subnet mask for the network.
  #&gt;

  [CmdLetBinding(DefaultParameterSetName = &quot;IPAndMask&quot;)]
  Param(
    [Parameter(Mandatory = $True, Position = 0, ParameterSetName = &quot;IPAndMask&quot;, ValueFromPipeline = $True)]
    [Net.IPAddress]$IPAddress, 

    [Parameter(Mandatory = $True, Position = 1, ParameterSetName = &quot;IPAndMask&quot;)]
    [Alias(&quot;Mask&quot;)]
    [Net.IPAddress]$SubnetMask,

    [Parameter(Mandatory = $True, ParameterSetName = &quot;CIDRNotation&quot;, ValueFromPipeline = $True)]
    [ValidatePattern('^(\d{1,3}\.){3}\d{1,3}[\\/]\d{1,2}$')]
    [String]$Network
  )

  Process {
    If ($PsCmdLet.ParameterSetName -eq 'CIDRNotation') {
      $Temp = $Network.Split(&quot;\/&quot;)
      $IPAddress = $Temp[0]
      $SubnetMask = ConvertTo-Mask $Temp[1]
    }

    $DecimalIP = ConvertTo-DecimalIP $IPAddress
    $DecimalMask = ConvertTo-DecimalIP $SubnetMask

    $DecimalNetwork = $DecimalIP -BAnd $DecimalMask
    $DecimalBroadcast = $DecimalIP -BOr ((-BNot $DecimalMask) -BAnd [UInt32]::MaxValue)

    For ($i = $($DecimalNetwork + 1); $i -lt $DecimalBroadcast; $i++) {
      ConvertTo-DottedDecimalIP $i
    }
  }
}
</pre>
<h3>Get-Subnets</h3>
<p>I ran into a need to calculate a list of subnets of a specific length within a super-net, this function does just that.</p>
<pre class="brush: powershell; title: ; notranslate">
Function Get-Subnets {
  &lt;#
    .Synopsis
      Generates a list of subnets for a given network range
    .Description
      Generates a list of subnets for a given network range using either the address class or a
      user-specified value.
    .Parameter NetworkAddress
      Any address in the supernet range.
    .Parameter SubnetMask
      The desired mask, determines the size of the resulting subnet. Must be a valid subnet mask.
    .Parameter SupernetLength
      By default Get-Subnets uses the address class to determine the size of the supernet. Where the
      supernet describes the range of addresses being split.
  #&gt;

  [CmdLetBinding(DefaultParameterSetName = &quot;PS1&quot;)]
  Param(
    [Parameter(Mandatory = $True, Position = 0, ParameterSetName = &quot;IPAndMask&quot;, ValueFromPipeline = $True)]
    [Net.IPAddress]$IPAddress, 

    [Parameter(Mandatory = $True, Position = 1, ParameterSetName = &quot;IPAndMask&quot;)]
    [Alias(&quot;Mask&quot;)]
    [Net.IPAddress]$SubnetMask,

    [Parameter(Mandatory = $True, ParameterSetName = &quot;CIDRNotation&quot;, ValueFromPipeline = $True)]
    [ValidatePattern('^(\d{1,3}\.){3}\d{1,3}[\\/]\d{1,2}$')]
    [String]$Network,

    [ValidateRange(1, 32)]
    [UInt32]$SupernetLength
  )

  Process {
    If ($PsCmdLet.ParameterSetName -eq 'CIDRNotation') {
      $Temp = $Network.Split(&quot;\/&quot;)
      $IPAddress = $Temp[0]
      $SubnetMask = ConvertTo-Mask $Temp[1]
    } Else {
      $SubnetLength = ConvertTo-MaskLength $SubnetMask
    }

    If (!$SupernetLength) {
      $SupernetLength = Switch -RegEx ($(ConvertTo-BinaryIP $IPAddress)) {
        &quot;^110&quot;  { 24 }
        &quot;^10&quot;   { 16 }
        &quot;^0&quot;    { 8 }
        default { 24 }
      }
    }
    If ($SupernetLength -gt $SubnetLength) {
      Write-Error &quot;Subnet is larger than supernet. Aborting&quot;
    }
    $SupernetMask = ConvertTo-Mask $SupernetLength

    $NumberOfNets = [Math]::Pow(2, ($SubnetLength - $SupernetLength))
    $NumberOfAddresses = [Math]::Pow(2, (32 - $SubnetLength))

    $SupernetAddress = Get-NetworkAddress $IPAddress $SupernetMask
    $DecimalAddress = ConvertTo-DecimalIP $SupernetAddress

    For ($i = 0; $i -lt $NumberOfNets; $i++) {
      $NetworkAddress = ConvertTo-DottedDecimalIP $DecimalAddress 

      &quot;&quot; | Select-Object @{n='NetworkAddress';e={ $NetworkAddress }},
        @{n='BroadcastAddress';e={ Get-BroadcastAddress $NetworkAddress $SubnetMask }},
        @{n='SubnetMask';e={ $SubnetMask }},
        @{n='HostAddresses';e={
          $NumberOfHosts = $NumberOfAddresses - 2
          If ($NumberOfHosts -lt 0) { 0 } Else { $NumberOfHosts } }}

      $DecimalAddress += $NumberOfAddresses
    }
  }
}
</pre>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2011/10/13/more-ipv4-subnet-maths-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NetShell</title>
		<link>http://www.indented.co.uk/index.php/2010/11/25/netshell/</link>
		<comments>http://www.indented.co.uk/index.php/2010/11/25/netshell/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 19:56:50 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[DHCP]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[subnet]]></category>
		<category><![CDATA[Subnet Math]]></category>
		<category><![CDATA[SysLog]]></category>

		<guid isPermaLink="false">http://www.indented.co.uk/?p=1561</guid>
		<description><![CDATA[It&#8217;s been a while since I&#8217;ve posted, and since it&#8217;s almost Christmas I thought I&#8217;d better get on with it. Without further ado I want to post NetShell (I&#8217;m not very good at coming up with imaginative names). NetShell is a collection of 17 functions and a few supporting functions in a script module. Download [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I&#8217;ve posted, and since it&#8217;s almost Christmas I thought I&#8217;d better get on with it.</p>
<p>Without further ado I want to post NetShell (I&#8217;m not very good at coming up with imaginative names). NetShell is a collection of 17 functions and a few supporting functions in a script module. </p>
<p>Download <a href='http://www.indented.co.uk/wp-content/uploads/2011/05/NetShell.zip'>NetShell</a><br />
<span id="more-1561"></span><br />
Installation is a manual process, but not too hard. Open up Documents\WindowsPowerShell\Modules, extract the ZIP file. Make sure it includes the NetShell folder or it won&#8217;t work. The module is not currently signed, something else on the to-do list. Once it&#8217;s there, Import-Module NetShell and off you go.</p>
<p>It includes the following:</p>
<table>
<tr>
<td><b>ConvertTo-BinaryIP</b></td>
<td>Converts an IP address into a binary string</td>
</tr>
<tr>
<td><b>ConvertTo-Byte</b></td>
<td>A supporting function, a simple conversion of a string to a byte array (ASCII encoding)</td>
</tr>
<tr>
<td><b>ConvertTo-DecimalIP</b></td>
<td>Converts an IP address to 32-bit decimal number</td>
</tr>
<tr>
<td><b>ConvertTo-DottedDecimalIP</b></td>
<td>Converts a binary or 32-bit decimal back to an IP</td>
</tr>
<tr>
<td><b>ConvertTo-Mask</b></td>
<td>Converts from a mask length. For example, gets you from 22 to 255.255.252.0</td>
</tr>
<tr>
<td><b>ConvertTo-MaskLength</b></td>
<td>Converts from the IP form of a mask to the length</td>
</tr>
<tr>
<td><b>ConvertTo-String</b></td>
<td>Another supporting function, converts a byte array to a string (ASCII encoding)</td>
</tr>
<tr>
<td><b>Get-BroadcastAddress</b></td>
<td>Returns the broadcast address for the specified IP address and subnet mask</td>
</tr>
<tr>
<td><b>Get-NetworkAddress</b></td>
<td>Returns the network address for the specified IP address and subnet mask</td>
</tr>
<tr>
<td><b>Get-NetworkRange</b></td>
<td>Returns every IP within the specified range</td>
</tr>
<tr>
<td><b>Get-NetworkSummary</b></td>
<td>Everything about an IP address and mask I considered useful</td>
</tr>
<tr>
<td><b>New-DhcpDiscoverPacket</b></td>
<td>A supporting function for Send-DhcpDiscover. Creates the packet to send (a large byte array)</td>
</tr>
<tr>
<td><b>New-Socket</b></td>
<td>Creates an instance of System.Net.Sockets.Socket, an arbitrary network socket to do with as you please.</td>
</tr>
<tr>
<td><b>New-SysLogDateTime</b></td>
<td>A supporting function to create a DateTime string in the format SysLog likes.</td>
</tr>
<tr>
<td><b>Read-DhcpOption</b></td>
<td>A supporting function to read off an Option from a DHCP packet. Needs to be fed the Extended.BinaryReader class at the top of the module.</td>
</tr>
<tr>
<td><b>Read-DhcpPacket</b></td>
<td>Creates and uses an instance of Extended.BinaryReader to process a DHCP packet and translate the fields.</td>
</tr>
<tr>
<td><b>Receive-Bytes</b></td>
<td>Receives a stream of bytes from the network using a socket</td>
</tr>
<tr>
<td><b>Remove-Socket</b></td>
<td>Cleans up after New-Socket</td>
</tr>
<tr>
<td><b>Send-Bytes</b></td>
<td>Sends an arbitrary byte array over the network using a socket</td>
</tr>
<tr>
<td><b>Send-DhcpDiscover</b></td>
<td>Creates and sends a DHCPDISCOVER packet, then processes and returns the response</td>
</tr>
<tr>
<td><b>Start-Syslog</b></td>
<td>Starts a SysLog server. No termination for this one at the moment. Needs a bit more work.</td>
</tr>
<tr>
<td><b>Test-Smtp</b></td>
<td>Does the SMTP test you normally wind up doing with telnet, returning all the results along with the SMTP banner.</td>
</tr>
<tr>
<td><b>Test-SysLogDateTime</b></td>
<td>A supporting function to check the format of a DateTime that may or may not be present in a SysLog message.</td>
</tr>
<tr>
<td><b>Test-SysLogPRI</b></td>
<td>A supporting function to test of the PRI value in a SysLog message.</td>
</tr>
<tr>
<td><b>Test-TcpPort</b></td>
<td>Returns a boolean indicating whether or not the port connection succeeded.</td>
</tr>
</table>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2010/11/25/netshell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>VbScript: GetADSubnets</title>
		<link>http://www.indented.co.uk/index.php/2008/10/25/vbscript-active-directory-subnets/</link>
		<comments>http://www.indented.co.uk/index.php/2008/10/25/vbscript-active-directory-subnets/#comments</comments>
		<pubDate>Sat, 25 Oct 2008 10:46:37 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[VbScript]]></category>
		<category><![CDATA[subnet]]></category>
		<category><![CDATA[vbs]]></category>

		<guid isPermaLink="false">http://www.highorbit.co.uk/?p=543</guid>
		<description><![CDATA[The VbScript below demonstrates how subnet information might be retrieved from Active Directory. It is possible to use a search using to return this information (for objectClass=subnet). This method connects to the subnets container and loops through the contents returning the network address and mask length in a Dictionary object. Usage example No related posts. [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>The VbScript below demonstrates how subnet information might be retrieved from Active Directory. It is possible to use a search using to return this information (for objectClass=subnet). This method connects to the subnets container and loops through the contents returning the network address and mask length in a Dictionary object.<br />
<span id="more-543"></span></p>
<pre class="brush: vb; title: ; notranslate">
Function GetADSubnets
  ' Return Type: Array

  Dim objRootDSE : Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
  Dim objSubnets : Set objSubnets = GetObject(&quot;LDAP://CN=Subnets,CN=Sites,&quot; &amp; _
    objRootDSE.GEt(&quot;configurationNamingContext&quot;))

  Dim arrSubnets() : Dim i : i = 0
  For Each objSubnet in objSubnets
    ReDim Preserve arrSubnets(i)
    arrSubnets(i) = objSubnet.Get(&quot;name&quot;)
    i = i + 1
  Next

  GetADSubnets = arrSubnets
End Function
</pre>
<h3>Usage example</h3>
<pre class="brush: vb; title: ; notranslate">
WScript.Echo Join(GetADSubnets, vbCrlf)
</pre>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2008/10/25/vbscript-active-directory-subnets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

