tankdthedruid

ifchk.sh

Nov 8th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.36 KB | None | 0 0
  1. #!/bin/bash
  2. #============================================================
  3. #
  4. #          FILE:  ifchk
  5. #
  6. #         USAGE:  ./ifchk
  7. #
  8. #   DESCRIPTION: Formated network interface information for better readability. More options
  9. #                          planned to be coded in subsequent releases.
  10. #
  11. #       OPTIONS:  ---
  12. #  REQUIREMENTS:  ---
  13. #          BUGS:  ---
  14. #         NOTES:  Development of this tool is still in progress.
  15. #        AUTHOR:   (tankd),
  16. #       COMPANY: ---
  17. #       VERSION:  1.0
  18. #       CREATED:  10/27/11 01:42:08 PST
  19. #      REVISION:  ---
  20. #============================================================
  21.  
  22. # Test for root permissions and terminate
  23. if [[ $EUID -ne 0 ]]; then
  24.    echo "This script must be run as root" 1>&2
  25.        exit 1
  26. fi
  27.  
  28. # Get a list of activated interfaces
  29. ifconfig | grep -E --only-matching '(eth[0-9]+)' >> /tmp/interface.lst
  30. ifconfig | grep -E --only-matching '(wlan[0-9]+)' >> /tmp/interface.lst
  31. ifconfig | grep -E --only-matching '(ppp[0-9]+)' >> /tmp/interface.lst
  32. ifconfig | grep -E --only-matching '(vboxnet[0-9]+)' >> /tmp/interface.lst
  33. ifconfig | grep -E --only-matching '(vmnet[0-9]+)' >> /tmp/interface.lst
  34. ifconfig | grep -E --only-matching '(lo)' >> /tmp/interface.lst
  35.  
  36. # Create variable $INT from user input
  37. INT=$1
  38.  
  39. # Test for empty variable $INT and terminate
  40. if [ $INT = "" ]; then
  41.     echo ERROR: No interface specified.
  42.     exit 1
  43. fi
  44.  
  45. # Test user input against generated interface list
  46. for i in `cat /tmp/interface.lst`; do
  47.         if [ $i = ${INT} ]; then
  48.                 STR_CHK=True
  49.                 break
  50.         else
  51.                 STR_CHK=False
  52.         fi
  53. done
  54.  
  55. # If user input does not match any interfaces on the list terminate
  56. if [ $STR_CHK = False ]; then
  57.     echo ERROR: Unrecognized interface
  58.         exit 1
  59. fi
  60.  
  61. # Display all available interfaces
  62. # cat /tmp/interfaces.lst
  63.  
  64. # Enumerate the interface configuration and set variables
  65. #
  66. # Connection Media
  67. MEDIA=$(ifconfig ${INT} | grep -E --only-matching '(encap\:[A-Za-z]+)' | sed 's/encap[:]//g')
  68.  
  69. # Inet Address
  70. INET=$(ifconfig ${INT} | grep -E --only-matching '(addr\:)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' | sed 's/addr[:]//g')
  71.  
  72. # MAC Address
  73. MAC=$(ifconfig ${INT} | grep -E --only-matching '([a-z0-9][a-z0-9]\:[a-z0-9][a-z0-9]\:[a-z0-9][a-z0-9]\:[a-z0-9][a-z0-9]\:[a-z0-9][a-z0-9]\:[a-z0-9][a-z0-9])')
  74.  
  75. # Subnet Mask
  76. MASK=$(ifconfig ${INT} | grep -E --only-matching '(Mask\:)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' | sed 's/Mask[:]//g')
  77.  
  78. # Username
  79. USER=$(whoami)
  80.  
  81. # Hostname
  82. HOST=$(hostname)
  83.  
  84. # Display gathered information
  85. echo Interface Information - ${INT}
  86. echo
  87. echo Connection Type - $MEDIA
  88. echo
  89. echo IPv4 - $INET
  90. echo
  91. echo Netmask - $MASK
  92. echo
  93. echo MAC Address - $MAC
  94.  
  95. # External IP
  96. NAT=$(curl 'http://automation.whatismyip.com/n09230945.asp')
  97.  
  98. # ARIN Search
  99. WHOIS=$(curl http://whois.arin.net/rest/ip/${NAT}.txt)
  100.  
  101.  
  102. # Variables established list
  103. #
  104. # $0*
  105. # $INT
  106. # $MEDIA
  107. # $INET
  108. # $MAC
  109. # $MASK
  110. # $NAT
  111. # $WHOIS
  112.  
  113.  
  114. # Test for Linux distro, kernel, and base
  115. DIST=$(cat /etc/*-release | grep 'DISTRIB_ID' | sed 's/DISTRIB\_ID\=//g')
  116. VER=$(cat /etc/*-release | grep 'DISTRIB_RELEASE' | sed 's/DISTRIB\_RELEASE\=//g')
  117. BASE=$(cat /etc/*-release | grep 'DISTRIB_CODENAME' | sed 's/DISTRIB\_CODENAME\=//g')
  118. KERNEL=$(uname -r)
  119.  
  120. echo 'DISTRO=${DIST}'
  121. echo 'VERSION=${VER}'
  122. echo 'KERNEL=${KERNEL}'
  123. echo 'BASE=${BASE}'
Advertisement
Add Comment
Please, Sign In to add comment