ns-vpn-x 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #! /bin/bash
  2. usage() {
  3. printf "Usage: %s [-t <namespace suffix>]\n" "$0"
  4. }
  5. namespace=ns-vpn
  6. while getopts t:h opt; do
  7. case $opt in
  8. h) usage
  9. exit
  10. ;;
  11. ?) 2>&1 usage
  12. exit 1
  13. ;;
  14. esac
  15. done
  16. netns_list=( $( find /run/netns/ -type f -regex ".*ns-vpn.*" -exec basename -a {} + | sort ) )
  17. case "${#netns_list[@]}" in
  18. 0)
  19. exit 0
  20. ;;
  21. 1)
  22. namespace="${netns_list[0]}"
  23. ;;
  24. [1-9]*)
  25. namespace=$( for ns in "${netns_list[@]}"; do printf '%s\n' "$ns"; done | dmenu -l -8 )
  26. ;;
  27. *)
  28. exit 1
  29. ;;
  30. esac
  31. task=$(dmenu -l 8 << EOF
  32. browser
  33. screen
  34. terminal
  35. EOF
  36. )
  37. cmd=
  38. case $task in
  39. browser)
  40. cmd="firefox"
  41. ;;
  42. screen)
  43. cmd="st -e screen -S $namespace"
  44. ;;
  45. terminal)
  46. cmd="tabbed -cr2 st -w '' -e /bin/bash -l"
  47. ;;
  48. *)
  49. exit 1
  50. ;;
  51. esac
  52. exec sudo /usr/bin/ip netns exec $namespace su -c "DISPLAY=$DISPLAY $cmd" $USER