Skip to content

Commit 9adfe08

Browse files
committed
Create first draft: Interface Detection and Sub-Tools
1 parent 4c839e0 commit 9adfe08

5 files changed

+294
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<#
2+
.SYNOPSIS
3+
Used to convert both IPv4 addresses and IPv6 addresses to binary.
4+
.DESCRIPTION
5+
ConvertTo-IcingaIPBinaryString returns a binary string based on the given IPv4 address or IPv6 address.
6+
7+
More Information on https://github.com/Icinga/icinga-powershell-framework
8+
.FUNCTIONALITY
9+
This module is intended to be used to convert an IPv4 address or IPv6 address to binary string.
10+
.PARAMETER IP
11+
Used to specify an IPv4 address or IPv6 address.
12+
.INPUTS
13+
System.String
14+
.OUTPUTS
15+
System.String
16+
17+
.LINK
18+
https://github.com/Icinga/icinga-powershell-framework
19+
.NOTES
20+
#>
21+
22+
function ConvertTo-IcingaIPBinaryString()
23+
{
24+
param(
25+
$IP
26+
);
27+
if ($IP -like '*.*') {
28+
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
29+
} elseif ($IP -like '*:*') {
30+
$IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
31+
} else {
32+
return 'Invalid IP was provided!';
33+
}
34+
35+
return $IP;
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<#
2+
.SYNOPSIS
3+
Used to convert an IPv4 address to binary.
4+
.DESCRIPTION
5+
ConvertTo-IcingaIPv6 returns a binary string based on the given IPv4 address.
6+
7+
More Information on https://github.com/Icinga/icinga-powershell-framework
8+
.FUNCTIONALITY
9+
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.
10+
.PARAMETER IP
11+
Used to specify an IPv4 address.
12+
.INPUTS
13+
System.String
14+
.OUTPUTS
15+
System.String
16+
17+
.LINK
18+
https://github.com/Icinga/icinga-powershell-framework
19+
.NOTES
20+
#>
21+
22+
function ConvertTo-IcingaIPv4BinaryString()
23+
{
24+
param(
25+
[string]$IP
26+
);
27+
$IP = $IP -split '\.' | ForEach-Object {
28+
[System.Convert]::ToString($_,2).PadLeft(8,'0');
29+
}
30+
$IP = $IP -join '';
31+
$IP = $IP -replace '\s','';
32+
33+
return @{'value' = $IP; 'name' = 'IPv4'}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<#
2+
.SYNOPSIS
3+
Used to convert an IPv6 address to binary.
4+
.DESCRIPTION
5+
ConvertTo-IcingaIPv6 returns a binary string based on the given IPv6 address.
6+
7+
More Information on https://github.com/Icinga/icinga-powershell-framework
8+
.FUNCTIONALITY
9+
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.
10+
.PARAMETER IP
11+
Used to specify an IPv6 address.
12+
.INPUTS
13+
System.String
14+
.OUTPUTS
15+
System.String
16+
17+
.LINK
18+
https://github.com/Icinga/icinga-powershell-framework
19+
.NOTES
20+
#>
21+
22+
function ConvertTo-IcingaIPv6BinaryString()
23+
{
24+
param(
25+
[string]$IP
26+
);
27+
[string]$IP = Expand-IcingaIPv6String $IP;
28+
[array]$IPArr = $IP.Split(':');
29+
30+
$IPArr = $IPArr.ToCharArray();
31+
$IP = $IPArr | ForEach-Object {
32+
[System.Convert]::ToString("0x$_",2).PadLeft(4, '0');
33+
}
34+
$IP = $IP -join '';
35+
$IP = $IP -replace '\s','';
36+
37+
return @{'value' = $IP; 'name' = 'IPv6'}
38+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<#
2+
.SYNOPSIS
3+
Used to Expand an IPv6 address.
4+
.DESCRIPTION
5+
Expand-IcingaIPv6String returns the expanded version of an IPv6 address.
6+
7+
More Information on https://github.com/Icinga/icinga-powershell-framework
8+
.FUNCTIONALITY
9+
This module is intended to be used to expand an IPv6 address.
10+
.EXAMPLE
11+
PS> Expand-IcingaIPv6String ffe8::71:ab:
12+
FFE8:0000:0000:0000:0000:0071:00AB:0000
13+
.PARAMETER IP
14+
Used to specify an IPv6 address.
15+
.INPUTS
16+
System.String
17+
.OUTPUTS
18+
System.String
19+
20+
.LINK
21+
https://github.com/Icinga/icinga-powershell-framework
22+
.NOTES
23+
#>
24+
25+
function Expand-IcingaIPv6String()
26+
{
27+
param (
28+
[String]$IP
29+
);
30+
31+
$Counter = 0
32+
$RelV = -1
33+
34+
for($Index = 0; $Index -lt $IP.Length; $Index++) {
35+
if ($IP[$Index] -eq ':') {
36+
$Counter++
37+
if (($Index - 1) -ge 0 -and $IP[$Index - 1] -eq ':'){
38+
$RelV = $Index
39+
}
40+
}
41+
}
42+
43+
if ($RelV -lt 0 -and $Counter -ne 7) {
44+
Write-Host "Invalid IP was provided!";
45+
return $null;
46+
}
47+
48+
if ($Counter -lt 7) {
49+
$IP = $IP.Substring(0, $RelV) + (':'*(7 - $Counter)) + $IP.Substring($RelV)
50+
}
51+
52+
$Result = @();
53+
54+
foreach ($SIP in $IP -split ':') {
55+
$Value = 0;
56+
[int]::TryParse(
57+
$SIP,
58+
[System.Globalization.NumberStyles]::HexNumber,
59+
[System.Globalization.CultureInfo]::InvariantCulture,
60+
[Ref]$Value
61+
) | Out-Null;
62+
$Result += ('{0:X4}' -f $Value)
63+
}
64+
$Result = $Result -join ':';
65+
66+
return $Result;
67+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<#
2+
.SYNOPSIS
3+
Returns interface ip address, which will be used for the host object within the icinga director.
4+
.DESCRIPTION
5+
Get-IcingaNetworkInterface returns the ip address of the interface, which will be used for the host object within the icinga director.
6+
7+
More Information on https://github.com/Icinga/icinga-powershell-framework
8+
.FUNCTIONALITY
9+
This module is intended to be used to determine the interface ip address, during kickstart wizard, but will also function standalone.
10+
.EXAMPLE
11+
PS> Get-IcingaNetworkInterface 'icinga.com'
12+
192.168.243.88
13+
.EXAMPLE
14+
PS> Get-IcingaNetworkInterface '8.8.8.8'
15+
192.168.243.88
16+
.PARAMETER IP
17+
Used to specify either an IPv4, IPv6 address or an FQDN.
18+
.INPUTS
19+
System.String
20+
.OUTPUTS
21+
System.String
22+
23+
.LINK
24+
https://github.com/Icinga/icinga-powershell-framework
25+
.NOTES
26+
#>
27+
28+
function Get-IcingaNetworkInterface()
29+
{
30+
param(
31+
[string]$IP
32+
);
33+
34+
try {
35+
$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
36+
} catch {
37+
Write-Host 'Invalid IP was provided!';
38+
return $null;
39+
}
40+
41+
$IPBinStringMaster = ConvertTo-IcingaIPBinaryString -IP $IP;
42+
43+
[hashtable]$InterfaceData=@{};
44+
45+
$InterfaceInfo = Get-NetRoute;
46+
$Counter = 0;
47+
48+
foreach ( $Info in $InterfaceInfo ) {
49+
$Counter++;
50+
51+
$Divide = $Info.DestinationPrefix;
52+
$IP,$Mask = $Divide.Split('/');
53+
54+
############################################################################
55+
################################ IPv4 ####################################
56+
############################################################################
57+
if ($IPBinStringMaster.name -eq 'IPv4') {
58+
if ($IP -like '*.*') {
59+
############################################################################
60+
if ([int]$Mask -lt 10) {
61+
[string]$MaskKey = [string]::Format('00{0}', $Mask);
62+
} else {
63+
[string]$MaskKey = [string]::Format('0{0}', $Mask);
64+
}
65+
66+
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
67+
68+
$InterfaceData.Add(
69+
$Key, @{
70+
'Binary IP String' = (ConvertTo-IcingaIPBinaryString -IP $IP).value;
71+
'Mask' = $Mask;
72+
'Interface' = $Info.ifIndex;
73+
}
74+
);
75+
############################################################################
76+
}
77+
}
78+
############################################################################
79+
################################ IPv4 ####################################
80+
############################################################################
81+
if ($IPBinStringMaster.name -eq 'IPv6') {
82+
if ($IP -like '*:*') {
83+
############################################################################
84+
if ([int]$Mask -lt 10) {
85+
[string]$MaskKey = [string]::Format('00{0}', $Mask);
86+
} elseif ([int]$Mask -lt 100) {
87+
[string]$MaskKey = [string]::Format('0{0}', $Mask);
88+
} else {
89+
[string]$MaskKey = $Mask;
90+
}
91+
92+
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
93+
94+
$InterfaceData.Add(
95+
$Key, @{
96+
'Binary IP String' = (ConvertTo-IcingaIPBinaryString -IP $IP);
97+
'Mask' = $Mask;
98+
'Interface' = $Info.ifIndex;
99+
}
100+
);
101+
############################################################################
102+
}
103+
}
104+
}
105+
106+
$InterfaceDataOrdered = $InterfaceData.GetEnumerator() | Sort-Object -Property Name -Descending;
107+
108+
foreach ( $Route in $InterfaceDataOrdered ) {
109+
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
110+
[string]$ToBeMatched = $Route.Value."Binary IP String";
111+
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
112+
$Match2=[regex]::Matches($IPBinStringMaster.Value, $RegexPattern).Value;
113+
114+
If ($Match1 -like $Match2) {
115+
return ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $IPBinStringMaster.name).IPAddress);
116+
}
117+
}
118+
return ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $IPBinStringMaster.name).IPAddress).split('%')[0];
119+
}

0 commit comments

Comments
 (0)