Breaking out ip subnet to smaller subnets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking out ip subnet to smaller subnets
# 1  
Old 08-19-2013
Breaking out ip subnet to smaller subnets

I have a script and it works fine, but I am sure this can be shrunk down to something much better. I would appreciate someone taking a crack at it for me.

What it does is take the ip block submitted and breaks it out down to /24's.

Code:
#!/bin/ksh
 
ipadd=${1}
octet1=`echo $ipadd | nawk -F. '{print $1}'`
octet2=`echo $ipadd | nawk -F. '{print $2}'`
octet3=`echo $ipadd | nawk -F. '{print $3}'`
subnet=`echo $ipadd | nawk -F/ '{print $2}'`
octet3_1=`echo "$octet3"+1 | bc`
octet3_2=`echo "$octet3"+2 | bc`
octet3_3=`echo "$octet3"+3 | bc`
octet3_4=`echo "$octet3"+4 | bc`
octet3_5=`echo "$octet3"+5 | bc`
octet3_6=`echo "$octet3"+6 | bc`
octet3_7=`echo "$octet3"+7 | bc`
octet3_8=`echo "$octet3"+8 | bc`
octet3_9=`echo "$octet3"+9 | bc`
octet3_10=`echo "$octet3"+10 | bc`
octet3_11=`echo "$octet3"+11 | bc`
octet3_12=`echo "$octet3"+12 | bc`
octet3_13=`echo "$octet3"+13 | bc`
octet3_14=`echo "$octet3"+14 | bc`
octet3_15=`echo "$octet3"+15 | bc`
octet3_16=`echo "$octet3"+16 | bc`
octet3_17=`echo "$octet3"+17 | bc`
octet3_18=`echo "$octet3"+18 | bc`
octet3_19=`echo "$octet3"+19 | bc`
octet3_20=`echo "$octet3"+20 | bc`
octet3_21=`echo "$octet3"+21 | bc`
octet3_22=`echo "$octet3"+22 | bc`
octet3_23=`echo "$octet3"+23 | bc`
octet3_24=`echo "$octet3"+24 | bc`
octet3_25=`echo "$octet3"+25 | bc`
octet3_26=`echo "$octet3"+26 | bc`
octet3_27=`echo "$octet3"+27 | bc`
octet3_28=`echo "$octet3"+28 | bc`
octet3_29=`echo "$octet3"+29 | bc`
octet3_30=`echo "$octet3"+30 | bc`
octet3_31=`echo "$octet3"+31 | bc`
octet3_32=`echo "$octet3"+32 | bc`
octet3_33=`echo "$octet3"+33 | bc`
octet3_34=`echo "$octet3"+34 | bc`
octet3_35=`echo "$octet3"+35 | bc`
octet3_36=`echo "$octet3"+36 | bc`
octet3_37=`echo "$octet3"+37 | bc`
octet3_38=`echo "$octet3"+38 | bc`
octet3_39=`echo "$octet3"+39 | bc`
octet3_40=`echo "$octet3"+40 | bc`
octet3_41=`echo "$octet3"+41 | bc`
octet3_42=`echo "$octet3"+42 | bc`
octet3_43=`echo "$octet3"+43 | bc`
octet3_44=`echo "$octet3"+44 | bc`
octet3_45=`echo "$octet3"+45 | bc`
octet3_46=`echo "$octet3"+46 | bc`
octet3_47=`echo "$octet3"+47 | bc`
octet3_48=`echo "$octet3"+48 | bc`
octet3_49=`echo "$octet3"+49 | bc`
octet3_50=`echo "$octet3"+50 | bc`
octet3_51=`echo "$octet3"+51 | bc`
octet3_52=`echo "$octet3"+52 | bc`
octet3_53=`echo "$octet3"+53 | bc`
octet3_54=`echo "$octet3"+54 | bc`
octet3_55=`echo "$octet3"+55 | bc`
octet3_56=`echo "$octet3"+56 | bc`
octet3_57=`echo "$octet3"+57 | bc`
octet3_58=`echo "$octet3"+58 | bc`
octet3_59=`echo "$octet3"+59 | bc`
octet3_60=`echo "$octet3"+60 | bc`
octet3_61=`echo "$octet3"+61 | bc`
octet3_62=`echo "$octet3"+62 | bc`
octet3_63=`echo "$octet3"+63 | bc`
if [[ $subnet -eq 24 ]]; then
echo $octet1"."$octet2"."$octet3".0/24"
fi
if [[ $subnet -eq 23 ]]; then
echo $octet1"."$octet2"."$octet3".0/23"  
echo $octet1"."$octet2"."$octet3".0/24"  
echo $octet1"."$octet2"."$octet3_1".0/24"
fi
if [[ $subnet -eq 22 ]]; then
echo $octet1"."$octet2"."$octet3".0/22"
echo $octet1"."$octet2"."$octet3".0/23"
echo $octet1"."$octet2"."$octet3_2".0/23"
echo $octet1"."$octet2"."$octet3".0/24"
echo $octet1"."$octet2"."$octet3_1".0/24"
echo $octet1"."$octet2"."$octet3_2".0/24"
echo $octet1"."$octet2"."$octet3_3".0/24"
fi
if [[ $subnet -eq 21 ]]; then
echo $octet1"."$octet2"."$octet3".0/21"
echo $octet1"."$octet2"."$octet3".0/22"
echo $octet1"."$octet2"."$octet3_4".0/22"
echo $octet1"."$octet2"."$octet3".0/23"
echo $octet1"."$octet2"."$octet3_2".0/23"
echo $octet1"."$octet2"."$octet3_4".0/23"
echo $octet1"."$octet2"."$octet3_6".0/23"
echo $octet1"."$octet2"."$octet3".0/24"
echo $octet1"."$octet2"."$octet3_1".0/24"
echo $octet1"."$octet2"."$octet3_2".0/24"
echo $octet1"."$octet2"."$octet3_3".0/24"
echo $octet1"."$octet2"."$octet3_4".0/24"  
echo $octet1"."$octet2"."$octet3_5".0/24"
echo $octet1"."$octet2"."$octet3_6".0/24"
echo $octet1"."$octet2"."$octet3_7".0/24"
fi
if [[ $subnet -eq 20 ]]; then
echo $octet1"."$octet2"."$octet3".0/20"
echo $octet1"."$octet2"."$octet3".0/21"
echo $octet1"."$octet2"."$octet3_8".0/21"
echo $octet1"."$octet2"."$octet3".0/22"
echo $octet1"."$octet2"."$octet3_4".0/22"
echo $octet1"."$octet2"."$octet3_8".0/22"  
echo $octet1"."$octet2"."$octet3_12".0/22"
echo $octet1"."$octet2"."$octet3".0/23"
echo $octet1"."$octet2"."$octet3_2".0/23"
echo $octet1"."$octet2"."$octet3_4".0/23"
echo $octet1"."$octet2"."$octet3_6".0/23"
echo $octet1"."$octet2"."$octet3_8".0/23"
echo $octet1"."$octet2"."$octet3_10".0/23"
echo $octet1"."$octet2"."$octet3_12".0/23"
echo $octet1"."$octet2"."$octet3".0/24"  
echo $octet1"."$octet2"."$octet3_1".0/24"
echo $octet1"."$octet2"."$octet3_2".0/24"
echo $octet1"."$octet2"."$octet3_3".0/24"
echo $octet1"."$octet2"."$octet3_4".0/24"
echo $octet1"."$octet2"."$octet3_5".0/24"
echo $octet1"."$octet2"."$octet3_6".0/24"
echo $octet1"."$octet2"."$octet3_7".0/24"
echo $octet1"."$octet2"."$octet3_8".0/24"
echo $octet1"."$octet2"."$octet3_9".0/24"
echo $octet1"."$octet2"."$octet3_10".0/24"
echo $octet1"."$octet2"."$octet3_11".0/24"
echo $octet1"."$octet2"."$octet3_12".0/24"
echo $octet1"."$octet2"."$octet3_13".0/24" 
echo $octet1"."$octet2"."$octet3_14".0/24"
echo $octet1"."$octet2"."$octet3_15".0/24"
fi
if [[ $subnet -eq 19 ]]; then
echo $octet1"."$octet2"."$octet3"."$octet"/19"
echo $octet1"."$octet2"."$octet3"."$octet"/20"
echo $octet1"."$octet2"."$octet3"."$octet16"/20"
echo $octet1"."$octet2"."$octet3".0/21"
echo $octet1"."$octet2"."$octet3_8".0/21"
echo $octet1"."$octet2"."$octet3_16".0/21" 
echo $octet1"."$octet2"."$octet3_24".0/21" 
echo $octet1"."$octet2"."$octet3".0/22"  
echo $octet1"."$octet2"."$octet3_4".0/22"
echo $octet1"."$octet2"."$octet3_8".0/22"
echo $octet1"."$octet2"."$octet3_12".0/22"
echo $octet1"."$octet2"."$octet3_16".0/22" 
echo $octet1"."$octet2"."$octet3_20".0/22" 
echo $octet1"."$octet2"."$octet3_24".0/22"
echo $octet1"."$octet2"."$octet3_28".0/22"
echo $octet1"."$octet2"."$octet3".0/23"  
echo $octet1"."$octet2"."$octet3_2".0/23"
echo $octet1"."$octet2"."$octet3_4".0/23" 
echo $octet1"."$octet2"."$octet3_6".0/23" 
echo $octet1"."$octet2"."$octet3_8".0/23" 
echo $octet1"."$octet2"."$octet3_10".0/23"
echo $octet1"."$octet2"."$octet3_12".0/23"
echo $octet1"."$octet2"."$octet3_14".0/23" 
echo $octet1"."$octet2"."$octet3_16".0/23" 
echo $octet1"."$octet2"."$octet3_18".0/23" 
echo $octet1"."$octet2"."$octet3_20".0/23"
echo $octet1"."$octet2"."$octet3_22".0/23"
echo $octet1"."$octet2"."$octet3_24".0/23"
echo $octet1"."$octet2"."$octet3_26".0/23"
echo $octet1"."$octet2"."$octet3_28".0/23"
echo $octet1"."$octet2"."$octet3_30".0/23"
echo $octet1"."$octet2"."$octet3".0/24"
echo $octet1"."$octet2"."$octet3_1".0/24"
echo $octet1"."$octet2"."$octet3_2".0/24"
echo $octet1"."$octet2"."$octet3_3".0/24" 
echo $octet1"."$octet2"."$octet3_4".0/24"
echo $octet1"."$octet2"."$octet3_5".0/24"
echo $octet1"."$octet2"."$octet3_6".0/24"
echo $octet1"."$octet2"."$octet3_7".0/24"
echo $octet1"."$octet2"."$octet3_8".0/24"
echo $octet1"."$octet2"."$octet3_9".0/24"
echo $octet1"."$octet2"."$octet3_10".0/24"
echo $octet1"."$octet2"."$octet3_11".0/24"
echo $octet1"."$octet2"."$octet3_12".0/24"
echo $octet1"."$octet2"."$octet3_13".0/24"
echo $octet1"."$octet2"."$octet3_14".0/24"
echo $octet1"."$octet2"."$octet3_15".0/24"
echo $octet1"."$octet2"."$octet3_16".0/24" 
echo $octet1"."$octet2"."$octet3_17".0/24"
echo $octet1"."$octet2"."$octet3_18".0/24"
echo $octet1"."$octet2"."$octet3_19".0/24"
echo $octet1"."$octet2"."$octet3_20".0/24" 
echo $octet1"."$octet2"."$octet3_21".0/24" 
echo $octet1"."$octet2"."$octet3_22".0/24"
echo $octet1"."$octet2"."$octet3_23".0/24" 
echo $octet1"."$octet2"."$octet3_24".0/24" 
echo $octet1"."$octet2"."$octet3_25".0/24"
echo $octet1"."$octet2"."$octet3_26".0/24"
echo $octet1"."$octet2"."$octet3_27".0/24"
echo $octet1"."$octet2"."$octet3_28".0/24"
echo $octet1"."$octet2"."$octet3_29".0/24"
echo $octet1"."$octet2"."$octet3_30".0/24"
echo $octet1"."$octet2"."$octet3_31".0/24"
fi
if [[ $subnet -eq 18 ]]; then
echo $octet1"."$octet2"."$octet3".0/18"
echo $octet1"."$octet2"."$octet3".0/19"
echo $octet1"."$octet2"."$octet3_32".0/19"
echo $octet1"."$octet2"."$octet3".0/20"
echo $octet1"."$octet2"."$octet3_16".0/20"
echo $octet1"."$octet2"."$octet3_32".0/20" 
echo $octet1"."$octet2"."$octet3_48".0/20"
echo $octet1"."$octet2"."$octet3".0/21"  
echo $octet1"."$octet2"."$octet3_8".0/21"
echo $octet1"."$octet2"."$octet3_16".0/21"
echo $octet1"."$octet2"."$octet3_24".0/21"
echo $octet1"."$octet2"."$octet3_32".0/21" 
echo $octet1"."$octet2"."$octet3_40".0/21" 
echo $octet1"."$octet2"."$octet3_48".0/21"
echo $octet1"."$octet2"."$octet3_56".0/21"
echo $octet1"."$octet2"."$octet3".0/22"  
echo $octet1"."$octet2"."$octet3_4".0/22"
echo $octet1"."$octet2"."$octet3_8".0/22" 
echo $octet1"."$octet2"."$octet3_12".0/22" 
echo $octet1"."$octet2"."$octet3_16".0/22" 
echo $octet1"."$octet2"."$octet3_20".0/22"
echo $octet1"."$octet2"."$octet3_24".0/22"
echo $octet1"."$octet2"."$octet3_28".0/22" 
echo $octet1"."$octet2"."$octet3_32".0/22" 
echo $octet1"."$octet2"."$octet3_36".0/22" 
echo $octet1"."$octet2"."$octet3_40".0/22"
echo $octet1"."$octet2"."$octet3_44".0/22"
echo $octet1"."$octet2"."$octet3_48".0/22"
echo $octet1"."$octet2"."$octet3_52".0/22"
echo $octet1"."$octet2"."$octet3_56".0/22"
echo $octet1"."$octet2"."$octet3_60".0/22"
echo $octet1"."$octet2"."$octet3".0/23"
echo $octet1"."$octet2"."$octet3_2".0/23"
echo $octet1"."$octet2"."$octet3_4".0/23"
echo $octet1"."$octet2"."$octet3_6".0/23" 
echo $octet1"."$octet2"."$octet3_8".0/23"
echo $octet1"."$octet2"."$octet3_10".0/23"
echo $octet1"."$octet2"."$octet3_12".0/23"
echo $octet1"."$octet2"."$octet3_14".0/23"
echo $octet1"."$octet2"."$octet3_16".0/23"
echo $octet1"."$octet2"."$octet3_18".0/23"
echo $octet1"."$octet2"."$octet3_20".0/23"
echo $octet1"."$octet2"."$octet3_22".0/23"
echo $octet1"."$octet2"."$octet3_24".0/23"
echo $octet1"."$octet2"."$octet3_26".0/23"
echo $octet1"."$octet2"."$octet3_28".0/23"
echo $octet1"."$octet2"."$octet3_30".0/23"
echo $octet1"."$octet2"."$octet3_32".0/23" 
echo $octet1"."$octet2"."$octet3_34".0/23"
echo $octet1"."$octet2"."$octet3_36".0/23"
echo $octet1"."$octet2"."$octet3_38".0/23"
echo $octet1"."$octet2"."$octet3_40".0/23" 
echo $octet1"."$octet2"."$octet3_42".0/23" 
echo $octet1"."$octet2"."$octet3_44".0/23"
echo $octet1"."$octet2"."$octet3_46".0/23" 
echo $octet1"."$octet2"."$octet3_48".0/23" 
echo $octet1"."$octet2"."$octet3_50".0/23"
echo $octet1"."$octet2"."$octet3_52".0/23"
echo $octet1"."$octet2"."$octet3_54".0/23"
echo $octet1"."$octet2"."$octet3_56".0/23"
echo $octet1"."$octet2"."$octet3_58".0/23"
echo $octet1"."$octet2"."$octet3_60".0/23"
echo $octet1"."$octet2"."$octet3_62".0/23"
echo $octet1"."$octet2"."$octet3".0/24"
echo $octet1"."$octet2"."$octet3_1".0/24"
echo $octet1"."$octet2"."$octet3_2".0/24"
echo $octet1"."$octet2"."$octet3_3".0/24" 
echo $octet1"."$octet2"."$octet3_4".0/24"
echo $octet1"."$octet2"."$octet3_5".0/24"
echo $octet1"."$octet2"."$octet3_6".0/24"
echo $octet1"."$octet2"."$octet3_7".0/24"
echo $octet1"."$octet2"."$octet3_8".0/24"
echo $octet1"."$octet2"."$octet3_9".0/24"
echo $octet1"."$octet2"."$octet3_10".0/24"
echo $octet1"."$octet2"."$octet3_11".0/24"
echo $octet1"."$octet2"."$octet3_12".0/24"
echo $octet1"."$octet2"."$octet3_13".0/24"
echo $octet1"."$octet2"."$octet3_14".0/24"
echo $octet1"."$octet2"."$octet3_15".0/24"
echo $octet1"."$octet2"."$octet3_16".0/24" 
echo $octet1"."$octet2"."$octet3_17".0/24"
echo $octet1"."$octet2"."$octet3_18".0/24"
echo $octet1"."$octet2"."$octet3_19".0/24"
echo $octet1"."$octet2"."$octet3_20".0/24" 
echo $octet1"."$octet2"."$octet3_21".0/24" 
echo $octet1"."$octet2"."$octet3_22".0/24"
echo $octet1"."$octet2"."$octet3_23".0/24" 
echo $octet1"."$octet2"."$octet3_24".0/24" 
echo $octet1"."$octet2"."$octet3_25".0/24"
echo $octet1"."$octet2"."$octet3_26".0/24"
echo $octet1"."$octet2"."$octet3_27".0/24"
echo $octet1"."$octet2"."$octet3_28".0/24"
echo $octet1"."$octet2"."$octet3_29".0/24"
echo $octet1"."$octet2"."$octet3_30".0/24"
echo $octet1"."$octet2"."$octet3_31".0/24"
echo $octet1"."$octet2"."$octet3_32".0/24"
echo $octet1"."$octet2"."$octet3_33".0/24" 
echo $octet1"."$octet2"."$octet3_34".0/24"
echo $octet1"."$octet2"."$octet3_35".0/24"
echo $octet1"."$octet2"."$octet3_36".0/24"
echo $octet1"."$octet2"."$octet3_37".0/24"
echo $octet1"."$octet2"."$octet3_38".0/24"
echo $octet1"."$octet2"."$octet3_39".0/24"
echo $octet1"."$octet2"."$octet3_40".0/24"
echo $octet1"."$octet2"."$octet3_41".0/24"
echo $octet1"."$octet2"."$octet3_42".0/24"
echo $octet1"."$octet2"."$octet3_43".0/24"
echo $octet1"."$octet2"."$octet3_44".0/24"
echo $octet1"."$octet2"."$octet3_45".0/24"
echo $octet1"."$octet2"."$octet3_46".0/24" 
echo $octet1"."$octet2"."$octet3_47".0/24"
echo $octet1"."$octet2"."$octet3_48".0/24"
echo $octet1"."$octet2"."$octet3_49".0/24"
echo $octet1"."$octet2"."$octet3_50".0/24" 
echo $octet1"."$octet2"."$octet3_51".0/24" 
echo $octet1"."$octet2"."$octet3_52".0/24"
echo $octet1"."$octet2"."$octet3_53".0/24" 
echo $octet1"."$octet2"."$octet3_54".0/24" 
echo $octet1"."$octet2"."$octet3_55".0/24"
echo $octet1"."$octet2"."$octet3_56".0/24"
echo $octet1"."$octet2"."$octet3_57".0/24"
echo $octet1"."$octet2"."$octet3_58".0/24"
echo $octet1"."$octet2"."$octet3_59".0/24"
echo $octet1"."$octet2"."$octet3_60".0/24"
echo $octet1"."$octet2"."$octet3_61".0/24"
echo $octet1"."$octet2"."$octet3_62".0/24"
echo $octet1"."$octet2"."$octet3_63".0/24"
fi

Here is an example run where a /22 is broken down into /23's and /24's.
Code:
./test.sh 192.168.1.0/22
192.168.1.0/22
192.168.1.0/23
192.168.3.0/23
192.168.1.0/24
192.168.2.0/24
192.168.3.0/24
192.168.4.0/24

# 2  
Old 08-20-2013
I don't review the logic with better arithmetic, I just provide the same function for shorter script as same as in your code

Code:
cat test.sh

echo $1|awk '{if ($5=="24") print $1,$2,$3, "0/24"
          if ($5=="23") { for (i=0;i<=0;i++) print $1,$2,$3+i, "0/23"
                          for (i=0;i<=1;i++) print $1,$2,$3+i, "0/24"
                        }
          if ($5=="22") { for (i=0;i<=0;i++) print $1,$2,$3+i, "0/22"
                          for (i=0;i<=2;i=i+2) print $1,$2,$3+i, "0/23"
                          for (i=0;i<=3;i++) print $1,$2,$3+i, "0/24"
                         }
          if ($5=="21") { for (i=0;i<=0;i++) print $1,$2,$3+i, "0/21"
                          for (i=0;i<=4;i=i+4) print $1,$2,$3+i, "0/22"
                          for (i=0;i<=6;i=i+2) print $1,$2,$3+i, "0/23"
                          for (i=0;i<=7;i++) print $1,$2,$3+i, "0/24"
                        }
          if ($5=="20") { for (i=0;i<=0;i++) print $1,$2,$3+i, "0/20"
                          for (i=0;i<=8;i=i+8) print $1,$2,$3+i, "0/21"
                          for (i=0;i<=12;i=i+4) print $1,$2,$3+i, "0/22"
                          for (i=0;i<=12;i=i+2) print $1,$2,$3+i, "0/23"
                          for (i=0;i<=15;i++) print $1,$2,$3+i, "0/24"
                        }
          if ($5=="19") { for (i=0;i<=0;i++) print $1,$2,$3+i, "0/19"
                          for (i=0;i<=16;i=i+16) print $1,$2,$3+i, "0/20"
                          for (i=0;i<=24;i=i+8) print $1,$2,$3+i, "0/21"
                          for (i=0;i<=28;i=i+4) print $1,$2,$3+i, "0/22"
                          for (i=0;i<=30;i=i+2) print $1,$2,$3+i, "0/23"
                          for (i=0;i<=31;i++) print $1,$2,$3+i, "0/24"
                        }
          if ($5=="18") { for (i=0;i<=0;i++) print $1,$2,$3+i, "0/18"
                          for (i=0;i<=32;i=i+32) print $1,$2,$3+i, "0/19"
                          for (i=0;i<=48;i=i+16) print $1,$2,$3+i, "0/20"
                          for (i=0;i<=56;i=i+8) print $1,$2,$3+i, "0/21"
                          for (i=0;i<=60;i=i+4) print $1,$2,$3+i, "0/22"
                          for (i=0;i<=62;i=i+2) print $1,$2,$3+i, "0/23"
                          for (i=0;i<=63;i++) print $1,$2,$3+i, "0/24"
                        }
          }' FS="[./]" OFS="."

So when you have this script ready, just run and test it:

Code:
./test.sh 192.168.1.0/22

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 08-20-2013
Many thanks rdcwayx, works great. I had to change awk to nawk but that was it.
# 4  
Old 08-20-2013
I think your result printed above is wrong. The 22 subnet of 192.168.1.0 is
Code:
11000000.10101000.000000 01.00000000

, so the 23 subnets would be 192.168.0 and 192.168.2, and the 24 subnets were 192.168.0..3.
Code:
11000000.10101000.000000 00.00000000
11000000.10101000.000000 01.00000000
11000000.10101000.000000 10.00000000
11000000.10101000.000000 11.00000000

The .4 would definitely be in another subnet range!
Code:
 
11000000.10101000.000001 00.00000000

---------- Post updated 21-08-13 at 00:27 ---------- Previous update was 20-08-13 at 22:29 ----------

EDIT: Try this (needs bash, not sure about other shells):
Code:
#! /bin/bash 
set -vx 

IP=$((0x$(printf "%02X" ${1//./ })))
NETMASK=$(( (2**32-1)-(2**(32-$2)-1) ))
ORG=$((IP & NETMASK))
STEP=$((2**(32-$3))) 

N=$((IP & NETMASK))
while [[ $((N & NETMASK)) -eq $ORG ]]; do printf "%X\n" $N; ((N += STEP)); done
N=$((IP & NETMASK))
while [[ $((N & NETMASK)) -eq $ORG ]]; do printf "%d.%d.%d.%d\n" $((N>>24)) $((N>>16&255)) $((N>>8&255)) $((N&255)); ((N += STEP)); done

#       print sth in binary
#       BITS=32
#       for ((i=BITS-1;i>=0;i--)); do printf "%d" $(( (IP>>i)%2 )); done; printf "\n"

Run like
Code:
./chksub 192.168.1.0 22 24
C0A80000
C0A80100
C0A80200
C0A80300
192.168.0.0
192.168.1.0
192.168.2.0
192.168.3.0

or

./chksub 192.168.1.0 22 23
C0A80000
C0A80200
192.168.0.0
192.168.2.0

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

2. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

3. IP Networking

DNS and two subnets

Hi guys I started to work in a small company which doesn't have infrastructure and I need to built it. My problem is: I am a student with theoretical knowledge but without practical one so I don't have any ideas how to solve my problem. I installed on CentOS 6.4 DNS and DHCP. This... (1 Reply)
Discussion started by: solaris_user
1 Replies

4. UNIX for Advanced & Expert Users

solaris zones on different subnets

Greetings, Having an issue with a multihomed global zone hosting zones on different subnets. The issue is with the dev zone that is hosted on the non-default network. Any help, other than going to exclusive IP, as I have one card for admin ntwk for both zones, would be appreciated. ... (1 Reply)
Discussion started by: 22blaze
1 Replies

5. IP Networking

Migrating existing Subnet to a new subnet and changing ip addresses in UNIX

Hi, My project needs to migrate the existing Subnet (255.255.255.0) to a new subnet and change the ipaddresses (currently C class). How can I do that. I need some information. John (0 Replies)
Discussion started by: johnmarsh
0 Replies

6. Solaris

Solaris as router between two subnets

I have two networks which is not connected to each other. Each network has its own internet connection. I also have one Solaris machine with two NICs connected to both of the networks. How do I use the Solaris to connect both network so that hosts from either side of the network can... (9 Replies)
Discussion started by: Ahmad Pakhri
9 Replies

7. IP Networking

Routing across different subnets

I have a routing problem that I need to solve for both Windows and Linux. In our test lab, I've set up a small network that is separated from the rest of the work by a firewall. One interface is on the 15.8.152 network, the other is on the 192.168.100 network. I can already get to the... (2 Replies)
Discussion started by: thomn8r
2 Replies

8. AIX

Printing across subnets (Xerox)

All, I have a pSeries 550 and several Xerox printers I need to print to from there. I can define the printers via the Xerox "xpadmin" utility for all the machines locally (in my building, .244.xxx subnet), but if I go to a different subnet (say .131.xxx) I can not apply any changes. I can... (1 Reply)
Discussion started by: kjbaumann
1 Replies

9. UNIX for Advanced & Expert Users

communication problem between two subnets

We have two Sun Sparc systems running on the xxx.xxx.1.xxx network and can be access fine by machines on that subnet. We are able the ping one of the machines from the xxx.xxx.2.xxx network, but we are not able to ping or communicate with the other machine on the (1) subnet from the (2) subnet. ... (8 Replies)
Discussion started by: pyerj
8 Replies
Login or Register to Ask a Question