Giter Site home page Giter Site logo

psnetaddressing's Introduction

PSNetAddressing

version

PSNetAddressing is a PowerShell module that makes dealing with IP addresses easy.

Given an IP address and subnet mask or CIDR prefix, it returns a list of all IPs inside the subnet, the Network Id, Broadcast, First and Last Usable IPs, and Wildcard Mask.

Installation

Via PowerShell Gallery

Install-Module -Name PSNetAddressing -Scope CurrentUser

Via Git

Clone the repository and run .\build.ps1 deploy.

This will install several modules if you do not already have them, see build.ps1 for details. These are only required for the build process and are not otherwise used by PSNetAddressing.

Manually

Copy the files from src to $Home\Documents\WindowsPowerShell\Modules\PSNetAddressing for PowerShell 5.1 or $Home\Documents\PowerShell\Modules\PSNetAddressing for PowerShell 7, and rename the .ps1 file(s) to .psm1.

Release Notes

1.0

  • Initial release

Motivation

What do you do when you need a generate a list of all IPs for a network? This is what most of us have been doing:

$IPs = @()
$NetworkId = "10.1.1"
1..254 | % {$IPs += "$NetworkId.$_"}

However, this isn't great. Validation for $NetworkId is difficult, working with anything larger than a /24 requires a lot of work which makes it impractical to cleanly integrate into modules or scripts.

With PSNetAddressing, the above code becomes:

$IPs = (Get-IPNetwork -IPAddress 10.1.1.0 -PrefixLength 24 -ReturnAllIPs).AllIPs

Usage

Get-IPNetwork

You have the option of specifying either a Subnet Mask or a CIDR Prefix Length

Get-IPNetwork -IPAddress <String> -SubnetMask <String> [-ReturnAllIPs]

Get-IPNetwork -IPAddress <String> -PrefixLength <Int> [-ReturnAllIPs]

Parameters

-IPAddress

Specifies the IP or network address.

Type: String
Position: 0
---
Example: 10.250.1.100
Example: 192.168.1.1
Example: 46.250.1.66

-SubnetMask

Specifies the Subnet Mask in dotted decimal notation

Type: String
Position: 1
---
Example: 255.255.255.0
Example: 255.255.255.252
Example: 255.255.128.0

-PrefixLength

Specifies the PrefixLength in slash notation

Type: Int
Position: 1
---
Example: 24
Example: 30
Example: 16

-ReturnAllIPs

If set, returns a populated array property called AllIPs that contains all usable IP addresses within the specified subnet. This has been set as an optional switch as large networks can return millions if not billions of usable IPs, which can consume significant time, CPU, and memory.

Type: SwitchParameter
Required: False

Examples

PS C:\> Get-IPNetwork -IPAddress 10.250.1.100 -SubnetMask 255.255.255.0


NetworkId    : 10.250.1.0
Broadcast    : 10.250.1.255
SubnetMask   : 255.255.255.0
PrefixLength : 24
WildcardMask : 0.0.0.255
FirstIP      : 10.250.1.1
LastIP       : 10.250.1.254
TotalIPs     : 256
UsableIPs    : 254
AllIPs       : {}
PS C:\> Get-IPNetwork -IPAddress 10.250.1.100 -PrefixLength 24


NetworkId    : 10.250.1.0
Broadcast    : 10.250.1.255
SubnetMask   : 255.255.255.0
PrefixLength : 24
WildcardMask : 0.0.0.255
FirstIP      : 10.250.1.1
LastIP       : 10.250.1.254
TotalIPs     : 256
UsableIPs    : 254
AllIPs       : {}
PS C:\> Get-IPNetwork 10.1.1.1 30 -ReturnAllIPs


NetworkId    : 10.1.1.0
Broadcast    : 10.1.1.3
SubnetMask   : 255.255.255.252
PrefixLength : 30
WildcardMask : 0.0.0.3
FirstIP      : 10.1.1.1
LastIP       : 10.1.1.2
TotalIPs     : 4
UsableIPs    : 2
AllIPs       : {10.1.1.1, 10.1.1.2}
PS C:\> Get-IPNetwork 45.122.250.67 255.255.255.128 -ReturnAllIPs


NetworkId    : 45.122.250.0
Broadcast    : 45.122.250.127
SubnetMask   : 255.255.255.128
PrefixLength : 25
WildcardMask : 0.0.0.127
FirstIP      : 45.122.250.1
LastIP       : 45.122.250.126
TotalIPs     : 128
UsableIPs    : 126
AllIPs       : {45.122.250.1, 45.122.250.2, 45.122.250.3, 45.122.250.4...}

psnetaddressing's People

Contributors

mdjx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.