确定CIDR前缀的网络掩码

时间:2016-01-26 12:03:58

标签: bash

我解决了我的问题,感谢所有试图帮助我的人。

#!/bin/bash
echo "enter you ip"
read ip
case $ip in  

将CIDR地址更改为四元

*.*.*.*/*)
b=`echo $ip | cut -d/ -f1`
a=`echo $ip | cut -d/ -f2`

if [ $a -eq 24 ];then
   echo "$b 255.255.255.0"
elif [ $a -eq 25 ];then
   echo "$b 255.255.255.128"
elif [ $a -eq 26 ];then
   echo "$b 255.255.255.192"
elif [ $a -eq 27 ];then
   echo "$b 255.255.255.224"
elif [ $a -eq 28 ];then
   echo "$b 255.255.255.240"
elif [ $a -eq 29 ];then
   echo "$b 255.255.255.248"
elif [ $a -eq 30 ];then
   echo "$b 255.255.255.252"
elif [ $a -eq 31 ];then
   echo "$b 255.255.255.254"
elif [ $a -eq 32 ];then
   echo "$b 255.255.255.255"
fi
;;  

对面

 *.*.*.*\ *.*.*.*)
c=`echo $ip | awk '{print $1}'`
d=`echo $ip | awk '{print $2}'`

if [ $d == 255.255.255.0 ];then
echo "$c/24"
elif [ $d == 255.255.255.128 ];then
echo "$c/25"
elif [ $d == 255.255.255.192 ];then
echo "$c/26"
elif [ $d == 255.255.255.224 ];then
echo "$c/27"
elif [ $d == 255.255.255.240 ];then
echo "$c/28"
elif [ $d == 255.255.255.248 ];then
echo "$c/29"
elif [ $d == 255.255.255.252 ];then
echo "$c/30"
elif [ $d == 255.255.255.254 ];then
echo "$c/31"
elif [ $d == 255.255.255.255 ];then
echo "$c/32"
fi

;;  

如果你写了ip地址的fel格式

*)
echo "please enter a valid ip address"

esac  
  

在这个脚本中你可以将ip地址从CIDR地址转换为四边形和相反的

1 个答案:

答案 0 :(得分:2)

如果您使用Python,我认为您将处于更好的位置。 netaddr模块可以帮助您轻松执行这些计算。

Netaddr = http://pythonhosted.org/netaddr/tutorial_01.html

如果您使用的是Linux系统,则可以使用ipcalc程序进行这些计算。