#!/bin/bash
if [ $UID -ne 0 ];then
echo "Your are not the root ....";exit 1
fi
if [ $# -ne 2 ];then
echo "Usage: $0 10.1.1.1 255.255.255.0";exit -1
fi
Judge()
{
num=`echo $1| grep -o '\.'|wc -l`
[ "$num" -ne 3 ]&&(echo " Error : It is not userful....")
}
Turn10to2()
{
OB=`echo "obase=2;ibase=10;$1"|bc`
echo $OB
}
Turn2to10()
{
OB=`echo "obase=10;ibase=2;$1"|bc`
echo $OB
}
rmfile()
{
rm -fr /mnt/ipaddress.log
rm -fr /mnt/netmask.log
rm -fr /mnt/_1.log
rm -fr /mnt/ip_mask.log
}
WANZHENG()
{
unset tmp;tmp=$1
_11=`echo $tmp |wc -c`
if [ $_11 -lt 9 ];then
_22=`expr 9 - $_11`
_00=`for ((t=1;t<=$_22;t++)); do y=${y}0; done;echo $y`
tmp=${_00}${tmp}
fi
echo $tmp
}
echo "Judging the ip address ....."
chmod 777 /mnt
chattr -i -a /mnt
rmfile
Judge $1
IFIP=`echo $1 |sed -n '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/p'`
if [ -z $IFIP ];then
echo "The ip is not userful";exit 1
else
for ((i=1;i<5;i++))
do
n=`echo $1|cut -d. -f $i`
_1=`echo $n|cut -c 1`
[ $_1 -eq 0 ]&& n=0
if [ $i -eq 1 ];then
[ "$n" -eq 0 ]&&echo "Error : $n is equal 0 ..."&& exit 1
fi
if [ $i -eq 4 ];then
[ "$n" -eq 0 ]&&echo "Error : $n is equal 0 ..."&& exit 1
fi
[ "$n" -gt 255 ]&&echo "Error : $n is greater than 255 ..."&& exit 1
echo "${n}." >>/mnt/ipaddress.log
done
IP=`cat /mnt/ipaddress.log| tr -d '\n' |sed 's/\./\n/4g'`
fi
echo "Judging the netmask....."
Judge $2
unset netmask;netmask=$2
echo $netmask |awk 'split($0,array,"."){for (i=1;i<=4;i++){print array[i]}}'|grep -A 1 255 >/mnt/_1.log
unset num;num=`cat /mnt/_1.log|wc -l`
unset ifz;ifz=`cat /mnt/_1.log |head -1`
if [ -z $ifz ];then
Cut_1=`echo $netmask |cut -d. -f1`
[ "$Cut_1" -gt 255 ]&&echo "Error : $Cut_1 is greater than 255 ..."&& exit 1
_10_2=`Turn10to2 $Cut_1`
_2_wanzheng=`WANZHENG $_10_2`
num_1=`echo $_2_wanzheng | grep -o 1 |wc -l`
num_0=`echo $_2_wanzheng | grep -o 0 |wc -l`
unset x;unset y
NUM_1=`for ((j=1;j<=$num_1;j++)); do x=${x}1; done;echo $x`;
NUM_0=` for ((v=1;v<=$num_0;v++)); do y=${y}0; done;echo $y`
NUM=${NUM_1}${NUM_0}
[ $NUM != $_2_wanzheng ]&&echo "Error : $n is not userful .______.."&&exit 1
MASK=$2
else
unset NUM; NUM=`expr 4 - $num`
if [ $NUM -ne 0 ];then
for ((i=$NUM;i>0;i--)); do echo 0 >>/mnt/_1.log; done
netmask=`cat /mnt/_1.log|tr '\n' .|sed 's/.*/&\n/g'|sed 's/\.$//'`
fi
IFMASK=`echo $netmask |sed -n '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/p'`
if [ -z $IFMASK ];then
echo "Error: the NETMASK is not userfull..."
else
rmfile
for ((i=1;i<5;i++))
do
n=`echo $netmask|cut -d. -f $i`
_1=`echo $n|cut -c 1`
[ "$_1" -eq "0" ]&& n=0
if [ $i -eq 1 ];then
[ "$n" -eq 0 ]&&echo "Error : $n is equal 0 ..."&& exit 1
fi
[ "$n" -gt 255 ]&&echo "Error : $n is greater than 255 ..."&& exit 1
n_3=`echo "obase=2;ibase=10;$n"|bc`
n_2=`WANZHENG $n_3`
num_1=`echo $n_2| grep -o 1 |wc -l`
num_0=`echo $n_2| grep -o 0 |wc -l`
unset x;unset y
NUM_1=`for ((j=1;j<=$num_1;j++)); do x=${x}1; done;echo $x`;
NUM_0=` for ((v=1;v<=$num_0;v++)); do y=${y}0; done;echo $y`
NUM=${NUM_1}${NUM_0}
[ $NUM != $n_2 ]&&echo "Error : $n is not userful .______.."&&exit 1
echo "${n}.">>/mnt/netmask.log
done
MASK=`cat /mnt/netmask.log| tr -d '\n' |sed 's/\./\n/4g'`
fi
fi
echo "The ip address is :"
echo "++++++++++++++++++++++++++"
echo "+ $IP +"|grep $IP --color=yes
echo "++++++++++++++++++++++++++"
echo "The netmask is :"
echo "++++++++++++++++++++++++++"
echo "+ $MASK +"|grep $MASK --color=yes
echo "++++++++++++++++++++++++++"
echo "Turning ...";sleep 2
function TURN()
{
for ((i=1;i<5;i++))
do
TN=`echo $1| awk 'split($0,array,"."){for (j=1;j<=4;j++){print array[j]}}' |
\awk 'NR==nr {print $0}' nr=$i`
TO_DUMP=`Turn10to2 $TN`
TO=`WANZHENG $TO_DUMP`
echo "${TO}.">>/mnt/${1}.log
done
echo `cat /mnt/${1}.log| tr -d '\n' |sed 's/\./\n/4g'`
rm -fr /mnt/${1}.log
}
unset ip;ip=`TURN $IP`
unset mask;mask=`TURN $MASK`
echo;echo -e "The IP: $ip"|grep $ip --color=yes
echo;echo -e "The MASK : $mask"|grep $mask --color=yes
unset ip_mask
for ((i=0;i<36;i++))
do
ip_number=`echo ${var:i:1}`
mask_number=`echo ${mask:i:1}`
if [ $mask_number == 1 ];then
ip_mask=${ip_mask}${ip:i:1}
fi
if [ $mask_number == 0 ];then
ip_mask=${ip_mask}0
fi
if [ $mask_number == "." ];then
ip_mask=${ip_mask}.
fi
done>/dev/null 2>&1
echo;echo -e "The NETWORK_NUMBER: $ip_mask"|grep $ip_mask --color=yes
ip_mask_=`echo $ip_mask|sed 's/\./ /g'`
for var in $ip_mask_
do
Turn2to10 $var>>/mnt/ip_mask.log
done
NETWORK=`cat /mnt/ip_mask.log|tr '\n' . |sed 's/\.//4g'|sed 's/.*/&\n/g'`
echo;echo "The network is: $NETWORK "|grep $NETWORK --color=yes
the_num_of_zero=`echo $mask | grep -o 0 |wc -l`
unset D;for ((i=1;i<=the_num_of_zero;i++)); do D=${D}1;done
DIFF=`Turn2to10 $D`
DIFF_1=`expr $DIFF - 1`
echo;echo "The number of userful ip: $DIFF_1" |grep $DIFF_1 --color=yes
rmfile
#_____________________________________________________________________________________________(0)
echolimits()
{
echo;echo "The broadcast is : $BROADCAST" |grep $BROADCAST --color=yes
echo;echo "the limits of usrful ip : ${FIRST_IP} - ${LAST_IP}" \
|grep "${FIRST_IP} - ${LAST_IP}" --color=yes
}
exitlist()
{
echo exiting;sleep 1;exit 0
}
n_8=`echo "${the_num_of_zero}/8"|bc`;
m_8=`echo "${the_num_of_zero}%8"|bc`;
if [ $n_8 == 0 ];then
NETWORK_4=`echo $NETWORK|cut -d. -f4`
LEST=`echo $NETWORK| sed "s/\${NETWORK_4}$//g"`
unset D;for ((i=1;i<=$the_num_of_zero;i++)); do D=${D}1; done
DIFF_1=`Turn2to10 $D`
first=`expr $NETWORK_4 + 1`;FIRST_IP=${LEST}${first}
broad=$[ NETWORK_4 + DIFF ];BROADCAST=${LEST}${broad}
last=$(( broad - 1 ));LAST_IP=${LEST}${last}
echolimits
read -t 4 -p "List(y/n) ?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=$DIFF-1;i++))
do
NL=`expr $NETWORK_4 + $i`
echo $LEST$NL |grep -o "$LEST$NL" --color=yes
done
exit 0 ;;
*)
exitlist ;;
esac
fi
#________________________________________________________________________________________(1)
if [ $n_8 == 1 ];then
if [ $m_8 == 0 ];then
IP_4=`echo $IP|cut -d. -f4`
LEST=`echo $IP| sed "s/\${IP_4}$//g"`
FIRST_IP=${LEST}1
LAST_IP=${LEST}254
BROADCAST=${LEST}255
echolimits
read -t 4 -p "List(y/n) ?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=254;i++)); do echo ${LEST}$i \
| grep "${LEST}$i" --color=yes; done ;;
*)
exitlist ;;
esac
else
unset LEST_2;LEST_2=`echo $NETWORK | tr '.' '\n' | head -2 \
| tr '\n' '.' |sed 's/\.$/\n/g'`
unset LEST_1;LEST_1=`echo $NETWORK | cut -d. -f3`
unset D;for ((i=1;i<=$m_8;i++));do D=${D}1;done
unset DIFF;DIFF=`Turn2to10 $D`
broad=`expr $LEST_1 + $DIFF`
FIRST_IP=${LEST_2}.${LEST_1}.1
LAST_IP=${LEST_2}.${broad}.254
BROADCAST=${LEST_2}.${broad}.255
echolimits
read -t 4 -p "List ?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=$DIFF;i++))
do
MON=`expr $LEST_1 + $i`
for ((j=1;j<=254;j++));do echo ${LEST_2}.${MON}.$j \
|grep "${LEST_2}.${MON}.$j" --color=yes;done
done ;;
*)
exitlist ;;
esac
fi
fi
#_________________________________________________________________________________2
if [ $n_8 == 2 ];then
if [ $m_8 == 0 ];then
unset LEST_2;LEST_2=`echo $NETWORK | tr '.' '\n' \
| head -2 | tr '\n' '.' |sed 's/\.$/\n/g'`
echo;echo "The broadcast is : ${LEST_2}.255.255" \
|grep "${LEST_2}.255.255" --color=yes
FIRST_IP=${LEST_2}.255.1
LAST_IP=${LEST_2}.255.254
BROADCAST=${LEST_2}.255.255
echolimits
read -t 4 -p "List ?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=254;i++))
do
for((j=1;j<=254;j++));do echo ${LEST_2}.${i}.${j} \
|grep "${LEST_2}.${i}.${j}" --color=auto;done
done ;;
*)
exitlist ;;
esac
else
unset LEST;LEST=`echo $NETWORK | tr '.' '\n' | head -1 \
| tr '\n' '.' |sed 's/\.$/\n/g'`
unset LEST_1;LEST_1=`echo $NETWORK | cut -d. -f2`
unset D;for ((i=1;i<=$m_8;i++));do D=${D}1;done
unset DIFF;DIFF=`Turn2to10 $D`
broad=`expr $LEST_1 + $DIFF`
FIRST_IP=${LEST}.${LEST_1}.255.1
LAST_IP=${LEST}.${broad}.255.254
BROADCAST=${LEST}.${broad}.255.255
echolimits
read -t 4 -p "List ?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=$DIFF;i++))
do
MON=`expr $LEST_1 + $i`
for ((j=1;j<=255;j++))
do
for((k=1;k<=254;k++));do echo ${LEST}.${MON}.${j}.${k} \
|grep "${LEST}.${MON}.${j}.${k}" --color=yes;done
done
done
;;
*)
exitlist ;;
esac
fi
fi
#_________________________________________________________________________________3
if [ $n_8 == 3 ];then
if [ $m_8 == 0 ];then
unset LEST_1;LEST_1=`echo $NETWORK | tr '.' '\n' | head -1 \
| tr '\n' '.' |sed 's/\.$/\n/g'`
echo;echo "The broadcast is : ${LEST_1}.255.255.255" \
|grep "${LEST_1}.255.255.255" --color=yes
FIRST_IP=${LEST_1}.255.255.1
LAST_IP=${LEST_1}.255.255.254
BROADCAST=${LEST_1}.255.255.255
echolimits
read -t 4 -p "List ?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=255;i++))
do
for ((k=1;k<=255;i++))
do
for((j=1;j<=254;j++));do echo ${LEST_1}.${i}.${k}.${j} \
|grep "${LEST_1}.${i}.${k}${j}" --color=yes;done
done
done
;;
*)
exitlist ;;
esac
else
unset LEST_1;LEST_1=`echo $NETWORK | tr '.' '\n' | head -1 \
| tr '\n' '.' |sed 's/\.$/\n/g'`
unset D;for ((i=1;i<=$m_8;i++));do D=${D}1;done
unset DIFF;DIFF=`Turn2to10 $D`
broad=`expr $LEST_1 + $DIFF`
FIRST_IP=${LEST_1}.255.255.1
LAST_IP=${broad}.255.255.254
BROADCAST=${broad}.255.255.255
echolimits
read -t 4 -p "List (y/n)?" list
case $list in
y|Y|yes|YES)
echo "The list of userful ip address:";sleep 2
for ((i=1;i<=$DIFF;i++))
do
MON=`expr $LEST_1 + $i`
for ((j=1;j<=255;j++))
do
for((n=1;n<=255;n++))
do
for((k=1;k<=254;k++));do echo ${MON}.${j}.${n}.${k} \
| grep "${MON}.${i}.${j}.${k}" --color=yes;done
done
done
done ;;
*)
exitlist ;;
esac
fi
fi
rmfile
转载于:https://blog.51cto.com/2364821/1260774