shirobert 发表于 2018-8-22 09:47:32

shell 子网掩码换算

#!/bin/ksh  
#
  
# This script produces subnet number and broadcast address
  
# according to network number, subnet number and hosts number
  
# per subnet.
  
#
  
set -A network ${1//(\D)/ }
  
if [ $# -ne 3 ]; then
  echo
  echo Usage:$0 netwok_numbersubnet_number hosts_per_subnet
  echo
  echo For example:
  echo
  echo      "       $0 192.9.200.03 20"
  echo
  exit
  
fi
  
# Check the network number format
  
if [ ${#network
[*]} -ne 4 ]; then
  echo Invalid network format
  exit
  
fi

  
# Check the network>  
for (( i=0; i  exit
  elif [ ${network[$i]} -ge 240 ]; then
  echo The network you input is a multicast address.
  exit
  fi
  
done

  
# Check the network>  
if [ ${network} -le 127 ]; then
  bits=24
  
elif [ ${network} -ge 128 ]&& [ ${network} -lt 192 ]; then
  bits=16
  
else
  bits=8
  
fi
  
# Get the bits needed for subnets and hosts
  
while true
  
do
  
if [ $((2**subnet)) -ge $2 ]; then
  break
  
else
  (( subnet++ ))
  
fi
  
done
  
while true
  
do
  
if [ $((2**subnet)) -ge $2 ]; then
  break
  
else
  (( subnet++ ))
  
fi
  
done
  
while true
  
do
  
if [ $((2**host - 2)) -ge $3 ]; then
  break
  
else
  (( host++ ))
  
fi
  
done
  
if [ $(( subnet + host )) -gt $bits ]; then
  echo Too many subnets or hosts in your configuration
  exit
  
fi
  
for (( i=31; i > (31 - host); i -- )); do
  net[$i]=0
  
done
  
for (( i=0; i
页: [1]
查看完整版本: shell 子网掩码换算