1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-22 22:04:08 +03:00

config: add some ipv4 helper shell functions

Many thanks to Michael Adam <obnox@samba.org>
for the basic work.

metze

(This used to be ctdb commit ff9c641763702ae99632bbf4d0825d578440c074)
This commit is contained in:
Stefan Metzmacher 2009-12-11 19:56:36 +01:00
parent 4493ba6ffa
commit 2ecf8053f9

View File

@ -658,6 +658,70 @@ ctdb_standard_event_handler ()
esac
}
ipv4_host_addr_to_net_addr()
{
local HOST=$1
local MASKBITS=$2
local HOST0=$(echo $HOST | awk -F . '{print $4}')
local HOST1=$(echo $HOST | awk -F . '{print $3}')
local HOST2=$(echo $HOST | awk -F . '{print $2}')
local HOST3=$(echo $HOST | awk -F . '{print $1}')
local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
local NET0=$(( $NET_NUM & 255 ))
local NET1=$(( ($NET_NUM & (255 * 256)) / 256 ))
local NET2=$(( ($NET_NUM & (255 * 256**2)) / 256**2 ))
local NET3=$(( ($NET_NUM & (255 * 256**3)) / 256**3 ))
echo "$NET3.$NET2.$NET1.$NET0"
}
ipv4_maskbits_to_net_mask()
{
local MASKBITS=$1
local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
local MASK0=$(( $MASK_NUM & 255 ))
local MASK1=$(( ($MASK_NUM & (255 * 256)) / 256 ))
local MASK2=$(( ($MASK_NUM & (255 * 256**2)) / 256**2 ))
local MASK3=$(( ($MASK_NUM & (255 * 256**3)) / 256**3 ))
echo "$MASK3.$MASK2.$MASK1.$MASK0"
}
ipv4_is_valid_addr()
{
local ADDR=$1
local fail=0
local N=`echo $ADDR | sed -e 's/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*//'`
test -n "$N" && fail=1
local ADDR0=$(echo $ADDR | awk -F . '{print $4}')
local ADDR1=$(echo $ADDR | awk -F . '{print $3}')
local ADDR2=$(echo $ADDR | awk -F . '{print $2}')
local ADDR3=$(echo $ADDR | awk -F . '{print $1}')
test "$ADDR0" -gt 255 && fail=1
test "$ADDR1" -gt 255 && fail=1
test "$ADDR2" -gt 255 && fail=1
test "$ADDR3" -gt 255 && fail=1
test x"$fail" != x"0" && {
#echo "IPv4: '$ADDR' is not a valid address"
return 1;
}
return 0;
}
########################################################
# load a site local config file
########################################################