Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Info: Has anyone thought about converting IP and CIDR Notation to Network Host

Reply
 
Old 03-28-2007   #1 (permalink)
Brandon Shell


 
 

Info: Has anyone thought about converting IP and CIDR Notation to Network Host

I have a ping-stat script that pings a group of machines (until I tell it to
stop,) but right now it only take the server(s) by pipe or by -server. I
want to pass a Network and mask with -NET switch and have it ping all the
host on the network.

What I have:
-------------
Ping-stat myserver #just one server
or
$myserverlist | ping-stat

Example of what I would like:
------------------------------
Ping-stat myserver #just one server
or
$myserverlist | ping-stat
or
ping-stat 10.0.0.0/24 -Net

What do you guys think would be the best way to accomplish this.

--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------


My System SpecsSystem Spec
Old 03-28-2007   #2 (permalink)
RichS


 
 

RE: Info: Has anyone thought about converting IP and CIDR Notation to

Now thats a really nice one. I see 2 approaches.

(1) You have a limited number of known subnets to scan in which case you
create a lookup table

(2) You could be wanting to scan any random subnet in which case you would
have to work through the logic to determine first and last IP addresses

My preference would be (2)
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Brandon Shell" wrote:

> I have a ping-stat script that pings a group of machines (until I tell it to
> stop,) but right now it only take the server(s) by pipe or by -server. I
> want to pass a Network and mask with -NET switch and have it ping all the
> host on the network.
>
> What I have:
> -------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
>
> Example of what I would like:
> ------------------------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
> or
> ping-stat 10.0.0.0/24 -Net
>
> What do you guys think would be the best way to accomplish this.
>
> --
> Brandon Shell
> ---------------
> Stop by my blog some time
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
> --------------------------------------
>
>

My System SpecsSystem Spec
Old 03-28-2007   #3 (permalink)
Brandon Shell


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host

This has turned out to be a little more complicated than I had planned.

Right now I need a powershell version of '^' and '>>' Operators. Any Clues?
I dont see anything in about_Operators.

--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------

"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:uGpqtCPcHHA.596@TK2MSFTNGP06.phx.gbl...
>I have a ping-stat script that pings a group of machines (until I tell it
>to stop,) but right now it only take the server(s) by pipe or by -server. I
>want to pass a Network and mask with -NET switch and have it ping all the
>host on the network.
>
> What I have:
> -------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
>
> Example of what I would like:
> ------------------------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
> or
> ping-stat 10.0.0.0/24 -Net
>
> What do you guys think would be the best way to accomplish this.
>
> --
> Brandon Shell
> ---------------
> Stop by my blog some time
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
> --------------------------------------
>


My System SpecsSystem Spec
Old 03-28-2007   #4 (permalink)
Marco Shaw


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host


"Brandon Shell" <tshell.mask@gmail.com> wrote in message
news:uUtj0NWcHHA.3408@TK2MSFTNGP03.phx.gbl...
> This has turned out to be a little more complicated than I had planned.
>
> Right now I need a powershell version of '^' and '>>' Operators. Any
> Clues?
> I dont see anything in about_Operators.


You're better at PowerShell than I. I must be missing something in your
questions?

">>" as in this:

149# "test" >> testing.txt
150# gc testing.txt
test

And "^" to match things 'starting with'?

Marco


My System SpecsSystem Spec
Old 03-28-2007   #5 (permalink)
Brandon Shell


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host

LoL... I would far from say I am better than you in any stretch, but that
aside.. to your question

I am asking specifically asking about operators.

In C#
^ = Bitwise Exclusive Or (I did figure this out its -bxor in Powershell)
>> = Shift to right (meaning it adds 0 to the left and drops of the right)

<< = Shift to left (meaning it adds 0 to the right and drops of the left)

This is critical in determining what is the HOST and what is the Network in
an IP address. My problem ATM is that -band/-bor do not support UInt32.

I'm not sure where I stand now... this is gonna be really big pain if I can
deal with the bits.

--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------

"Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message
news:OTAg69WcHHA.3484@TK2MSFTNGP04.phx.gbl...
>
> "Brandon Shell" <tshell.mask@gmail.com> wrote in message
> news:uUtj0NWcHHA.3408@TK2MSFTNGP03.phx.gbl...
>> This has turned out to be a little more complicated than I had planned.
>>
>> Right now I need a powershell version of '^' and '>>' Operators. Any
>> Clues?
>> I dont see anything in about_Operators.

>
> You're better at PowerShell than I. I must be missing something in your
> questions?
>
> ">>" as in this:
>
> 149# "test" >> testing.txt
> 150# gc testing.txt
> test
>
> And "^" to match things 'starting with'?
>
> Marco
>


My System SpecsSystem Spec
Old 03-28-2007   #6 (permalink)
Joris van Lier


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host

Brandon Shell wrote:
> This has turned out to be a little more complicated than I had
> planned.
>
> Right now I need a powershell version of '^' and '>>' Operators. Any
> Clues? I dont see anything in about_Operators.


I tried creating a cmdlet for Right Shifting bits in bytes from powershell,
using the Compile-Csharp function published on the powershell Blog
Does anyone know how to Install a PSSnapIn that's compiled into an in-memory
assembly?


PS> Compile-Csharp 'using System;using System.ComponentModel;
using System.Management.Automation;[RunInstaller(true)]
public class DynamicSnapIn : PSSnapIn{
public DynamicSnapIn():base(){}
public override string Name{get{return"DynamicSnapIn";}}
public override string Description{get{return"DynamicSnapIn";}}
public override string Vendor{get{return "Me";}}
}
[Cmdlet("Shift","BitLeft")]
public class ShiftBitLeftCommand:Cmdlet{
[Parameter(Mandatory=true,Position=1)]public Byte subject;
[Parameter(Mandatory=true,Position=2)]public Byte modifier;
protected override void ProcessRecord(){WriteObject(subject >>
modifier);}
}'

PS> $dsi = new-object DynamicSnapIn

PS> $uninstallinfo = New-Object HashTable
PS> $dsi.Install($uninstallinfo)
Exception calling "Install" with "1" argument(s): "The path is not of a
legal form."
At line:1 char:13
+ $dsi.Install( <<<< $uninstallinfo)

My System SpecsSystem Spec
Old 03-28-2007   #7 (permalink)
RichS


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Ne

Do you actual need to resort to binary to do this? I seem to remember a
method from my CCNA stuff that would work here. I'll see what I can dig up.

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Brandon Shell" wrote:

> LoL... I would far from say I am better than you in any stretch, but that
> aside.. to your question
>
> I am asking specifically asking about operators.
>
> In C#
> ^ = Bitwise Exclusive Or (I did figure this out its -bxor in Powershell)
> >> = Shift to right (meaning it adds 0 to the left and drops of the right)

> << = Shift to left (meaning it adds 0 to the right and drops of the left)
>
> This is critical in determining what is the HOST and what is the Network in
> an IP address. My problem ATM is that -band/-bor do not support UInt32.
>
> I'm not sure where I stand now... this is gonna be really big pain if I can
> deal with the bits.
>
> --
> Brandon Shell
> ---------------
> Stop by my blog some time
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
> --------------------------------------
>
> "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message
> news:OTAg69WcHHA.3484@TK2MSFTNGP04.phx.gbl...
> >
> > "Brandon Shell" <tshell.mask@gmail.com> wrote in message
> > news:uUtj0NWcHHA.3408@TK2MSFTNGP03.phx.gbl...
> >> This has turned out to be a little more complicated than I had planned.
> >>
> >> Right now I need a powershell version of '^' and '>>' Operators. Any
> >> Clues?
> >> I dont see anything in about_Operators.

> >
> > You're better at PowerShell than I. I must be missing something in your
> > questions?
> >
> > ">>" as in this:
> >
> > 149# "test" >> testing.txt
> > 150# gc testing.txt
> > test
> >
> > And "^" to match things 'starting with'?
> >
> > Marco
> >

>
>

My System SpecsSystem Spec
Old 03-28-2007   #8 (permalink)
Brandon Shell


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host

Actually... I just ended up writing my own bitwise Operator wrapper.

Looks like this:
80# [bwOperators.BitWise]::Band(2886844697,4294967040)
2886844672
81# [bwOperators.BitWise]::Bor(2886844697,4294967040)
4294967065
82# [bwOperators.BitWise]::Bnot(2886844697)
1408122598
83# [bwOperators.BitWise]::LShift(2886844697,2)
2957444196
84# [bwOperators.BitWise]::LShift(2886844697,23)
2357198848
85# [bwOperators.BitWise]::RShift(2886844697,23)
344

Although... honestly... I think i am just going to write this in C#. If I
have to load a .DLL might as well do it all.
It is really frustrating the bitwise operators don't support UInt32.

--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------

"Joris van Lier" <whizzrd@hotmail.com> wrote in message
news:%23v3KsZXcHHA.4216@TK2MSFTNGP02.phx.gbl...
Brandon Shell wrote:
> This has turned out to be a little more complicated than I had
> planned.
>
> Right now I need a powershell version of '^' and '>>' Operators. Any
> Clues? I dont see anything in about_Operators.


I tried creating a cmdlet for Right Shifting bits in bytes from powershell,
using the Compile-Csharp function published on the powershell Blog
Does anyone know how to Install a PSSnapIn that's compiled into an in-memory
assembly?


PS> Compile-Csharp 'using System;using System.ComponentModel;
using System.Management.Automation;[RunInstaller(true)]
public class DynamicSnapIn : PSSnapIn{
public DynamicSnapIn():base(){}
public override string Name{get{return"DynamicSnapIn";}}
public override string Description{get{return"DynamicSnapIn";}}
public override string Vendor{get{return "Me";}}
}
[Cmdlet("Shift","BitLeft")]
public class ShiftBitLeftCommand:Cmdlet{
[Parameter(Mandatory=true,Position=1)]public Byte subject;
[Parameter(Mandatory=true,Position=2)]public Byte modifier;
protected override void ProcessRecord(){WriteObject(subject >>
modifier);}
}'

PS> $dsi = new-object DynamicSnapIn

PS> $uninstallinfo = New-Object HashTable
PS> $dsi.Install($uninstallinfo)
Exception calling "Install" with "1" argument(s): "The path is not of a
legal form."
At line:1 char:13
+ $dsi.Install( <<<< $uninstallinfo)

My System SpecsSystem Spec
Old 03-28-2007   #9 (permalink)
Gaurhoth


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host

This was hastily done so there may be bugs, but here's one way of doing it. I'm sure there has to be a more "Math-centric" way of doing this... but here's the powershell equivelant of how I would do this by hand should I be forced to




function iptolong {

param([string]$ipaddress)

$a = [system.Net.IPAddress]::Parse($ipaddress)

[byte[]]$b = $a.GetAddressBytes()

$longip = 0

for ($i = 0; $i -lt 4; $i++) {

$num = $b[$i]

$longip = (($num % 256) * ([math]:ow(256,3-$i))) + $longip

}

return $longip

}



function longtoip {

param ($long)

return [system.Net.IPAddress]::Parse($long)

}



function ToBinary {

param($dec)

return ([system.Convert]::ToString($dec,2)).padleft(32,[char]"0")

}



function ToDecimal {

param($binary)

$binary = $binary.replace(".","")

return [system.Convert]::ToInt64($binary,2)

}



#EXAMPLE:

# 192.168.5.0/24

$address = "192.168.5.0"

$cidr = 24



$binaddress = ToBinary (iptolong $address)

$binmask = ("1" * $cidr).padright(32,[char]"0")



$binnetwork = ""

$binbroadcast = ""

for ($i = 0; $i -lt 32; $i++) {

$binnetwork += [string]( $binmask.Substring($i,1) -band $binaddress.substring($i,1) )

if ($i -lt $cidr) {

$binbroadcast += [string]( $binaddress.Substring($i,1) )

} else {

$binbroadcast += [string]"1"

}

}



$intnetwork = ToDecimal $binnetwork

$intbroadcast = ToDecimal $binbroadcast



#Check Results:

longtoip $intnetwork

longtoip $intbroadcast





Gaurhoth



"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:uGpqtCPcHHA.596@TK2MSFTNGP06.phx.gbl...
>I have a ping-stat script that pings a group of machines (until I tell it to
> stop,) but right now it only take the server(s) by pipe or by -server. I
> want to pass a Network and mask with -NET switch and have it ping all the
> host on the network.
>
> What I have:
> -------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
>
> Example of what I would like:
> ------------------------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
> or
> ping-stat 10.0.0.0/24 -Net
>
> What do you guys think would be the best way to accomplish this.
>
> --
> Brandon Shell
> ---------------
> Stop by my blog some time
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
> --------------------------------------
>

My System SpecsSystem Spec
Old 03-29-2007   #10 (permalink)
Gaurhoth


 
 

Re: Has anyone thought about converting IP and CIDR Notation to Network Host

I've posted an updated function that supports pipeline input and streamlined some of the conversion functions.

http://thepowershellguy.com/blogs/ga...addresses.aspx

Gaurhoth

"Gaurhoth" <gaurhoth@live.com> wrote in message news:eZa7lcacHHA.4772@TK2MSFTNGP05.phx.gbl...
This was hastily done so there may be bugs, but here's one way of doing it. I'm sure there has to be a more "Math-centric" way of doing this... but here's the powershell equivelant of how I would do this by hand should I be forced to




function iptolong {

param([string]$ipaddress)

$a = [system.Net.IPAddress]::Parse($ipaddress)

[byte[]]$b = $a.GetAddressBytes()

$longip = 0

for ($i = 0; $i -lt 4; $i++) {

$num = $b[$i]

$longip = (($num % 256) * ([math]:ow(256,3-$i))) + $longip

}

return $longip

}



function longtoip {

param ($long)

return [system.Net.IPAddress]::Parse($long)

}



function ToBinary {

param($dec)

return ([system.Convert]::ToString($dec,2)).padleft(32,[char]"0")

}



function ToDecimal {

param($binary)

$binary = $binary.replace(".","")

return [system.Convert]::ToInt64($binary,2)

}



#EXAMPLE:

# 192.168.5.0/24

$address = "192.168.5.0"

$cidr = 24



$binaddress = ToBinary (iptolong $address)

$binmask = ("1" * $cidr).padright(32,[char]"0")



$binnetwork = ""

$binbroadcast = ""

for ($i = 0; $i -lt 32; $i++) {

$binnetwork += [string]( $binmask.Substring($i,1) -band $binaddress.substring($i,1) )

if ($i -lt $cidr) {

$binbroadcast += [string]( $binaddress.Substring($i,1) )

} else {

$binbroadcast += [string]"1"

}

}



$intnetwork = ToDecimal $binnetwork

$intbroadcast = ToDecimal $binbroadcast



#Check Results:

longtoip $intnetwork

longtoip $intbroadcast





Gaurhoth



"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:uGpqtCPcHHA.596@TK2MSFTNGP06.phx.gbl...
>I have a ping-stat script that pings a group of machines (until I tell it to
> stop,) but right now it only take the server(s) by pipe or by -server. I
> want to pass a Network and mask with -NET switch and have it ping all the
> host on the network.
>
> What I have:
> -------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
>
> Example of what I would like:
> ------------------------------
> Ping-stat myserver #just one server
> or
> $myserverlist | ping-stat
> or
> ping-stat 10.0.0.0/24 -Net
>
> What do you guys think would be the best way to accomplish this.
>
> --
> Brandon Shell
> ---------------
> Stop by my blog some time
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
> --------------------------------------
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Network in Host and guest os Virtual PC
I thought virtual machines were separate from the host? Virtual PC
VS on remote network - DNS on Corp network - Host is W. Server 200 Virtual Server
Weird host on network Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46