Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 07-29-2013, 01:17 PM   #1
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default [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.


Code:
#assign variables
$arrIP = @()
$arrSM = @()
$strIP = Read-Host "Please enter an IP address."
$strSN = Read-Host "Please enter a classful subnet mask."

#split IP and subnet masks into 4 element arrays
[int[]]$arrIP = $strIP.Split('.')
[int[]]$arrSM = $strSN.Split('.')

#test that the IP is valid and is not a reserved address
if (($arrIP[0] -lt 224) -and ($arrIP[1] -lt 255) -and ($arrIP[2] -lt 255) -and ($arrIP[3] -lt 255)){  
   #Valid IP
   $booIP = 1
   }
else{
   #Invalid IP
   $booIP = 0
   }

#test for valid classful subnet mask
if(($arrSM[0] -eq 255) -and ($arrSM[1] -eq 255) -and ($arrSM[2] -eq 255)){
   #Valid Class C Subnet Mask
   $booSN = 1
   }
elseif(($arrSM[0] -eq 255) -and ($arrSM[1] -eq 255)){
   #Valid Class B Subnet Mask
   $booSN = 1
   }
elseif($arrSM[0] -eq 255){
   #Valid Class A Subnet Mask
   $booSN = 1
   }
else{
   #Invalid Subnet Mask
   $booSN = 0
   }

#intialize loop variable and network array
$count = 0
$arrNIP = 0,0,0,0

#compare subnet mask and given IP
if(($booIP -eq 1) -and ($booSN -eq 1)){
do{
   if($arrSM[$count] -eq 255){
      $arrNIP[$count] = $arrIP[$count]
      }
   else{
      $arrNIP[$count] = 0
      }
   $count++
   }
until($count -eq 3)

#create network string from array
$strNIP = "$($arrNIP[0]).$($arrNIP[1]).$($arrNIP[2]).$($arrNIP[3])"

#output
"IP address: $strIP"
"Subnet mask: $strSN"
"Network IP address: $strNIP"
}

#this should run whenever an invalid IP or subnet is used
else{
"Your IP or subnet mask were invalid, please try again."
}

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.
__________________
Choofers is offline   Reply With Quote
Old 07-29-2013, 02:25 PM   #2
Fission
no
Simfile JudgeFFR Simfile AuthorFFR Veteran
 
Fission's Avatar
 
Join Date: Jan 2004
Age: 33
Posts: 1,850
Default 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.
Fission is offline   Reply With Quote
Old 07-29-2013, 02:26 PM   #3
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default Re: [Powershell] Calculating Network IPs based on CIDR

I'm dumb
__________________
Choofers is offline   Reply With Quote
Old 07-29-2013, 04:09 PM   #4
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default 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
__________________
Choofers is offline   Reply With Quote
Old 07-29-2013, 04:18 PM   #5
Fission
no
Simfile JudgeFFR Simfile AuthorFFR Veteran
 
Fission's Avatar
 
Join Date: Jan 2004
Age: 33
Posts: 1,850
Default 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..
Fission is offline   Reply With Quote
Old 07-29-2013, 04:55 PM   #6
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default 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)
take those values, and BAND them while converting them... again

Code:
$z = ([convert]::toInt32($xbin,2) -band [convert]::toInt32($ybin,2))
$z will be the base 10 value of $xbin BAND $ybin, if we want it in binary we have to do this

Code:
[convert]::ToString($z,2)
fucking powershell ladies and gentlemen
__________________
Choofers is offline   Reply With Quote
Old 07-29-2013, 05:00 PM   #7
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default 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
__________________
Choofers is offline   Reply With Quote
Old 07-29-2013, 05:16 PM   #8
Fission
no
Simfile JudgeFFR Simfile AuthorFFR Veteran
 
Fission's Avatar
 
Join Date: Jan 2004
Age: 33
Posts: 1,850
Default 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..
Fission is offline   Reply With Quote
Old 07-29-2013, 05:18 PM   #9
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default 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..
Choofers is offline   Reply With Quote
Old 07-29-2013, 05:48 PM   #10
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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/
dAnceguy117 is offline   Reply With Quote
Old 07-29-2013, 06:08 PM   #11
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default 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
__________________
Choofers is offline   Reply With Quote
Old 07-29-2013, 06:59 PM   #12
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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
dAnceguy117 is offline   Reply With Quote
Old 08-5-2013, 04:50 PM   #13
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 33
Posts: 6,205
Default 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])"

}
__________________
Choofers is offline   Reply With Quote
Old 08-5-2013, 05:39 PM   #14
Fission
no
Simfile JudgeFFR Simfile AuthorFFR Veteran
 
Fission's Avatar
 
Join Date: Jan 2004
Age: 33
Posts: 1,850
Default 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..
Fission is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 03:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution