etherswitch.sh 707 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/env bash
  2. conf=~/.etherswitch
  3. switch_state=0
  4. function check_fn {
  5. type $1 &> /dev/null || {
  6. echo "$1 is undefined"
  7. exit 1
  8. }
  9. }
  10. source $conf
  11. # handle SIGTERM
  12. trap 'exit' 15
  13. if [ -z "$device" ]
  14. then
  15. echo "device is not set"
  16. exit 1
  17. fi
  18. # check if $device is a network device
  19. ip link show $device > /dev/null
  20. if [ $? -eq 1 ]; then exit 1; fi
  21. check_fn "on_switch_close"
  22. check_fn "on_switch_open"
  23. while :
  24. do
  25. ip link show $device | grep -q "LOWER_UP"
  26. if [ $? -eq 1 ]
  27. then
  28. if [ $switch_state -eq 1 ]
  29. then
  30. switch_state=0
  31. on_switch_open
  32. fi
  33. else
  34. if [ $switch_state -eq 0 ]
  35. then
  36. switch_state=1
  37. on_switch_close
  38. fi
  39. fi
  40. sleep 0.3
  41. done