interfaces.sh

  1. #!/bin/sh
  2.  
  3. # BEFORE: LOGIN
  4.  
  5. ################################################################################
  6. #
  7. # interfaces.sh (display all interfaces with and without an IP address)  
  8. #
  9. ################################################################################
  10. # Copyright (c) 2016, Manuel Strauch (manuel.strauch@outlook.com)
  11. # All rights reserved.
  12. #
  13. # Redistribution and use in source and binary forms, with or without
  14. # modification, are permitted provided that the following conditions are met:
  15. #
  16. #  -Redistributions of source code must retain the above copyright notice,
  17. #   this list of conditions and the following disclaimer.
  18. #  -Redistributions in binary form must reproduce the above copyright notice,
  19. #   this list of conditions and the following disclaimer in the documentation
  20. #   and/or other materials provided with the distribution.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  26. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. # POSSIBILITY OF SUCH DAMAGE.
  33. ################################################################################
  34.  
  35.  
  36. ###  functions
  37. ################################################################################
  38. ################################################################################
  39. screenoutput() {
  40.   if [ "`echo $i | wc -m | sed -e 's/ //g'`" = "6" ]; then
  41.     echo -e "                ${blue}   ${i}${normal}       |    ${yellow} ${j}${normal}"
  42.   else
  43.     if [ "`echo $i | wc -m | sed -e 's/ //g'`" = "5" ]; then
  44.       echo -e "                ${blue}   ${i}${normal}        |    ${yellow} ${j}${normal}"
  45.     else
  46.       echo -e "                ${blue}   ${i}${normal}         |    ${yellow} ${j}${normal}"
  47.     fi
  48.   fi
  49. }
  50.  
  51. ###  start program
  52. ################################################################################
  53. ################################################################################
  54.  
  55. case "$1" in
  56.   start)
  57.  
  58. ###  needed variables
  59. ################################################################################
  60. ################################################################################
  61.  
  62.     . /opt/etc/system.var
  63.     gateway="`/sbin/route -n get default 2>/dev/null | grep "gateway" | sed 's/ //g' | cut -f2 -d':'`"
  64.     if [ -z "${gateway}" ]; then
  65.       gateway="not available"
  66.     fi
  67.     all_interfaces="`/sbin/ifconfig | /usr/bin/egrep "^[a-z][a-z]?[a-z]?[a-z][0-9]" | \
  68.      cut -f1 -d':'`"
  69.     j="----====----"
  70.  
  71. ### interfaces without a IP
  72. ################################################################################
  73. ################################################################################
  74.  
  75.     echo
  76.     echo "           ========================================="
  77.     echo -e "           ${red}NO IP   ${blue}Interface  ${normal} |    ${yellow} IP-Address${normal}"
  78.     echo "           -----------------------------------------"
  79.     for i in ${all_interfaces}; do
  80.       noip="false"
  81.       /sbin/ifconfig ${i} | /usr/bin/grep "inet" >/dev/null 2>&1 || noip="true"
  82.       if [ "${noip}" == "true" ]; then
  83.         screenoutput "{@}"
  84.       fi
  85.     done
  86.  
  87. ### interfaces with a IPV4 address
  88. ################################################################################
  89. ################################################################################
  90.  
  91.     echo "           ========================================="
  92.     echo -e "           ${red}IPV4    ${blue}Interface  ${normal} |    ${yellow} IP-Address${normal}"
  93.     echo "           -----------------------------------------"
  94.     for i in ${all_interfaces}; do
  95.       ips="`/sbin/ifconfig ${i} | /usr/bin/grep "inet " | cut -f2 -d' '`"
  96.       for j in ${ips}; do
  97.         screenoutput "{@}"
  98.       done
  99.     done
  100.  
  101. ### interfaces with a IPV6 address
  102. ################################################################################
  103. ################################################################################
  104.  
  105.     echo "           ========================================="
  106.     echo -e "           ${red}IPV6    ${blue}Interface  ${normal} |    ${yellow} IP-Address${normal}"
  107.     echo "           -----------------------------------------"
  108.     for i in ${all_interfaces}; do
  109.       ips="`/sbin/ifconfig ${i} | /usr/bin/grep "inet6" | cut -f2 -d' '`"
  110.       for j in ${ips}; do
  111.         screenoutput "{@}"
  112.       done
  113.     done
  114.     echo "           ========================================="
  115.     echo -e "          ${red} Default ${blue}Gateway  ${normal}   |   ${yellow}  ${gateway} ${normal}"
  116.     echo "           -----------------------------------------"
  117.     echo            
  118.   ;;
  119.   stop)
  120.  
  121.   ;;
  122.   *)
  123.  
  124. ### dummy to start the script at runtime without the option "start"
  125. ################################################################################
  126. ################################################################################
  127.  
  128.     $0 start
  129.   ;;
  130. esac
  131.  
  132.  
  133. exit 0