簡體   English   中英

編寫 bash 腳本將給定的 su.net 細分為預定義數量的較小 su.net

[英]Write a bash script to subdivide an given subnet into a pre-defined number of smaller subnets

這個問題最近在一次采訪中被問到。

問題:編寫一個 bash 腳本將給定的 su.net 細分為預定義數量的較小 su.net。

分割后 IP 地址不應該被浪費,即你的細分的積累應該彌補分割的 su.net。

每個 su.net 都有 3 個 IP 地址保留,主機不能使用:.network、broadcast、gateway。

顯示網絡/廣播地址、主機數量和分配網關。 網關應該是第一個IP 在划分su.net 中可用。 例子:

輸入: ./su.netter.sh 192.168.0.0/24 3

OUTPUT:

subnet=192.168.0.0/25   network=192.168.0.0   broadcast=192.168.0.127 gateway=192.168.0.1   hosts=125 
subnet=192.168.0.128/26 network=192.168.0.128 broadcast=192.168.0.191 gateway=192.168.0.129 hosts=61 
subnet=192.168.0.192/26 network=192.168.0.192 broadcast=192.168.0.255 gateway=192.168.0.193 hosts=61

輸入: ./su.netter.sh 192.168.0.0/24 4

OUTPUT:

subnet=192.168.0.0/26   network=192.168.0.0   broadcast=192.168.0.63  gateway=192.168.0.1   hosts=61 
subnet=192.168.0.64/26  network=192.168.0.64  broadcast=192.168.0.127 gateway=192.168.0.65  hosts=61 
subnet=192.168.0.128/26 network=192.168.0.128 broadcast=192.168.0.191 gateway=192.168.0.129 hosts=61 
subnet=192.168.0.192/26 network=192.168.0.192 broadcast=192.168.0.255 gateway=192.168.0.193 hosts=61

輸入: ./su.netter.sh 10.55.10.64/28 2

OUTPUT:

subnet=10.55.10.64/29   network=10.55.10.64   broadcast=10.55.10.71   gateway=10.55.10.65   hosts=5
subnet=10.55.10.72/29   network=10.55.10.72   broadcast=10.55.10.79   gateway=10.55.10.73   hosts=5

首先,我想分析一下su.net是用什么邏輯來划分的。 其次,我正在嘗試使用ipcalc命令獲取輸出但沒有成功。

謝謝

這是 bash 腳本,我已經嘗試過:應該可以正常工作。 該腳本需要 2 arguments,CIDR 塊和 su.net 的數量來划分。

      #!/bin/bash

      cidr=$1
      total_subnet=$2
      nw_addr=`echo $cidr | awk -F'/' '{ print $1 }'` # retrieving network IP from input 1
      nw_mask=`echo $cidr | awk -F'/' '{ print $2 }'` # retrieving network mask from input 1
      dbit=`echo $nw_addr | awk -F'.' '{ print $4 }'` # retrieving the D-bit from network ( A.B.C.D )
      significant_bit=`echo $nw_addr | awk -F'.' 'BEGIN {OFS = ""}{print $1,".",$2,".",$3 }'` # retrieving A.B.C bits from n/w address

      change_bit=$(( 32 - $nw_mask)) 
      max_addr=$(( 2 ** $change_bit)) # calculating maximum addresses available in the network that can be subdivided
      dbit_max=$dbit 

      # A Funtion to calculate the least power of 2 that is equal or greater than the argument
      least_greater_power_of_two()
      {
      next_power=2
      power=1
      subnet_range=$1
      while [ $next_power -lt $subnet_range  ]; do

       power=$(($power+1))
       next_power=$(( 2 ** $power))
      done

      }

      #initialising Loop Variables
      remaining_addr=$max_addr
      max_subnet_dbit=$dbit
      total_subnet_addr=0
      starting_dbit=$dbit
      i=$total_subnet


      while [ $i -gt 0 ]; do
        starting_dbit=$(( $starting_dbit + $total_subnet_addr )) #Finding the starting D bit of the subnet

        #finding the total number of addresses in the subnet 
        subnet_range=$(( $remaining_addr /  $i )) 
        least_greater_power_of_two $subnet_range 
        total_subnet_addr=$(( 2 ** $power ))

        max_subnet_dbit=$(( $max_subnet_dbit + $total_subnet_addr ))
        remaining_addr=$(( $remaining_addr - $total_subnet_addr ))  # Remaining addresses left to be assigned to the other subnets

        last_dbit=$(( $max_subnet_dbit - 1)) #calculating last D bit in the subnet range
        subnet_mask=$(( $change_bit - $power + $nw_mask )) #calculating the subnet mask
        gateway_dbit=$(( $starting_dbit + 1 )) # calculating the Gateway D bit
        total_hosts=$(( $total_subnet_addr - 3 )) # calculating the Total-hosts in the network
        echo "Subnet= $significant_bit.$starting_dbit/$subnet_mask Network=$significant_bit.$starting_dbit Broadcast=$significant_bit.$last_dbit Gateway= $significant_bit.$gateway_dbit Hosts=$total_hosts"
        i=$(($i-1)) # updating loop variable

      done

我相信我已經根據腳本完成了您需要的 70%。 由於您使用的是 ipcalc,因此致力於使用類似的二進制文件。 要開始,請執行以下操作:

yum install sipcalc 如果您有基於 RPM 的操作系統或 apt-get install sipcalc 取決於您的 Linux 操作系統的發行版。

然后編寫如下腳本,保存為su.netter.sh,賦予'x'權限,使其可以執行。

#!/bin/bash

if [ $# == 0 ]; then
echo "Usage: ./subnetter.sh  IP/SUBNET RANGE"
exit
fi

subnet=$1
network=`echo $subnet | cut -d / -f1`
broadcast=`/usr/bin/sipcalc $1 | grep -i broadcast | cut -d '-' -f2`
gateway=`/usr/bin/sipcalc $1 | grep -i usable | awk '{print $4}'`
hosts=`/usr/bin/sipcalc $1 |  grep -i addresses | cut -d '-' -f2`

echo "subnet =" $subnet "network =" $network "broadcast =" $broadcast "gateway =" $gateway "hosts =" $hosts

我的腳本的 Output:

[root@puppet ~]# ./subnetter.sh  192.168.0.0/24
subnet = 192.168.0.0/24 network = 192.168.0.0 broadcast = 192.168.0.255 gateway = 192.168.0.1 hosts = 256

請注意,第三個參數的要求非常簡單,可以使用 for 循環簡單地完成。 我希望你這樣做。

您可以使用以下工具來確保您的 output 是正確的: http://www.su.net-calculator.com/su.net.php.net_class=C

我已經完成了上述要求,下面是我為實現結果而編寫的程序。

Python 代碼結合上面的shell腳本達到用戶期望的結果

下面的代碼將為現有的 su.net 創建子 su.net,然后調用 shell 腳本循環執行操作,並根據用戶請求提供記錄。

#
#!/bin/bash

for res in `cat hst.txt` ; do

subnet=$res
network=`echo $subnet | cut -d / -f1`
broadcast=`/usr/bin/sipcalc $res | grep -i broadcast | cut -d '-' -f2`
gateway=`/usr/bin/sipcalc $res | grep -i usable | awk '{print $4}'`
hosts=`/usr/bin/sipcalc $res |  grep -i addresses | cut -d '-' -f2`

echo "subnet =" $subnet "network =" $network "broadcast =" $broadcast "gateway =" $gateway "hosts =" $hosts

done
##

代碼2

 #./bin/bash for res in `cat hst;txt` ; do su.net=$res.network=`echo $su.net | cut -d / -f1` broadcast=`/usr/bin/sipcalc $res | grep -i broadcast | cut -d '-' -f2` gateway=`/usr/bin/sipcalc $res | grep -i usable | awk '{print $4}'` hosts=`/usr/bin/sipcalc $res | grep -i addresses | cut -d '-' -f2` echo "su.net =" $su.net .network =" .network "broadcast =" $broadcast "gateway =" $gateway "hosts =" $hosts done
#

從結果中抽樣###

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM