FreeNAS Code
This project has moved to github - see https://github.com/freenas
Brought to you by:
cochard,
mattolander
# Copyright © 2007 Volker Theile (votdev@gmx.de) # All rights reserved. # # util.subr # functions used by various rc scripts # # get_if [interface] # Get the interface. If it is set to 'auto' use the # first interface found. get_if() { local _interface _iflist _interface=$1 case ${_interface} in [Aa][Uu][Tt][Oo]) _iflist=`/sbin/ifconfig -l` _interface=${_iflist% *} ;; esac echo ${_interface} } # get_ipaddr [protocol_family] [interface] # Get IP address for given protocol family and interface. # # Example: # get_ipaddr inet eth0 # get_ipaddr inet6 auto get_ipaddr() { local _protocol_family _interface _protocol_family=$1 _interface=`get_if $2` echo `/usr/bin/netstat -inW -f ${_protocol_family} -I ${_interface} | grep ${_interface} | awk '{print $4}'` } # get_subnetmask [address] # Get subnet mask from given IP address. # Return subnet mask as string. # # Example: # get_subnetmask 192.168.0.1/16 # get_subnetmask 3ffe:beef:13e1:4c92::cd90/48 get_subnetmask() { _data=`/usr/local/bin/sipcalc $@` _protocol=`cat ${_data} | head -n 1 | awk '{print $1}'` _protocol=${_protocol#-[*} case ${_protocol} in ipv4) echo `cat ${_data} | grep "Network mask" | head -n 1 | awk '{print $4}'` ;; ipv6) echo `cat ${_data} | grep "Prefix address" | awk '{print $4}'` ;; esac } # is_validip [protocol_family] [address] # Check if given IP is valid. # Return 0 if valid, nonzero otherwise. # # Example: # is_validip inet 192.168.0.1/24 # is_validip inet 192.168.0.1 # is_validip inet6 3ffe:beef:13e1:4c92::cd90/48 is_validip() { _protocol=$1 shift 1 _result=`/usr/local/bin/sipcalc $@ | head -n 1 | awk '{print $1}'` _result=${_result#-[*} if [ "ipv4" = "${_result}" -a "inet" = "${_protocol}" ]; then return 0 fi if [ "ipv6" = "${_result}" -a "inet6" = "${_protocol}" ]; then return 0 fi return 1 }