07-29-2013, 01:17 PM | #1 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
[Powershell] Calculating Network IPs based on CIDR
In this program, we're currently working with complex subnetting, which includes CIDR and VLSM. I'm a tad bit ahead of the class so I figured I'd work a bit more with powershell. I'm trying to write a script that calculates valid network IP addresses based on two inputs: A random host IP and a complex subnet mask. I've already wrote a script that calculates network IP based on a host IP and a classful subnet mask, but that's effectively useless and simple.
I'm wondering what would be the best way to go about this? My current plan is to take the host IP and subnet mask, convert them to binary, separate the bits, and use ANDing to get the network IPs. This is probably going to be terribly inefficient though. This is the code I have for classful subnet masks. Hate on my commenting please, I haven't used a comment in coding since high school xfd.
__________________
|
07-29-2013, 02:25 PM | #2 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: [Powershell] Calculating Network IPs based on CIDR
bitwise AND by octet instead of by binary digit. each time you bitwise AND an octet, just subtract 8 from how many available bits you have left in your subnet mask. once you get the the point you have less than 8, just calculate what you are ANDing with using 2^(however many bits left). you'll want to test for 0 bits left, of course.
|
07-29-2013, 02:26 PM | #3 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
I'm dumb
__________________
|
07-29-2013, 04:09 PM | #4 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
Check this out.
PS C:\Windows\system32> 101 -band 111 101 PS C:\Windows\system32> 1011 -band 1111 83
__________________
|
07-29-2013, 04:18 PM | #5 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: [Powershell] Calculating Network IPs based on CIDR
so the first result is base2 AND and the second result is packed BCD AND? the fuck?
does that mean 0101 -band 1111 is 41 :? Last edited by Fission; 07-29-2013 at 04:23 PM.. |
07-29-2013, 04:55 PM | #6 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
0101 -band 1111 is 69 lmfao
one of the instructors and I figured out what was going on though 1011 -band 1111 is actually doing BAND of the binary values of those integers 10001010111 base 2 = 1111 base 10 01111110011 base 2 = 1011 base 10 00001010011 base 2 = 83 base 10 once we figured that out, we had to do some internet scouring to find a way to bypass that, and we came up with this: take the integer values and convert them to base 2 Code:
$xbin = [convert]::ToString($x,2) $ybin = [convert]::ToString($y,2) Code:
$z = ([convert]::toInt32($xbin,2) -band [convert]::toInt32($ybin,2)) Code:
[convert]::ToString($z,2)
__________________
|
07-29-2013, 05:00 PM | #7 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
something as simple as binary ANDing in powershell took a simfile artist and a particle physicist 30 minutes to figure the fuck out, gg
__________________
|
07-29-2013, 05:16 PM | #8 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: [Powershell] Calculating Network IPs based on CIDR
thank you for indirectly informing me to never ever touch powershell
EDIT: then again, most other languages do bitwise AND in base 10. 101 -band 111 resulting in 101 made me thing it was being done in base 2 -- tricky. Last edited by Fission; 07-29-2013 at 05:22 PM.. |
07-29-2013, 05:18 PM | #9 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
I actually think this is the perfect level of frustration because I'm learning a shitton lmao
__________________
Last edited by Choofers; 07-29-2013 at 05:22 PM.. |
07-29-2013, 05:48 PM | #10 |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: [Powershell] Calculating Network IPs based on CIDR
it would be sick if there were a better function/command to tell powershell, "hey, these numbers are already in binary because I don't suck. bitwise-AND them for me please." even if it spits out an answer in base 10, you would only have to do one conversion.
I guess if there's no existing support for that then you can just write a function and include it in every script, right? but... lol gj guys. also Choof your comments look fine to me \m/ |
07-29-2013, 06:08 PM | #11 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
We actually found a function on the internet that takes two binary values and gives you the ANDed (??) result, also in binary
But I kinda want to write the function myself... too bad I know very little about functions in powershell right now xd
__________________
|
07-29-2013, 06:59 PM | #12 |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: [Powershell] Calculating Network IPs based on CIDR
hit up that documentation yo. at the very least, powershell documentation and help guides and whatnot should be good. microsoft is a p big company
|
08-5-2013, 04:50 PM | #13 |
FFR Player
Join Date: Dec 2008
Age: 33
Posts: 6,205
|
Re: [Powershell] Calculating Network IPs based on CIDR
fuckin GOT IT
messy code as hell, and I'm sure using an array with all the possible values for a subnet mask octet is NOT the best way to do this but who_care_nine_seven_three Code:
$IPnCIDR = Read-Host "Please input an IP with CIDR notation. `nExample: 192.168.7.24/16" $b = $IPnCIDR.Split('/') $cidr = $b[1] $IP = $b[0] $a = @() $count = 0 $cidrVals = 0,128,192,224,240,248,252,254,255 $subMask = 0,0,0,0 $arrIP = $IP.Split('.') $andCount = 0 $nIP = 0,0,0,0 if($cidr[0] -eq '/'){ $cidr = $cidr.replace('/','') } if([int]$cidr -gt 32){ "Your CIDR value is invalid." } else{ do{ if([int]$cidr -gt 8){ $subMask[$count] = 255 $cidr = $cidr - 8 #"$count : '$cidr' is currently $cidr." } else{ $subMask[$count] = $cidrVals[$cidr] $cidr = $cidr - $cidr #"$count : '$cidr' is currently $cidr." } $count++ } while($count -lt 4) do{ $x = $subMask[$andCount] $y = $arrIP[$andCount] $xbin = [convert]::ToString($x,2) $ybin = [convert]::ToString($y,2) $noctet = ([convert]::toInt32($xbin,2) -band [convert]::toInt32($ybin,2)) $nIP[$andCount] = $noctet $andCount++ } while($andCount -lt 4) "Classless subnet mask is $($subMask[0]).$($subMask[1]).$($subMask[2]).$($subMask[3])" "IP address is $($arrIP[0]).$($arrIP[1]).$($arrIP[2]).$($arrIP[3])" "IP Network address is $($nIP[0]).$($nIP[1]).$($nIP[2]).$($nIP[3])" }
__________________
|
08-5-2013, 05:39 PM | #14 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: [Powershell] Calculating Network IPs based on CIDR
a lookup table is the fastest way to do what you are trying to do, so you are all set there.
there is an algorithmic improvement you could make that will allow for one comparison best case, three comparisons worst case to replace the first loop. i'll leave that as an exercise for you ;) EDIT: you should consider storing 4x8 addresses as 8 digit hex numbers. it makes bitwise manipulation with them more efficient. i should have mentioned that before when you were talking about bitwise ANDing. Last edited by Fission; 08-5-2013 at 05:46 PM.. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|