#---------------------------------------------------------------------------- # /var/install/include/inetlib - library for eisfair scripts # # Creation : 2005-02-19 ansgar # Last update: $Id$ # # Copyright (c) 2005-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # --------------------------------------------------------------------------- # interface description # --------------------------------------------------------------------------- # # get_interfaces() get list of interfaces # get_interface() get name of interface # get_ipaddr() get IP address of interface # get_netmaskbits() get Netmaskbits of interface # get_netmask() get Netmask of interface # get_broadcast() get Boadcast address of interface # get_network() get Network of interface # get_ipaddr6() get IPv6 address of interface # get_netmaskbits6() get IPv6 Netmaskbits of interface # get_network6() get IPv6 Network of interface # # --------------------------------------------------------------------------- #============================================================================ # only include this file once #============================================================================ if [ "$_INETLIB" != "true" ] then _INETLIB='true' #============================================================================ # check-eisfair-version #============================================================================ if [ -z "${EISFAIR_SYSTEM}" ] then # get eisfair system-type . /var/install/include/check-eisfair-version fi # case "${EISFAIR_SYSTEM}" in # eisfair-1) # eisfair-1 code InterFace="ETH" # ;; # *) # code for other systems # InterFace="NET" # ;; # esac # # get_interfaces # ============== # # Parameters: -none- # # Output: list of interfaces # e.g. "eth0 eth1 eth1:0 ppp0" # get_interfaces() { _interfaces='' _interfaces="$( /usr/sbin/ip link show | /usr/bin/grep '^[^[:blank:]]' | /usr/bin/grep -v '<.*LOOPBACK.*>' | /usr/bin/grep -v 'tun' | /usr/bin/cut -d' ' -f2 | /usr/bin/cut -d: -f1 )" test -n "$_interfaces" && echo "$_interfaces" test -n "$_interfaces" return $? } # # get_interface # ============= # # Parameters: $1...$9 number # get_interface() { _retval=1 for i in $@ do if [ $i -ge 1 ] then _interface=$( get_interfaces | /usr/bin/tail -n +$i | /usr/bin/head -1 ) if test -n "$_interface" then _retval=0 echo "$_interface" fi fi done return $_retval } # # get_ipaddr # ========== # # Parameters: $1 number # or # interface # Index of value IP_NET__NAME # from /etc/config.d/base. # # Output: IP address of interface # # Attention: # This value may differ from the value # given in /etc/config.d/base e.g. # IP_NET__IPADDR # if a dynamic assignment was used # (eg. when using DHCP). # # Returns: 0 # get_ipaddr() { test -z "$1" && return 1 if echo "$1" | /usr/bin/grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get ip address _ip='' if /usr/sbin/ip -f inet addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet[[:blank:]]" then _ip="$( /usr/sbin/ip -f inet addr show ${_ipinterface} | /usr/bin/grep "inet[[:blank:]]" | /usr/bin/grep "${_ipinterface}$" | /usr/bin/tr -s ' ' | /usr/bin/cut -d' ' -f3 | /usr/bin/cut -d/ -f1 )" if test -n "$_ip" then echo "$_ip" return 0 fi fi return 1 } # # get_netmaskbits # =============== # # like get_ipaddr # # Output: Netmaskbits of interface # get_netmaskbits() { test -z "$1" && return 1 if echo "$1" | /usr/bin/grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get netmaskbits _maskbits='' if /usr/sbin/ip -f inet addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet[[:blank:]]" then _maskbits="$( /usr/sbin/ip -f inet addr show ${_ipinterface} | /usr/bin/grep "inet[[:blank:]]" | /usr/bin/grep "${_ipinterface}$" | /usr/bin/sed 's#^.*/##; s# .*$##' )" # tr -s ' ' | # cut -d/ -f2 | # cut -d' ' -f1)" if test -n "$_maskbits" \ -a "$_maskbits" -ge 1 \ -a "$_maskbits" -le 32 then echo "$_maskbits" return 0 fi fi return 1 } # # get_netmask # =========== # # like get_ipaddr # # Output: Netmask of interface # get_netmask() { _mask='' _maskbits=$( get_netmaskbits $1 ) if test $? -eq 0 -a -n "$_maskbits" then _mask=$( /usr/bin/netcalc netmask $_maskbits ) if test -n "$_mask" then echo "$_mask" return 0 fi fi return 1 } # # get_broacast # ============ # # like get_ipaddr # # Output: Boadcast address of interface # get_broadcast() { test -z "$1" && return 1 if echo "$1" | /usr/bin/grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get broadcast address _broadcast='' if /usr/sbin/ip -f inet addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet[[:blank:]]" then # legende for multiple grep # grep "${_ipinterface}$" -> ala eth0 or eth0:1 # grep 'brd' -> device 'tun' dont have a broadcast _broadcast="$( /usr/sbin/ip -f inet addr show ${_ipinterface} | /usr/bin/grep "${_ipinterface}$" | /usr/bin/grep 'brd' | /usr/bin/sed '/inet/s#^.*brd \(.*\) scope.*$#\1#' )" # grep "inet[[:blank:]]" | # grep "${_ipinterface}$" | # tr -s ' ' | # cut -d' ' -f5 )" if test -n "$_broadcast" then echo "$_broadcast" return 0 fi fi return 1 } # # get_network # =========== # # like get_ipaddr # # Output: Network of interface # get_network() { test -z "$1" && return 1 if echo "$1" | /usr/bin/grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get ip address and netmask _ip_mask='' _network='' if /usr/sbin/ip -f inet addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet[[:blank:]]" then _ip_mask="$( /usr/sbin/ip -f inet addr show ${_ipinterface} | /usr/bin/grep "inet[[:blank:]]" | /usr/bin/grep "${_ipinterface}$" | /usr/bin/tr -s ' ' | /usr/bin/cut -d' ' -f3 )" if test -n "$_ip_mask" then # calculate network _network=$( /usr/bin/netcalc network $_ip_mask ) if test -n "$_network" then echo "$_network" return 0 fi fi fi return 1 } # # get_ipaddr6 # =========== # # Parameters: $1 number # or # interface # Index of value IP_NET__NAME # from /etc/config.d/base. # # Output: IPv6 address of interface # # Attention: # This value may differ from the value # given in /etc/config.d/base e.g. # IP_NET__IPADDR # if a dynamic assignment was used # (eg. when using DHCP). # # Returns: 0 # get_ipaddr6() { test -z "$1" && return 1 if echo "$1" | grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get ip address _ip='' if /usr/sbin/ip -f inet6 addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet6[[:blank:]].*scope[[:blank:]]global" then _ip="$( /usr/sbin/ip -f inet6 addr show ${_ipinterface} | /usr/bin/grep "inet6[[:blank:]].*scope[[:blank:]]global" | /usr/bin/tr -s ' ' | /usr/bin/cut -d' ' -f3 | /usr/bin/cut -d/ -f1 )" if test -n "$_ip" then echo "$_ip" return 0 fi fi return 1 } # # get_netmaskbits6 # ================ # # like get_ipaddr6 # # Output: Netmaskbits of interface # get_netmaskbits6() { test -z "$1" && return 1 if echo "$1" | /usr/bin/grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get netmaskbits _maskbits='' if /usr/sbin/ip -f inet6 addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet6[[:blank:]].*scope[[:blank:]]global" then _maskbits="$( /usr/sbin/ip -f inet6 addr show ${_ipinterface} | /usr/bin/grep "inet6[[:blank:]].*scope[[:blank:]]global" | /usr/bin/tr -s ' ' | /usr/bin/cut -d' ' -f3 | /usr/bin/cut -d/ -f2)" if test -n "$_maskbits" \ -a "$_maskbits" -ge 1 \ -a "$_maskbits" -le 128 then echo "$_maskbits" return 0 fi fi return 1 } # # get_network6 # ============ # # like get_ipaddr6 # # Output: Network of interface # get_network6() { test -z "$1" && return 1 if echo "$1" | /usr/bin/grep -q '^[0-9]*$' then . /etc/config.d/base # get interface eval _ipinterface=\${IP_${InterFace}_${1}_NAME} test -z "$_ipinterface" && _ipinterface='eth'$((${1} - 1)) else _ipinterface=$1 fi # get ip address and netmask _ip_mask='' _network='' if /usr/sbin/ip -f inet6 addr show ${_ipinterface} 2>/dev/null | /usr/bin/grep -q "inet6[[:blank:]].*scope[[:blank:]]global" then _ip_mask="$( /usr/sbin/ip -f inet6 addr show ${_ipinterface} | /usr/bin/grep "inet6[[:blank:]].*scope[[:blank:]]global" | /usr/bin/tr -s ' ' | /usr/bin/cut -d' ' -f3)" if test -n "$_ip_mask" then # calculate network _network=$( /usr/bin/netcalc6 network $_ip_mask ) if test -n "$_network" then echo "$_network" return 0 fi fi fi return 1 } fi