Advertisement
tolikpunkoff

ndstatus (network devices status check)

Mar 8th, 2019
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.11 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #check network device status
  4. #exit codes 0 - device up 1 - device down 2 - none device
  5. #3 - help or wrong parameters
  6.  
  7. SLNT=0
  8.  
  9. print_help()
  10. {
  11.     echo "Use "`basename $0`" <device> [-s]"
  12.     echo "<device> - network device name, e.g. eth0"
  13.     echo "-s - silent mode, no console output"
  14. }
  15.  
  16. #parameters check and set silent mode
  17. if [ $# -eq 0 ]; then
  18.     echo "Wrong parameters!"
  19.     echo
  20.     print_help
  21.     exit 3
  22. else
  23.     if [ $# -eq 2 ]; then
  24.     if [[ "$2" == "-s" ]]; then
  25.         SLNT=1
  26.     else
  27.         echo "Wrong parameters!"
  28.         echo
  29.         print_help
  30.         exit 3
  31.     fi
  32.     fi
  33. fi
  34.  
  35. #print help
  36. if [[ "$1" == "--help" || "$1" == "-h" ]]; then
  37.     print_help
  38.     exit 3
  39. fi
  40.  
  41. DEV=$1
  42.  
  43. # check device exist
  44. DOWN=`ifconfig -a | grep $DEV -c`
  45. if [ $DOWN -eq 0 ]; then
  46.     if [ $SLNT -eq 0 ];then
  47.     echo "Device $DEV: NONE"
  48.     fi
  49.     exit 2
  50. fi
  51.  
  52. #check up/down status
  53. UP=`ifconfig | grep $DEV -c`
  54. if [ $UP -eq 0 ]; then #device down
  55.     if [ $SLNT -eq 0 ];then
  56.     echo "Device $DEV: DOWN"
  57.     fi
  58.     exit 1
  59. else
  60.     if [ $SLNT -eq 0 ];then
  61.     echo "Device $DEV: UP"
  62.     fi
  63.     exit 0
  64. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement