FreeNAS Code
This project has moved to github - see https://github.com/freenas
Brought to you by:
cochard,
mattolander
# Copyright (c) 2007-2009 Volker Theile (votdev@gmx.de) # All rights reserved. # # util.subr # functions used by various rc scripts # # get_if [interface] # Get the interface. If set to 'auto' use the # first interface found. get_if() { local _interface _interface=$1 case ${_interface} in [Aa][Uu][Tt][Oo]) _interface=`/sbin/ifconfig -l | /usr/bin/awk '{print $1}'` ;; esac echo ${_interface} } # get_ipaddr [protocol_family] [interface] # Get IP address for the 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} | /usr/bin/grep ${_interface} | /usr/bin/awk '{print $4}'` } # get_macaddr [interface] # Get MAC address for the given interface. # Example: # get_ipaddr eth0 # get_ipaddr auto get_macaddr() { local _interface _interface=`get_if $1` echo `/usr/bin/netstat -inW -f link -I ${_interface} | tail -n 1 | /usr/bin/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() { local _protocol _sipcalc=`/usr/local/bin/sipcalc $@` _protocol=`/bin/expr -- "${_sipcalc}" : '-\[\(ipv[46]\).*'` case ${_protocol} in ipv4) echo `/usr/local/bin/sipcalc $@ | /usr/bin/grep "Network mask" | /usr/bin/head -n 1 | /usr/bin/awk '{print $4}'`; return 0; ;; ipv6) echo `/usr/local/bin/sipcalc $@ | /usr/bin/grep "Prefix address" | /usr/bin/awk '{print $4}'`; return 0; ;; esac return 1 } # is_validip [protocol_family] [address] # Check if given IP is valid. # Return 0 if valid, non-zero 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() { local _protocol_family _protocol _protocol_family=$1 shift 1 _sipcalc=`/usr/local/bin/sipcalc $@` _protocol=`/bin/expr -- "${_sipcalc}" : '-\[\(ipv[46]\).*'` case ${_protocol} in ipv4) [ "inet" = "${_protocol_family}" ] && return 0; ;; ipv6) [ "inet6" = "${_protocol_family}" ] && return 0; ;; esac return 1 } # get_product_name # Get the product name. # Return product name string. get_product_name() { echo $(cat /etc/prd.name) } # get_vmtype # Get the virtual machine type. # Return virtual machine type string. # none = unknown or physical get_vmtype() { local _type _smmaker _smproduct _smversion; _smmaker=`/bin/kenv -q smbios.system.maker` _smproduct=`/bin/kenv -q smbios.system.product` _smversion=`/bin/kenv -q smbios.system.version` _type="none"; if [ "$_smproduct" = "VMware Virtual Platform" ]; then _type="vmware"; elif [ "$_smproduct" = "VirtualBox" ]; then _type="vbox"; elif [ "$_smproduct" = "Virtual Machine" -a "$_smmaker" = "Microsoft Corporation" ]; then if [ "$_smversion" = "7.0" ]; then _type="hv2"; elif [ "$_smversion" = "5.0" ]; then _type="hv1"; elif [ "$_smversion" = "VS2005R2" ]; then _type="vs"; else _type="msvm"; fi # elif [ -x /usr/local/bin/vmware-checkvm ] && /usr/local/bin/vmware-checkvm > /dev/null; then # _type="vmware"; fi echo "$_type"; }