etherswitch 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. conf=/etc/config/etherswitch
  3. switch_state=-1
  4. check_fn() {
  5. type $1 &> /dev/null || {
  6. >&2 echo "$1 is undefined"
  7. exit 1
  8. }
  9. }
  10. load_config() {
  11. unset -f on_switch_close
  12. unset -f on_switch_open
  13. unset -f on_switch_toggle
  14. source $conf
  15. if [ -z "$device" ]
  16. then
  17. >&2 echo "device is not set"
  18. exit 1
  19. fi
  20. if [ -z "$port" ]
  21. then
  22. >&2 echo "port is not set"
  23. exit 1
  24. fi
  25. # check if $device is a network device
  26. swconfig dev $device show > /dev/null
  27. if [ $? -eq 1 ]; then exit 1; fi
  28. check_fn "on_switch_close"
  29. check_fn "on_switch_open"
  30. check_fn "on_switch_toggle"
  31. }
  32. # handle SIGTERM
  33. trap 'exit' 15
  34. # handle SIGHUP (config reload)
  35. trap 'load_config' 1
  36. load_config
  37. echo $$ > /var/run/etherswitch.pid
  38. while :
  39. do
  40. swconfig dev $device port $port show | grep -q "up"
  41. current_state=$?
  42. if [ $current_state -ne $switch_state ]
  43. then
  44. switch_state=$current_state
  45. on_switch_toggle
  46. if [ $switch_state -eq 1 ]
  47. then
  48. on_switch_open
  49. else
  50. on_switch_close
  51. fi
  52. fi
  53. sleep 1
  54. done