Menu

[r10367]: / branches / 0.7exp / etc / util.subr  Maximize  Restore  History

Download this file

107 lines (89 with data), 2.2 kB

# Copyright (c) 2007-2008 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 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_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)
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.