Uno script PowerShell che, dato un IP e una netmask ritorna l'elenco di tutti gli IP compresi nel range

PowerShell

$ip = "10.134.16.8";
$mask = "255.255.255.128";

$bIP = [System.Net.IPAddress]::Parse($ip).GetAddressBytes();
Write-Host "IP: $([string]::Join('.', $bIP))";

$bMask = [System.Net.IPAddress]::Parse($mask).GetAddressBytes();
Write-Host "Mask: $([string]::Join('.', $bMask))";

$delta = [System.Net.IPAddress]::Parse(0).GetAddressBytes();
$delta[0] = (-bnot $bMask[0]) -band 255;
$delta[1] = (-bnot $bMask[1]) -band 255;
$delta[2] = (-bnot $bMask[2]) -band 255;
$delta[3] = (-bnot $bMask[3]) -band 255;
Write-Host "Mask not: $([string]::Join('.', $delta))";

$bNet = [System.Net.IPAddress]::Parse(0).GetAddressBytes();
$bNet[0] = ($bIP[0] -band $bMask[0]);
$bNet[1] = ($bIP[1] -band $bMask[1]);
$bNet[2] = ($bIP[2] -band $bMask[2]);
$bNet[3] = ($bIP[3] -band $bMask[3]);
Write-Host "Net: $([string]::Join('.', $bNet))";

$bBrodcast = [System.Net.IPAddress]::Parse(0).GetAddressBytes();
$bBrodcast[0] = ($bNet[0] -bor $delta[0]);
$bBrodcast[1] = ($bNet[1] -bor $delta[1]);
$bBrodcast[2] = ($bNet[2] -bor $delta[2]);
$bBrodcast[3] = ($bNet[3] -bor $delta[3]);
Write-Host "Broadcast: $([string]::Join('.', $bBrodcast))";


$intNet = $bNet[0] * 0x1000000 + $bNet[1] * 0x10000 + $bNet[2] * 0x100 + $bNet[3];
$intDelta = ($delta[0] * 0x1000000 + $delta[1] * 0x10000 + $delta[2] * 0x100 + $delta[3])-1;
Write-Host "Delta: $intDelta";

$bIPBegin = [System.Net.IPAddress]::Parse($intNet + 1).GetAddressBytes();
Write-Host "IP range begin: $([string]::Join('.', $bIPBegin))";

$bIPEnd = [System.Net.IPAddress]::Parse($intNet + $intDelta).GetAddressBytes();
Write-Host "IP range end: $([string]::Join('.', $bIPEnd))";

$i = 1;
while($i -le $intDelta){
	$bIPx = [System.Net.IPAddress]::Parse($intNet + $i).GetAddressBytes();
	Write-Host "IP $i : $([string]::Join('.', $bIPx))";
	$i += 1;
}
da questo risultato

Text

IP 1 : 10.134.16.1
IP 2 : 10.134.16.2
IP 3 : 10.134.16.3
IP 4 : 10.134.16.4
IP 5 : 10.134.16.5
IP 6 : 10.134.16.6
IP 7 : 10.134.16.7
IP 8 : 10.134.16.8
IP 9 : 10.134.16.9
IP 10 : 10.134.16.10
IP 11 : 10.134.16.11
IP 12 : 10.134.16.12
IP 13 : 10.134.16.13
IP 14 : 10.134.16.14
IP 15 : 10.134.16.15
IP 16 : 10.134.16.16
IP 17 : 10.134.16.17
IP 18 : 10.134.16.18
IP 19 : 10.134.16.19
IP 20 : 10.134.16.20
IP 21 : 10.134.16.21
IP 22 : 10.134.16.22
IP 23 : 10.134.16.23
IP 24 : 10.134.16.24
IP 25 : 10.134.16.25
IP 26 : 10.134.16.26
IP 27 : 10.134.16.27
IP 28 : 10.134.16.28
IP 29 : 10.134.16.29
IP 30 : 10.134.16.30
IP 31 : 10.134.16.31
IP 32 : 10.134.16.32
IP 33 : 10.134.16.33
IP 34 : 10.134.16.34
IP 35 : 10.134.16.35
IP 36 : 10.134.16.36
IP 37 : 10.134.16.37
IP 38 : 10.134.16.38
IP 39 : 10.134.16.39
IP 40 : 10.134.16.40
IP 41 : 10.134.16.41
IP 42 : 10.134.16.42
IP 43 : 10.134.16.43
IP 44 : 10.134.16.44
IP 45 : 10.134.16.45
IP 46 : 10.134.16.46
IP 47 : 10.134.16.47
IP 48 : 10.134.16.48
IP 49 : 10.134.16.49
IP 50 : 10.134.16.50
IP 51 : 10.134.16.51
IP 52 : 10.134.16.52
IP 53 : 10.134.16.53
IP 54 : 10.134.16.54
IP 55 : 10.134.16.55
IP 56 : 10.134.16.56
IP 57 : 10.134.16.57
IP 58 : 10.134.16.58
IP 59 : 10.134.16.59
IP 60 : 10.134.16.60
IP 61 : 10.134.16.61
IP 62 : 10.134.16.62
IP 63 : 10.134.16.63
IP 64 : 10.134.16.64
IP 65 : 10.134.16.65
IP 66 : 10.134.16.66
IP 67 : 10.134.16.67
IP 68 : 10.134.16.68
IP 69 : 10.134.16.69
IP 70 : 10.134.16.70
IP 71 : 10.134.16.71
IP 72 : 10.134.16.72
IP 73 : 10.134.16.73
IP 74 : 10.134.16.74
IP 75 : 10.134.16.75
IP 76 : 10.134.16.76
IP 77 : 10.134.16.77
IP 78 : 10.134.16.78
IP 79 : 10.134.16.79
IP 80 : 10.134.16.80
IP 81 : 10.134.16.81
IP 82 : 10.134.16.82
IP 83 : 10.134.16.83
IP 84 : 10.134.16.84
IP 85 : 10.134.16.85
IP 86 : 10.134.16.86
IP 87 : 10.134.16.87
IP 88 : 10.134.16.88
IP 89 : 10.134.16.89
IP 90 : 10.134.16.90
IP 91 : 10.134.16.91
IP 92 : 10.134.16.92
IP 93 : 10.134.16.93
IP 94 : 10.134.16.94
IP 95 : 10.134.16.95
IP 96 : 10.134.16.96
IP 97 : 10.134.16.97
IP 98 : 10.134.16.98
IP 99 : 10.134.16.99
IP 100 : 10.134.16.100
IP 101 : 10.134.16.101
IP 102 : 10.134.16.102
IP 103 : 10.134.16.103
IP 104 : 10.134.16.104
IP 105 : 10.134.16.105
IP 106 : 10.134.16.106
IP 107 : 10.134.16.107
IP 108 : 10.134.16.108
IP 109 : 10.134.16.109
IP 110 : 10.134.16.110
IP 111 : 10.134.16.111
IP 112 : 10.134.16.112
IP 113 : 10.134.16.113
IP 114 : 10.134.16.114
IP 115 : 10.134.16.115
IP 116 : 10.134.16.116
IP 117 : 10.134.16.117
IP 118 : 10.134.16.118
IP 119 : 10.134.16.119
IP 120 : 10.134.16.120
IP 121 : 10.134.16.121
IP 122 : 10.134.16.122
IP 123 : 10.134.16.123
IP 124 : 10.134.16.124
IP 125 : 10.134.16.125
IP 126 : 10.134.16.126
Tags:
Network13 PowerShell200 Script85
Potrebbe interessarti anche: