shell 编写bash脚本,将给定子网细分为预定义数量的较小子网

pkwftd7m  于 2023-01-17  发布在  Shell
关注(0)|答案(3)|浏览(189)

这个问题最近在一次采访中被问到。

**问题:**编写一个bash脚本,将给定的子网细分为预定义数量的较小子网。

分割后的IP地址不应被浪费,也就是说,细分的累积应该构成分割后的子网。
每个子网有3个保留的IP地址,主机无法使用:网络、广播、网关。
显示网络/广播地址、主机数量并分配网关。网关应该是划分的子网中第一个可用的IP。示例:
输入:./subnetter.sh 192.168.0.0/24 3
输出:

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

输入:./subnetter.sh 192.168.0.0/24 4
输出:

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

输入:./subnetter.sh 10.55.10.64/28 2
输出:

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

首先,我试图分析使用什么逻辑来划分子网。其次,我试图使用ipcalc命令来获得输出,但没有运气。
谢谢

ngynwnxp

ngynwnxp1#

下面是bash脚本,我已经尝试过了:应该可以正常工作。该脚本有两个参数,CIDR块和要划分的子网数量。

#!/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
kcrjzv8t

kcrjzv8t2#

我相信我已经完成了70%的你需要的东西了。因为你使用ipcalc,所以我也使用了类似的二进制。开始的时候,做以下的事情:
如果您有基于RPM的操作系统,则使用yum安装sipcalc,或者根据Linux操作系统的发行版使用apt-get安装sipcalc。
然后编写以下脚本并将其保存为subnetter.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

我的脚本的输出:

[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循环来完成,我希望你能做到这一点。
您可以使用以下工具来确保输出正确:http://www.subnet-calculator.com/subnet.php?net_class=C

pkbketx9

pkbketx93#

我已经通过了上述要求,下面是我已经编程,以实现的结果。
Python代码与上述shell脚本集成,以实现用户预期的结果
下面的代码将为现有子网创建子子网,然后调用shell脚本执行循环操作,并根据用户请求提供记录。
∮ ∮ ∮
代码2

#!/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

编号
Sample Out from the result ###

相关问题