Question IpAddress : "IsInSameSubnet" (code C# / PoSh)
- Matthew BETTON
- Auteur du sujet
- Hors Ligne
- Membre platinium
-
- Messages : 968
- Remerciements reçus 0
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.
- SiSMik
- Hors Ligne
- Membre platinium
-
- Messages : 492
- Remerciements reçus 0
Pas réussi non plus, mais ça m'a pas l'air très sorcier à reproduire en powershell
PS C:\Users\Mafia> [IpAddress]$ip = '127.0.0.1'
PS C:\Users\Mafia> $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
Connexion ou Créer un compte pour participer à la conversation.
- SiSMik
- Hors Ligne
- Membre platinium
-
- Messages : 492
- Remerciements reçus 0
[code:1]function Get-BroadcastAddress {
param (
[IpAddress]$ip,
[IpAddress]$Mask
)
$IpAddressBytes = $ip.GetAddressBytes()
$SubnetMaskBytes = $Mask.GetAddressBytes()
if ($IpAddressBytes.Length -ne $SubnetMaskBytes.Length) {
throw \"Lengths of IP address and subnet mask do not match.\"
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 \".\"
return [IpAddress]$BroadcastAddressString
}
function Get-NetwotkAddress {
param (
[IpAddress]$ip,
[IpAddress]$Mask
)
$IpAddressBytes = $ip.GetAddressBytes()
$SubnetMaskBytes = $Mask.GetAddressBytes()
if ($IpAddressBytes.Length -ne $SubnetMaskBytes.Length) {
throw \"Lengths of IP address and subnet mask do not match.\"
exit 0
}
$BroadcastAddress = @()
for ($i=0;$i -le 3;$i++) {
$BroadcastAddress += $ipAddressBytes[$i]-band $subnetMaskBytes[$i]
}
$BroadcastAddressString = $BroadcastAddress -Join \".\"
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]
<br><br>Message édité par: benduru, à: 22/09/12 05:14Windows PowerShell
Copyright © 2012 Microsoft Corporation. Tous droits réservés.
PS C:\Users\Fabien> cd d:
PS D:\> cd .\Powershell
PS D:\Powershell> Import-Module .\Network.psm1
PS D:\Powershell> Test-IsInSameSubnet -ip1 '192.168.45.1' -ip2 '192.168.45.2' -mask '255.255.255.128'
True
PS D:\Powershell> Test-IsInSameSubnet -ip1 '192.168.123.1' -ip2 '192.168.45.2' -mask '255.255.255.128'
False
PS D:\Powershell> Test-IsInSameSubnet -ip1 '192.168.123.1' -ip2 '192.168.45.2' -mask '255.255.0.0'
True
Connexion ou Créer un compte pour participer à la conversation.
- Matthew BETTON
- Auteur du sujet
- Hors Ligne
- Membre platinium
-
- Messages : 968
- Remerciements reçus 0
Merci pour le code, c'est sympa.
D'après mes quelques tests, cela fonctionne :
[code:1]
PS > Test-IsInSameSubnet -ip1 \"10.32.25.41\" -ip2 \"10.32.23.0\" -mask \"255.255.248.0\"
False
PS > Test-IsInSameSubnet -ip1 \"10.32.25.41\" -ip2 \"10.32.24.0\" -mask \"255.255.248.0\"
True
PS > Test-IsInSameSubnet -ip1 \"10.32.17.41\" -ip2 \"10.32.23.0\" -mask \"255.255.248.0\"
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 \"appliquer un des principes de l'objet, la réutilisation\" .
@+
Matthew<br><br>Message édité par: Matthew BETTON, à: 22/09/12 10:30
Connexion ou Créer un compte pour participer à la conversation.
- SiSMik
- Hors Ligne
- Membre platinium
-
- Messages : 492
- Remerciements reçus 0
[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(\"Lengths of IP address and subnet mask do not match.\"«»);
byte[] broadcastAddress = new byte[ipAdressBytes.Length];
for (int i = 0; i < 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(\"Lengths of IP address and subnet mask do not match.\"«»);
byte[] broadcastAddress = new byte[ipAdressBytes.Length];
for (int i = 0; i < broadcastAddress.Length; i++)
{
broadcastAddress = (byte)(ipAdressBytes & (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> [IPAddressExtensions]
IsPublic IsSerial Name BaseType
----
True False IPAddressExtensions System.Object
PS C:\Users\Fabien> [IpAddress]$ip1 = \"192.168.0.2\"
PS C:\Users\Fabien> [IpAddress]$ip2 = \"192.168.1.15\"
PS C:\Users\Fabien> [IpAddress]$mask = \"255.255.255.0\"
PS C:\Users\Fabien> [IPAddressExtensions]::IsInSameSubnet($ip1,$ip2,$mask)
False
Bon week end !
Connexion ou Créer un compte pour participer à la conversation.
- Matthew BETTON
- Auteur du sujet
- Hors Ligne
- Membre platinium
-
- Messages : 968
- Remerciements reçus 0
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.
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les initiés
- IpAddress : "IsInSameSubnet" (code C# / PoSh)