openwrt gets the network segment information of the specified network card

#!/bin/sh


function Dec2Bin() {
    
    
	Decimal=$1
	Binary=""
	
	while [ $Decimal -gt 0 ]; do
		remainder=$((Decimal % 2))
		Binary="$remainder$Binary"
		Decimal=$((Decimal / 2))
	done

	if [ -z "$Binary" ]; then
		Binary="0"
	fi	
	echo "$Binary"
}

Bin2Dec() {
    
    
        Binary=$1
        Decimal=0

        # ........................
        length=${
    
    #Binary}

        # ..........................................
        i=$((length - 1))
        while [ $i -ge 0 ]; do
                #bit=${Binary:i:1}  # .....................
                bit=$(echo "$Binary" | cut -c $(($i+1)))
                if [ "$bit" -eq 1 ]; then
                        power=$((length - 1 - i))  # ............
                        Decimal=$((Decimal + 2 ** power))
                fi
                i=$(expr $i - 1)
        done

        echo "$Decimal"
}


function Toip() {
    
    
	ipc=$1
	local TempP1=$(echo $ipc | cut -c -8)
	TempP1=$(Bin2Dec $TempP1)
	local TempP2=$(echo $ipc | cut -c 9-16)
	TempP2=$(Bin2Dec $TempP2)
	local TempP3=$(echo $ipc | cut -c 17-24)
	TempP3=$(Bin2Dec $TempP3)
	local TempP4=$(echo $ipc | cut -c 25-32)
	TempP4=$(Bin2Dec $TempP4)
	ipc="$TempP1.$TempP2.$TempP3.$TempP4"
	echo $ipc
}

function test(){
    
    
	Subnet=$(ip -o -f inet addr show dev gnLan | awk '{print $4}')
	IpAddress=$(echo "$Subnet" | cut -d '/' -f 1)
	SubnetMask=$(echo "$Subnet" | cut -d '/' -f 2)


	# TODO: 校验是不是为空
	echo "$(date +'%Y-%m-%d %H:%M:%S') 网卡gnlan的网段:$IpAddress   $SubnetMask"
	TempP1=$(echo $IpAddress | cut -d. -f1)
	TempP2=$(echo $IpAddress | cut -d. -f2)
	TempP3=$(echo $IpAddress | cut -d. -f3)
	TempP4=$(echo $IpAddress | cut -d. -f4)

	echo "$TempP1"
	echo "$TempP2"
	echo "$TempP3"
	echo "$TempP4"

	TempP12=$(Dec2Bin $TempP1)
	TempP12=$(printf "%08d\n" $TempP12)            
	TempP22=$(Dec2Bin $TempP2)
	TempP22=$(printf "%08d\n" $TempP22)            
	TempP32=$(Dec2Bin $TempP3)
	TempP32=$(printf "%08d\n" $TempP32)            
	TempP42=$(Dec2Bin $TempP4)
	TempP42=$(printf "%08d\n" $TempP42)
	echo "$TempP42"
	IpAddress2="$TempP12$TempP22$TempP32$TempP42"

	echo "$IpAddress2"

	if [ $SubnetMask -ge 1 -a $SubnetMask -le 32 ]; then
		echo -e "\e[32m掩码位数:$SubnetMask 输入合规 \e[0m "
	else
		echo -e "\e[31m掩码位数:$SubnetMask 输入不合规,要求输入1-32之间数值 \e[0m "
		exit
	fi

	#获取网络地址
	netw=$(echo $IpAddress2 | cut -c -$SubnetMask)
	echo "netw  $netw"
	#获取主机位
	let nn=$SubnetMask+1
	host=$(echo $IpAddress2 | cut -c $nn-)
	#将主机位全部转换为0,得到地址段网络地址
	host=$(echo $host | sed 's/1/0/g')
	#将主机位全部转换为1,得到地址段广播地址
	brod=$(echo $host | sed 's/0/1/g')
	#拼接二进制网络地址
	net="$netw$host"
	#获取掩码
	mask=$(echo $netw | sed 's/0/1/g')
	mask="$mask$host"


	echo "net $net"
	echo "mask $mask"
	#拼接二进制广播地址
	broadcast="$netw$brod"
	#echo $broadcast
	#获取网段第1个可用地址
	fhost=$(echo $host | sed 's/0$/1/g')
	fhost="$netw$fhost"
	#获取网段最后1个可用地址
	lhost=$(echo $brod | sed 's/1$/0/g')
	lhost="$netw$lhost"
	#获取掩码
	mask=$(echo $netw | sed 's/0/1/g')
	mask="$mask$host"
	
	echo "地址$IpAddress 计算结果如下:"
	echo -e "网络地址是:\e[32m $(Toip $net) \e[0m"
	echo -e "地址掩码是:\e[32m $(Toip $mask) \e[0m"
	#echo -e "广播地址是:\e[32m $(Toip $broadcast) \e[0m"
	#echo -e "可用地址范围是:\e[32m $(Toip $fhost) - $(Toip $lhost) \e[0m"
}

Guess you like

Origin blog.csdn.net/qq_33191599/article/details/131598343