Improve interface detection for multi IP hosts

Possibly Fix #38
This commit is contained in:
Christian Stein 2020-01-15 15:25:10 +01:00
parent 5506c496db
commit edacccd75a
2 changed files with 106 additions and 63 deletions

View file

@ -22,15 +22,21 @@
function ConvertTo-IcingaIPBinaryString() function ConvertTo-IcingaIPBinaryString()
{ {
param( param(
$IP [string]$IP
); );
if ($IP -like '*.*') {
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP; $IPArray = $IP.Split(' ');
} elseif ($IP -like '*:*') { $IPList = @();
$IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
foreach ($entry in $IPArray) {
if ($entry -like '*.*') {
$IPList += ConvertTo-IcingaIPv4BinaryString -IP $entry;
} elseif ($entry -like '*:*') {
$IPList += ConvertTo-IcingaIPv6BinaryString -IP $entry;
} else { } else {
return 'Invalid IP was provided!'; return 'Invalid IP was provided!';
} }
}
return $IP;
return $IPList;
} }

View file

@ -37,7 +37,7 @@ function Get-IcingaNetworkInterface()
} }
try { try {
$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString; [array]$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
} catch { } catch {
Write-Host 'Invalid IP was provided!'; Write-Host 'Invalid IP was provided!';
return $null; return $null;
@ -56,19 +56,45 @@ function Get-IcingaNetworkInterface()
$Divide = $Info.DestinationPrefix; $Divide = $Info.DestinationPrefix;
$IP, $Mask = $Divide.Split('/'); $IP, $Mask = $Divide.Split('/');
foreach ($destinationIP in $IPBinStringMaster) {
[string]$Key = '';
[string]$MaskKey = '';
############################################################################ ############################################################################
################################ IPv4 #################################### ################################ IPv4 ####################################
############################################################################ ############################################################################
if ($IPBinStringMaster.name -eq 'IPv4') { if ($destinationIP.name -eq 'IPv4') {
if ($IP -like '*.*') { if ($IP -like '*.*') {
############################################################################ ############################################################################
if ([int]$Mask -lt 10) { if ([int]$Mask -lt 10) {
[string]$MaskKey = [string]::Format('00{0}', $Mask); $MaskKey = [string]::Format('00{0}', $Mask);
} else { } 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( $InterfaceData.Add(
$Key, @{ $Key, @{
@ -77,48 +103,59 @@ function Get-IcingaNetworkInterface()
'Interface' = $Info.ifIndex; '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; $InterfaceDataOrdered = $InterfaceData.GetEnumerator() | Sort-Object -Property Name -Descending;
$ExternalInterfaces = @{};
foreach ( $Route in $InterfaceDataOrdered ) { foreach ( $Route in $InterfaceDataOrdered ) {
foreach ($destinationIP in $IPBinStringMaster) {
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask); [string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
[string]$ToBeMatched = $Route.Value."Binary IP String"; [string]$ToBeMatched = $Route.Value."Binary IP String";
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value; $Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
$Match2=[regex]::Matches($IPBinStringMaster.Value, $RegexPattern).Value; $Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value;
If ($Match1 -like $Match2) { 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;
} }