From f0c3a7b25c0f960e7deae3721f1193771910d429 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Fri, 27 Aug 2021 15:35:34 +0200 Subject: [PATCH] Add check-subnet-mask.ps1 --- Data/scripts.csv | 1 + README.md | 1 + Scripts/check-subnet-mask.ps1 | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100755 Scripts/check-subnet-mask.ps1 diff --git a/Data/scripts.csv b/Data/scripts.csv index b55bfa09..d8b7c144 100644 --- a/Data/scripts.csv +++ b/Data/scripts.csv @@ -27,6 +27,7 @@ check-ipv4-address.ps1, checks the given IPv4 address for validity check-ipv6-address.ps1, checks the given IPv6 address for validity check-mac-address.ps1, checks the given MAC address for validity check-ping.ps1, checks the ping latency to the internet +check-subnet-mask.ps1, checks the given subnet mask for validity check-swap-space.ps1, checks the swap space for free space left check-symlinks.ps1, checks every symlink in the given directory tree check-weather.ps1, checks the current weather for critical values diff --git a/README.md b/README.md index b0bb0f30..31489748 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,7 @@ Mega Collection of PowerShell Scripts * [check-ipv4-address.ps1](Scripts/check-ipv4-address.ps1) - checks the given IPv4 address for validity * [check-ipv6-address.ps1](Scripts/check-ipv6-address.ps1) - checks the given IPv6 address for validity * [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity +* [check-subnet-mask.ps1](Scripts/check-subnet-mask.ps1) - checks the given subnet mask for validity * [check-weather.ps1](Scripts/check-weather.ps1) - checks the current weather for critical values * [convert-csv2txt.ps1](Scripts/convert-csv2txt.ps1) - converts the given CSV file to a text list * [convert-mysql2csv.ps1](Scripts/convert-mysql2csv.ps1) - converts the MySQL database table to a CSV file diff --git a/Scripts/check-subnet-mask.ps1 b/Scripts/check-subnet-mask.ps1 new file mode 100755 index 00000000..d8e4d6a5 --- /dev/null +++ b/Scripts/check-subnet-mask.ps1 @@ -0,0 +1,39 @@ +<# +.SYNOPSIS + check-subnet-mask.ps1 [
] +.DESCRIPTION + Checks the given subnet mask for validity +.EXAMPLE + PS> .\check-subnet-mask.ps1 255.255.255.0 +.LINK + https://github.com/fleschutz/PowerShell +.NOTES + Author: Markus Fleschutz + License: CC0 +#> + +param([string]$address = "") + +function IsSubNetMaskValid { param([string]$IP) + $RegEx = "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(255|254|252|248|240|224|192|128|0)$" + if ($IP -match $RegEx) { + return $true + } else { + return $false + } +} + +try { + if ($address -eq "" ) { $address = read-host "Enter subnet mask to validate" } + + if (IsSubNetMaskValid $address) { + "✔️ subnet mask $Address is valid" + exit 0 + } else { + write-warning "Invalid subnet mask: $address" + exit 1 + } +} catch { + write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +}