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

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 07-29-2013, 01:17 PM   #1
Choofers
FFR Player
FFR Music Producer
 
Join Date: Dec 2008
Age: 35
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
 


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 08:24 PM.


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