Question IpAddress : "IsInSameSubnet" (code C# / PoSh)

Plus d'informations
il y a 13 ans 5 mois #12731 par Matthew BETTON
Bonsoir,

Peut on ré utiliser ce code avec PowerShell, afin de pouvoir utiliser la méthode 'IsInSameSubnet' ?

J'ai effectué quelques essais (avec Add-Type), mais cela ne fonctionne pas et je sèche un peu...

Existe-t'il une solution ?

:)

@ +

Matthew<br><br>Message édité par: Matthew BETTON, à: 22/09/12 10:29

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 5 mois #12732 par SiSMik
Réponse de SiSMik sur le sujet Re:IsInSameSubnet
Yop Matthew,

Pas réussi non plus, mais ça m'a pas l'air très sorcier à reproduire en powershell :)

PS C:\Users\Mafia&gt; [IpAddress]$ip = '127.0.0.1'
PS C:\Users\Mafia&gt; $ip | gm


TypeName: System.Net.IPAddress

Name MemberType Definition
----



Equals Method bool Equals(System.Object comparand)
GetAddressBytes Method byte[] GetAddressBytes()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Address Property System.Int64 Address {get;set;}
AddressFamily Property System.Net.Sockets.AddressFamily AddressFamily {get;}
IsIPv6LinkLocal Property System.Boolean IsIPv6LinkLocal {get;}
IsIPv6Multicast Property System.Boolean IsIPv6Multicast {get;}
IsIPv6SiteLocal Property System.Boolean IsIPv6SiteLocal {get;}
ScopeId Property System.Int64 ScopeId {get;set;}
IPAddressToString ScriptProperty System.Object IPAddressToString {get=$this.Tostring();}


Avec ça, on doit avoir un moyen de moyenner :evil:

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 5 mois #12734 par SiSMik
Réponse de SiSMik sur le sujet Re:IsInSameSubnet
C'est moche mais ça marche, enfin je crois !

[code:1]function Get-BroadcastAddress {
param (
[IpAddress]$ip,
[IpAddress]$Mask
)

$IpAddressBytes = $ip.GetAddressBytes()
$SubnetMaskBytes = $Mask.GetAddressBytes()

if ($IpAddressBytes.Length -ne $SubnetMaskBytes.Length) {
throw \&quot;Lengths of IP address and subnet mask do not match.\&quot;
exit 0
}

$BroadcastAddress = @()

for ($i=0;$i -le 3;$i++) {
$a = $subnetMaskBytes[$i] -bxor 255
if ($a -eq 0) {
$BroadcastAddress += $ipAddressBytes[$i]
}
else {
$BroadcastAddress += $a
}
}

$BroadcastAddressString = $BroadcastAddress -Join \&quot;.\&quot;
return [IpAddress]$BroadcastAddressString
}

function Get-NetwotkAddress {
param (
[IpAddress]$ip,
[IpAddress]$Mask
)

$IpAddressBytes = $ip.GetAddressBytes()
$SubnetMaskBytes = $Mask.GetAddressBytes()

if ($IpAddressBytes.Length -ne $SubnetMaskBytes.Length) {
throw \&quot;Lengths of IP address and subnet mask do not match.\&quot;
exit 0
}

$BroadcastAddress = @()

for ($i=0;$i -le 3;$i++) {
$BroadcastAddress += $ipAddressBytes[$i]-band $subnetMaskBytes[$i]

}

$BroadcastAddressString = $BroadcastAddress -Join \&quot;.\&quot;
return [IpAddress]$BroadcastAddressString
}

function Test-IsInSameSubnet {
param (
[IpAddress]$ip1,
[IpAddress]$ip2,
[IpAddress]$mask
)

$Network1 = Get-NetwotkAddress -ip $ip1 -mask $mask
$Network2 = Get-NetwotkAddress -ip $ip2 -mask $mask

return $Network1.Equals($Network2)
}

[/code:1]

Windows PowerShell
Copyright © 2012 Microsoft Corporation. Tous droits réservés.

PS C:\Users\Fabien&gt; cd d:
PS D:\&gt; cd .\Powershell
PS D:\Powershell&gt; Import-Module .\Network.psm1
PS D:\Powershell&gt; Test-IsInSameSubnet -ip1 '192.168.45.1' -ip2 '192.168.45.2' -mask '255.255.255.128'
True
PS D:\Powershell&gt; Test-IsInSameSubnet -ip1 '192.168.123.1' -ip2 '192.168.45.2' -mask '255.255.255.128'
False
PS D:\Powershell&gt; Test-IsInSameSubnet -ip1 '192.168.123.1' -ip2 '192.168.45.2' -mask '255.255.0.0'
True

<br><br>Message édité par: benduru, à: 22/09/12 05:14

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 5 mois #12735 par Matthew BETTON
Réponse de Matthew BETTON sur le sujet Re:IsInSameSubnet
Salut Fabien,

Merci pour le code, c'est sympa.

D'après mes quelques tests, cela fonctionne :
[code:1]
PS &gt; Test-IsInSameSubnet -ip1 \&quot;10.32.25.41\&quot; -ip2 \&quot;10.32.23.0\&quot; -mask \&quot;255.255.248.0\&quot;
False
PS &gt; Test-IsInSameSubnet -ip1 \&quot;10.32.25.41\&quot; -ip2 \&quot;10.32.24.0\&quot; -mask \&quot;255.255.248.0\&quot;
True
PS &gt; Test-IsInSameSubnet -ip1 \&quot;10.32.17.41\&quot; -ip2 \&quot;10.32.23.0\&quot; -mask \&quot;255.255.248.0\&quot;
True[/code:1]

En fait, un collègue à moi a déjà travaillé sur le sujet.

Le souci ici pour moi n'était pas de ré écrire le code en PoSh, bien que ta contribution pourra servir à tout le monde, et c'est très bien ;)

Ce que j'aurais aimé savoir c'est s'il est possible de ré utiliser le code dans PowerShell et, quelque soit la réponse, pourquoi ? (ex. : Est-ce du à la surcharge ?)

Voir \&quot;appliquer un des principes de l'objet, la réutilisation\&quot; .

@+

Matthew<br><br>Message édité par: Matthew BETTON, à: 22/09/12 10:30

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 5 mois #12736 par SiSMik
Réponse de SiSMik sur le sujet Re:IsInSameSubnet
Ok, solution trouvée, il fallait ajouté les assemblies à utiliser car il ne connaissait pas les types IpAddress ;)

[code:1]$Class = @'
using System;
using System.Collections;
using System.Net;

public static class IPAddressExtensions
{
public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
{
byte[] ipAdressBytes = address.GetAddressBytes();
byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

if (ipAdressBytes.Length != subnetMaskBytes.Length)
throw new ArgumentException(\&quot;Lengths of IP address and subnet mask do not match.\&quot;«»);

byte[] broadcastAddress = new byte[ipAdressBytes.Length];
for (int i = 0; i &lt; broadcastAddress.Length; i++)
{
broadcastAddress = (byte)(ipAdressBytes | (subnetMaskBytes ^ 255));
}
return new IPAddress(broadcastAddress);
}

public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
{
byte[] ipAdressBytes = address.GetAddressBytes();
byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

if (ipAdressBytes.Length != subnetMaskBytes.Length)
throw new ArgumentException(\&quot;Lengths of IP address and subnet mask do not match.\&quot;«»);

byte[] broadcastAddress = new byte[ipAdressBytes.Length];
for (int i = 0; i &lt; broadcastAddress.Length; i++)
{
broadcastAddress = (byte)(ipAdressBytes &amp; (subnetMaskBytes));
}
return new IPAddress(broadcastAddress);
}

public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
{
IPAddress network1 = address.GetNetworkAddress(subnetMask);
IPAddress network2 = address2.GetNetworkAddress(subnetMask);

return network1.Equals(network2);
}
}
'@

Add-Type $Class -PassThru[/code:1]

PS C:\Users\Fabien&gt; [IPAddressExtensions]

IsPublic IsSerial Name BaseType



----
True False IPAddressExtensions System.Object



PS C:\Users\Fabien&gt; [IpAddress]$ip1 = \&quot;192.168.0.2\&quot;

PS C:\Users\Fabien&gt; [IpAddress]$ip2 = \&quot;192.168.1.15\&quot;

PS C:\Users\Fabien&gt; [IpAddress]$mask = \&quot;255.255.255.0\&quot;

PS C:\Users\Fabien&gt; [IPAddressExtensions]::IsInSameSubnet($ip1,$ip2,$mask)
False


Bon week end !

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 5 mois #12737 par Matthew BETTON
Réponse de Matthew BETTON sur le sujet Re:IsInSameSubnet
Voilà ! Maintenant je comprends mieux mes erreurs :laugh: J'avais bien ajouté les assemblies mais c'est l'appel de la classe qui faisait défaut.

Merci Fabien ;)

P.S. : Tu devrais peut être poster les codes dans le Forum Contributions.

Bon Week et @ bientôt

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.077 secondes
Propulsé par Kunena