mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
parent
5506c496db
commit
edacccd75a
2 changed files with 106 additions and 63 deletions
|
|
@ -22,15 +22,21 @@
|
|||
function ConvertTo-IcingaIPBinaryString()
|
||||
{
|
||||
param(
|
||||
$IP
|
||||
[string]$IP
|
||||
);
|
||||
if ($IP -like '*.*') {
|
||||
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
|
||||
} elseif ($IP -like '*:*') {
|
||||
$IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
|
||||
|
||||
$IPArray = $IP.Split(' ');
|
||||
$IPList = @();
|
||||
|
||||
foreach ($entry in $IPArray) {
|
||||
if ($entry -like '*.*') {
|
||||
$IPList += ConvertTo-IcingaIPv4BinaryString -IP $entry;
|
||||
} elseif ($entry -like '*:*') {
|
||||
$IPList += ConvertTo-IcingaIPv6BinaryString -IP $entry;
|
||||
} else {
|
||||
return 'Invalid IP was provided!';
|
||||
}
|
||||
|
||||
return $IP;
|
||||
}
|
||||
|
||||
return $IPList;
|
||||
}
|
||||
|
|
@ -37,7 +37,7 @@ function Get-IcingaNetworkInterface()
|
|||
}
|
||||
|
||||
try {
|
||||
$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
|
||||
[array]$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
|
||||
} catch {
|
||||
Write-Host 'Invalid IP was provided!';
|
||||
return $null;
|
||||
|
|
@ -56,19 +56,45 @@ function Get-IcingaNetworkInterface()
|
|||
$Divide = $Info.DestinationPrefix;
|
||||
$IP, $Mask = $Divide.Split('/');
|
||||
|
||||
foreach ($destinationIP in $IPBinStringMaster) {
|
||||
[string]$Key = '';
|
||||
[string]$MaskKey = '';
|
||||
############################################################################
|
||||
################################ IPv4 ####################################
|
||||
############################################################################
|
||||
if ($IPBinStringMaster.name -eq 'IPv4') {
|
||||
if ($destinationIP.name -eq 'IPv4') {
|
||||
if ($IP -like '*.*') {
|
||||
############################################################################
|
||||
if ([int]$Mask -lt 10) {
|
||||
[string]$MaskKey = [string]::Format('00{0}', $Mask);
|
||||
$MaskKey = [string]::Format('00{0}', $Mask);
|
||||
} else {
|
||||
[string]$MaskKey = [string]::Format('0{0}', $Mask);
|
||||
$MaskKey = [string]::Format('0{0}', $Mask);
|
||||
}
|
||||
############################################################################
|
||||
}
|
||||
}
|
||||
############################################################################
|
||||
################################ IPv6 ####################################
|
||||
############################################################################
|
||||
if ($destinationIP.name -eq 'IPv6') {
|
||||
if ($IP -like '*:*') {
|
||||
############################################################################
|
||||
if ([int]$Mask -lt 10) {
|
||||
$MaskKey = [string]::Format('00{0}', $Mask);
|
||||
} elseif ([int]$Mask -lt 100) {
|
||||
$MaskKey = [string]::Format('0{0}', $Mask);
|
||||
} else {
|
||||
$MaskKey = $Mask;
|
||||
}
|
||||
############################################################################
|
||||
}
|
||||
}
|
||||
|
||||
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
|
||||
$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
|
||||
|
||||
if ($InterfaceData.ContainsKey($Key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$InterfaceData.Add(
|
||||
$Key, @{
|
||||
|
|
@ -77,48 +103,59 @@ function Get-IcingaNetworkInterface()
|
|||
'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;
|
||||
$ExternalInterfaces = @{};
|
||||
|
||||
foreach ( $Route in $InterfaceDataOrdered ) {
|
||||
foreach ($destinationIP in $IPBinStringMaster) {
|
||||
[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;
|
||||
$Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value;
|
||||
|
||||
If ($Match1 -like $Match2) {
|
||||
return ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $IPBinStringMaster.name).IPAddress);
|
||||
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $destinationIP.Name).IPAddress);
|
||||
if ($ExternalInterfaces.ContainsKey($ExternalInterface)) {
|
||||
$ExternalInterfaces[$ExternalInterface].count += 1;
|
||||
} else {
|
||||
$ExternalInterfaces.Add(
|
||||
$ExternalInterface,
|
||||
@{
|
||||
'count' = 1
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
return ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $IPBinStringMaster.name).IPAddress).split('%')[0];
|
||||
}
|
||||
}
|
||||
|
||||
if ($ExternalInterfaces.Count -eq 0) {
|
||||
foreach ($destinationIP in $IPBinStringMaster) {
|
||||
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $destinationIP.name).IPAddress).split('%')[0];
|
||||
if ($ExternalInterfaces.ContainsKey($ExternalInterface)) {
|
||||
$ExternalInterfaces[$ExternalInterface].count += 1;
|
||||
} else {
|
||||
$ExternalInterfaces.Add(
|
||||
$ExternalInterface,
|
||||
@{
|
||||
'count' = 1
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$InternalCount = 0;
|
||||
$UseInterface = '';
|
||||
foreach ($interface in $ExternalInterfaces.Keys) {
|
||||
$currentCount = $ExternalInterfaces[$interface].count;
|
||||
if ($currentCount -gt $InternalCount) {
|
||||
$InternalCount = $currentCount;
|
||||
$UseInterface = $interface;
|
||||
}
|
||||
}
|
||||
return $UseInterface;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue