mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-23 05:01:37 +01:00
Added check-ipv6-address.ps1
This commit is contained in:
parent
7e1b9fe79f
commit
0f4a7b11a6
@ -1,6 +1,7 @@
|
||||
Filename,Description
|
||||
Script,Description
|
||||
add-firewall-rules.ps1, adds firewall rules to the given executables (requires admin rights)
|
||||
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-symlinks.ps1, checks every symlink in the given directory tree
|
||||
check-xml-file.ps1, checks the given XML file for validity
|
||||
|
|
@ -7,6 +7,7 @@ Table of Contents
|
||||
-----------------
|
||||
* [add-firewall-rules.ps1](Scripts/add-firewall-rules.ps1) - adds firewall rules for the given executables (requires admin rights)
|
||||
* [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-symlinks.ps1](Scripts/check-symlinks.ps1) - checks every symlink in the given directory tree
|
||||
* [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity
|
||||
|
@ -6,20 +6,26 @@
|
||||
.NOTES Author: Markus Fleschutz / License: CC0
|
||||
#>
|
||||
|
||||
param([string]$Addr)
|
||||
param($Address = "")
|
||||
|
||||
# IPv4-Address like 192.168.178.1
|
||||
$RegEx = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
||||
function IsIPv4AddressValid { param([string]$IP)
|
||||
$RegEx = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
||||
if ($IP -match $RegEx) {
|
||||
return $true
|
||||
} else {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if ($Addr -eq "" ) {
|
||||
$Addr = read-host "Enter IPv4 address to validate"
|
||||
if ($Address -eq "" ) {
|
||||
$Address = read-host "Enter IPv4 address to validate"
|
||||
}
|
||||
if ($Addr -match $RegEx) {
|
||||
write-output "IPv4 address $Addr is valid"
|
||||
if (IsIPv4AddressValid $Address) {
|
||||
write-host -foregroundColor green "OK - IPv4 address $Address is valid"
|
||||
exit 0
|
||||
} else {
|
||||
write-output "IPv4 address $Addr is NOT valid"
|
||||
write-warning "Invalid IPv4 address: $Address"
|
||||
exit 1
|
||||
}
|
||||
} catch {
|
||||
|
47
Scripts/check-ipv6-address.ps1
Executable file
47
Scripts/check-ipv6-address.ps1
Executable file
@ -0,0 +1,47 @@
|
||||
#!/bin/powershell
|
||||
<#
|
||||
.SYNTAX ./check-ipv6-address.ps1 [<address>]
|
||||
.DESCRIPTION checks the given IPv6 address for validity
|
||||
.LINK https://github.com/fleschutz/PowerShell
|
||||
.NOTES Author: Markus Fleschutz / License: CC0
|
||||
#>
|
||||
|
||||
param($Address = "")
|
||||
|
||||
function IsIPv6AddressValid { param([string]$IP)
|
||||
$IPv4Regex = '(((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))'
|
||||
$G = '[a-f\d]{1,4}'
|
||||
$Tail = @(":",
|
||||
"(:($G)?|$IPv4Regex)",
|
||||
":($IPv4Regex|$G(:$G)?|)",
|
||||
"(:$IPv4Regex|:$G(:$IPv4Regex|(:$G){0,2})|:)",
|
||||
"((:$G){0,2}(:$IPv4Regex|(:$G){1,2})|:)",
|
||||
"((:$G){0,3}(:$IPv4Regex|(:$G){1,2})|:)",
|
||||
"((:$G){0,4}(:$IPv4Regex|(:$G){1,2})|:)")
|
||||
[string] $IPv6RegexString = $G
|
||||
$Tail | foreach { $IPv6RegexString = "${G}:($IPv6RegexString|$_)" }
|
||||
$IPv6RegexString = ":(:$G){0,5}((:$G){1,2}|:$IPv4Regex)|$IPv6RegexString"
|
||||
$IPv6RegexString = $IPv6RegexString -replace '\(' , '(?:' # make all groups non-capturing
|
||||
[regex] $IPv6Regex = $IPv6RegexString
|
||||
if ($IP -imatch "^$IPv6Regex$") {
|
||||
return $true
|
||||
} else {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if ($Address -eq "" ) {
|
||||
$Address = read-host "Enter IPv6 address to validate"
|
||||
}
|
||||
if (IsIPv6AddressValid $Address) {
|
||||
write-host -foregroundColor green "OK - IPv6 address $Address is valid"
|
||||
exit 0
|
||||
} else {
|
||||
write-warning "Invalid IPv6 address: $Address"
|
||||
exit 1
|
||||
}
|
||||
} catch {
|
||||
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
@ -11,7 +11,7 @@ function ListScripts { param([string]$FilePath)
|
||||
$Table = import-csv "$FilePath"
|
||||
foreach($Row in $Table) {
|
||||
New-Object PSObject -Property @{
|
||||
'Script' = "$($Row.Filename)"
|
||||
'Script' = "$($Row.Script)"
|
||||
'Description' = "$($Row.Description)"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user