Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

136 views
Skip to first unread message

Brandon Shell

unread,
Mar 28, 2007, 12:22:10 AM3/28/07
to
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
--------------------------------------

Brandon Shell

unread,
Mar 28, 2007, 1:57:03 PM3/28/07
to
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" <tshel...@mk.gmail.com> wrote in message
news:uGpqtCP...@TK2MSFTNGP06.phx.gbl...

Marco Shaw

unread,
Mar 28, 2007, 3:29:59 PM3/28/07
to

"Brandon Shell" <tshel...@gmail.com> wrote in message
news:uUtj0NWc...@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


Brandon Shell

unread,
Mar 28, 2007, 3:56:22 PM3/28/07
to
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:OTAg69Wc...@TK2MSFTNGP04.phx.gbl...

Joris van Lier

unread,
Mar 28, 2007, 4:19:35 PM3/28/07
to
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)

RichS

unread,
Mar 28, 2007, 4:20:04 PM3/28/07
to
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

unread,
Mar 28, 2007, 5:15:22 PM3/28/07
to
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" <whi...@hotmail.com> wrote in message
news:%23v3KsZX...@TK2MSFTNGP02.phx.gbl...

Gaurhoth

unread,
Mar 28, 2007, 10:08:28 PM3/28/07
to
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]::pow(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" <tshel...@mk.gmail.com> wrote in message news:uGpqtCP...@TK2MSFTNGP06.phx.gbl...

Gaurhoth

unread,
Mar 29, 2007, 10:41:19 AM3/29/07
to
I've posted an updated function that supports pipeline input and streamlined some of the conversion functions.
 
 
Gaurhoth
 
0 new messages