mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-26 01:30:00 -05:00
Create first draft: Interface Detection and Sub-Tools
This commit is contained in:
parent
4c839e0b19
commit
9adfe087d8
5 changed files with 294 additions and 0 deletions
36
lib/core/tools/ConvertTo-IcingaIPBinaryString.psm1
Normal file
36
lib/core/tools/ConvertTo-IcingaIPBinaryString.psm1
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Used to convert both IPv4 addresses and IPv6 addresses to binary.
|
||||
.DESCRIPTION
|
||||
ConvertTo-IcingaIPBinaryString returns a binary string based on the given IPv4 address or IPv6 address.
|
||||
|
||||
More Information on https://github.com/Icinga/icinga-powershell-framework
|
||||
.FUNCTIONALITY
|
||||
This module is intended to be used to convert an IPv4 address or IPv6 address to binary string.
|
||||
.PARAMETER IP
|
||||
Used to specify an IPv4 address or IPv6 address.
|
||||
.INPUTS
|
||||
System.String
|
||||
.OUTPUTS
|
||||
System.String
|
||||
|
||||
.LINK
|
||||
https://github.com/Icinga/icinga-powershell-framework
|
||||
.NOTES
|
||||
#>
|
||||
|
||||
function ConvertTo-IcingaIPBinaryString()
|
||||
{
|
||||
param(
|
||||
$IP
|
||||
);
|
||||
if ($IP -like '*.*') {
|
||||
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
|
||||
} elseif ($IP -like '*:*') {
|
||||
$IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
|
||||
} else {
|
||||
return 'Invalid IP was provided!';
|
||||
}
|
||||
|
||||
return $IP;
|
||||
}
|
||||
34
lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1
Normal file
34
lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Used to convert an IPv4 address to binary.
|
||||
.DESCRIPTION
|
||||
ConvertTo-IcingaIPv6 returns a binary string based on the given IPv4 address.
|
||||
|
||||
More Information on https://github.com/Icinga/icinga-powershell-framework
|
||||
.FUNCTIONALITY
|
||||
This module is intended to be used to convert an IPv4 address to binary string. Its recommended to use ConvertTo-IcingaIPBinaryString as a smart function instead.
|
||||
.PARAMETER IP
|
||||
Used to specify an IPv4 address.
|
||||
.INPUTS
|
||||
System.String
|
||||
.OUTPUTS
|
||||
System.String
|
||||
|
||||
.LINK
|
||||
https://github.com/Icinga/icinga-powershell-framework
|
||||
.NOTES
|
||||
#>
|
||||
|
||||
function ConvertTo-IcingaIPv4BinaryString()
|
||||
{
|
||||
param(
|
||||
[string]$IP
|
||||
);
|
||||
$IP = $IP -split '\.' | ForEach-Object {
|
||||
[System.Convert]::ToString($_,2).PadLeft(8,'0');
|
||||
}
|
||||
$IP = $IP -join '';
|
||||
$IP = $IP -replace '\s','';
|
||||
|
||||
return @{'value' = $IP; 'name' = 'IPv4'}
|
||||
}
|
||||
38
lib/core/tools/ConvertTo-IcingaIPv6BinaryString.psm1
Normal file
38
lib/core/tools/ConvertTo-IcingaIPv6BinaryString.psm1
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Used to convert an IPv6 address to binary.
|
||||
.DESCRIPTION
|
||||
ConvertTo-IcingaIPv6 returns a binary string based on the given IPv6 address.
|
||||
|
||||
More Information on https://github.com/Icinga/icinga-powershell-framework
|
||||
.FUNCTIONALITY
|
||||
This module is intended to be used to convert an IPv6 address to binary string. Its recommended to use ConvertTo-IcingaIPBinaryString as a smart function instead.
|
||||
.PARAMETER IP
|
||||
Used to specify an IPv6 address.
|
||||
.INPUTS
|
||||
System.String
|
||||
.OUTPUTS
|
||||
System.String
|
||||
|
||||
.LINK
|
||||
https://github.com/Icinga/icinga-powershell-framework
|
||||
.NOTES
|
||||
#>
|
||||
|
||||
function ConvertTo-IcingaIPv6BinaryString()
|
||||
{
|
||||
param(
|
||||
[string]$IP
|
||||
);
|
||||
[string]$IP = Expand-IcingaIPv6String $IP;
|
||||
[array]$IPArr = $IP.Split(':');
|
||||
|
||||
$IPArr = $IPArr.ToCharArray();
|
||||
$IP = $IPArr | ForEach-Object {
|
||||
[System.Convert]::ToString("0x$_",2).PadLeft(4, '0');
|
||||
}
|
||||
$IP = $IP -join '';
|
||||
$IP = $IP -replace '\s','';
|
||||
|
||||
return @{'value' = $IP; 'name' = 'IPv6'}
|
||||
}
|
||||
67
lib/core/tools/Expand-IcingaIPv6String.psm1
Normal file
67
lib/core/tools/Expand-IcingaIPv6String.psm1
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Used to Expand an IPv6 address.
|
||||
.DESCRIPTION
|
||||
Expand-IcingaIPv6String returns the expanded version of an IPv6 address.
|
||||
|
||||
More Information on https://github.com/Icinga/icinga-powershell-framework
|
||||
.FUNCTIONALITY
|
||||
This module is intended to be used to expand an IPv6 address.
|
||||
.EXAMPLE
|
||||
PS> Expand-IcingaIPv6String ffe8::71:ab:
|
||||
FFE8:0000:0000:0000:0000:0071:00AB:0000
|
||||
.PARAMETER IP
|
||||
Used to specify an IPv6 address.
|
||||
.INPUTS
|
||||
System.String
|
||||
.OUTPUTS
|
||||
System.String
|
||||
|
||||
.LINK
|
||||
https://github.com/Icinga/icinga-powershell-framework
|
||||
.NOTES
|
||||
#>
|
||||
|
||||
function Expand-IcingaIPv6String()
|
||||
{
|
||||
param (
|
||||
[String]$IP
|
||||
);
|
||||
|
||||
$Counter = 0
|
||||
$RelV = -1
|
||||
|
||||
for($Index = 0; $Index -lt $IP.Length; $Index++) {
|
||||
if ($IP[$Index] -eq ':') {
|
||||
$Counter++
|
||||
if (($Index - 1) -ge 0 -and $IP[$Index - 1] -eq ':'){
|
||||
$RelV = $Index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($RelV -lt 0 -and $Counter -ne 7) {
|
||||
Write-Host "Invalid IP was provided!";
|
||||
return $null;
|
||||
}
|
||||
|
||||
if ($Counter -lt 7) {
|
||||
$IP = $IP.Substring(0, $RelV) + (':'*(7 - $Counter)) + $IP.Substring($RelV)
|
||||
}
|
||||
|
||||
$Result = @();
|
||||
|
||||
foreach ($SIP in $IP -split ':') {
|
||||
$Value = 0;
|
||||
[int]::TryParse(
|
||||
$SIP,
|
||||
[System.Globalization.NumberStyles]::HexNumber,
|
||||
[System.Globalization.CultureInfo]::InvariantCulture,
|
||||
[Ref]$Value
|
||||
) | Out-Null;
|
||||
$Result += ('{0:X4}' -f $Value)
|
||||
}
|
||||
$Result = $Result -join ':';
|
||||
|
||||
return $Result;
|
||||
}
|
||||
119
lib/core/tools/Get-IcingaNetworkInterface.psm1
Normal file
119
lib/core/tools/Get-IcingaNetworkInterface.psm1
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Returns interface ip address, which will be used for the host object within the icinga director.
|
||||
.DESCRIPTION
|
||||
Get-IcingaNetworkInterface returns the ip address of the interface, which will be used for the host object within the icinga director.
|
||||
|
||||
More Information on https://github.com/Icinga/icinga-powershell-framework
|
||||
.FUNCTIONALITY
|
||||
This module is intended to be used to determine the interface ip address, during kickstart wizard, but will also function standalone.
|
||||
.EXAMPLE
|
||||
PS> Get-IcingaNetworkInterface 'icinga.com'
|
||||
192.168.243.88
|
||||
.EXAMPLE
|
||||
PS> Get-IcingaNetworkInterface '8.8.8.8'
|
||||
192.168.243.88
|
||||
.PARAMETER IP
|
||||
Used to specify either an IPv4, IPv6 address or an FQDN.
|
||||
.INPUTS
|
||||
System.String
|
||||
.OUTPUTS
|
||||
System.String
|
||||
|
||||
.LINK
|
||||
https://github.com/Icinga/icinga-powershell-framework
|
||||
.NOTES
|
||||
#>
|
||||
|
||||
function Get-IcingaNetworkInterface()
|
||||
{
|
||||
param(
|
||||
[string]$IP
|
||||
);
|
||||
|
||||
try {
|
||||
$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
|
||||
} catch {
|
||||
Write-Host 'Invalid IP was provided!';
|
||||
return $null;
|
||||
}
|
||||
|
||||
$IPBinStringMaster = ConvertTo-IcingaIPBinaryString -IP $IP;
|
||||
|
||||
[hashtable]$InterfaceData=@{};
|
||||
|
||||
$InterfaceInfo = Get-NetRoute;
|
||||
$Counter = 0;
|
||||
|
||||
foreach ( $Info in $InterfaceInfo ) {
|
||||
$Counter++;
|
||||
|
||||
$Divide = $Info.DestinationPrefix;
|
||||
$IP,$Mask = $Divide.Split('/');
|
||||
|
||||
############################################################################
|
||||
################################ IPv4 ####################################
|
||||
############################################################################
|
||||
if ($IPBinStringMaster.name -eq 'IPv4') {
|
||||
if ($IP -like '*.*') {
|
||||
############################################################################
|
||||
if ([int]$Mask -lt 10) {
|
||||
[string]$MaskKey = [string]::Format('00{0}', $Mask);
|
||||
} else {
|
||||
[string]$MaskKey = [string]::Format('0{0}', $Mask);
|
||||
}
|
||||
|
||||
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
|
||||
|
||||
$InterfaceData.Add(
|
||||
$Key, @{
|
||||
'Binary IP String' = (ConvertTo-IcingaIPBinaryString -IP $IP).value;
|
||||
'Mask' = $Mask;
|
||||
'Interface' = $Info.ifIndex;
|
||||
}
|
||||
);
|
||||
############################################################################
|
||||
}
|
||||
}
|
||||
############################################################################
|
||||
################################ IPv4 ####################################
|
||||
############################################################################
|
||||
if ($IPBinStringMaster.name -eq 'IPv6') {
|
||||
if ($IP -like '*:*') {
|
||||
############################################################################
|
||||
if ([int]$Mask -lt 10) {
|
||||
[string]$MaskKey = [string]::Format('00{0}', $Mask);
|
||||
} elseif ([int]$Mask -lt 100) {
|
||||
[string]$MaskKey = [string]::Format('0{0}', $Mask);
|
||||
} else {
|
||||
[string]$MaskKey = $Mask;
|
||||
}
|
||||
|
||||
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
|
||||
|
||||
$InterfaceData.Add(
|
||||
$Key, @{
|
||||
'Binary IP String' = (ConvertTo-IcingaIPBinaryString -IP $IP);
|
||||
'Mask' = $Mask;
|
||||
'Interface' = $Info.ifIndex;
|
||||
}
|
||||
);
|
||||
############################################################################
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$InterfaceDataOrdered = $InterfaceData.GetEnumerator() | Sort-Object -Property Name -Descending;
|
||||
|
||||
foreach ( $Route in $InterfaceDataOrdered ) {
|
||||
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
|
||||
[string]$ToBeMatched = $Route.Value."Binary IP String";
|
||||
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
|
||||
$Match2=[regex]::Matches($IPBinStringMaster.Value, $RegexPattern).Value;
|
||||
|
||||
If ($Match1 -like $Match2) {
|
||||
return ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $IPBinStringMaster.name).IPAddress);
|
||||
}
|
||||
}
|
||||
return ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $IPBinStringMaster.name).IPAddress).split('%')[0];
|
||||
}
|
||||
Loading…
Reference in a new issue