Sometimes we need to check the correctness of IP input and the correctness of the network mask, written in shell script:
# Verify IP address correctness check_ip_format () {echo $1 | grep "^ [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \} $ ">/dev/null if [$? = 1]; thenreturn 1 else a = 'echo $1 | awk-f. '{print $1}' 'B = 'echo $1 | awk-f. '{print $2}' C = 'echo $1 | awk-f. '{print $3} 'd = 'echo $1 | awk-f. '{print $4}' If [$ A-ge 255-o $ A-Le 0-o $ B-GT 255-o $ B-lt 0-o $ C -GT 255-o $ C-lt 0-o $ D-GT 255-o $ D-Le 0]; thenreturn 2; FI # verify whether the multicast address is [$ A-ge 224-A $ A-Le 239]; thenreturn 3fi # verify the loop if [$ A-EQ 127]; thenreturn 4 fifireturn 0} # convert a number to a binary number D2b () {num = "$1" d2b = 'echo "obase = 2; $ num" | BC 'echo $ d2b} # verify the correctness of the mask check_mask_format () {echo $1 | grep "^ [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \} $ ">/dev/null if [$? = 1]; thenreturn 1fia = 'echo $1 | awk-f. '{print $1}' 'B = 'echo $1 | awk-f. '{print $2}' C = 'echo $1 | awk-f. '{print $3} 'd = 'echo $1 | awk-f. '{print $4} 'a = 'd2b $ a' B = 'd2b $ B' c = 'd2b $ C' d = 'd2b $ D' for I in $ A $ B $ C $ D; do [[$ I! = 0 & $ {# I }! = 8] & return 2 donemask = $ A $ B $ C $ d [["$ mask" = ~ ^ 1 [1] * [0] * $] | return 3 return 0}