1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. # trigger.sh
  3. myip() {
  4. ip addr \
  5. | grep 192 \
  6. | sed 's/^ *//' \
  7. | cut -d\ -f 2 \
  8. | cut -d/ -f 1
  9. }
  10. user=joe
  11. use_tcpdump=true
  12. use_nc_proxy=false
  13. delay=2
  14. verbose=false
  15. out=out/$(date +%Y%m%d-%H%M%S)
  16. srv_port=1234
  17. srv_host=$(myip)
  18. while true ; do
  19. case "$1" in
  20. -o) out="$2"; shift 2
  21. ;;
  22. -u) user="$2"; shift 2
  23. ;;
  24. -h) srv_host="$2"; shift 2
  25. ;;
  26. -p) srv_port="$2"; shift 2
  27. ;;
  28. -P) pxy_port="$2"; shift 2
  29. ;;
  30. -d) delay="$2"; shift 2
  31. ;;
  32. -t) use_tcpdump=true; shift 1
  33. ;;
  34. -T) use_tcpdump=false; shift 1
  35. ;;
  36. -n) use_nc_proxy=true; shift 1
  37. ;;
  38. -N) use_nc_proxy=false; shift 1
  39. ;;
  40. -v) verbose=true; shift 1
  41. ;;
  42. --) shift 1
  43. break;
  44. ;;
  45. *) break;
  46. ;;
  47. esac
  48. done
  49. tmp=$(mktemp -d)
  50. mkdir -p $out
  51. start_tcpdump() {
  52. local pcap=$out/traffic.pcap
  53. echo starting tcpdump
  54. yum -q -y install tcpdump &>/dev/null
  55. tcpdump -i any -w $pcap "port $srv_port" \
  56. &
  57. }
  58. start_nc_proxy() {
  59. local pxy_port=$(($srv_port - 1))
  60. local back=$tmp/back
  61. local sent=$out/sent
  62. local rcvd=$out/rcvd
  63. echo starting proxy
  64. mkfifo $back
  65. cat < $back \
  66. | nc -k -l -p $pxy_port \
  67. | tee $sent \
  68. | nc $srv_host $srv_port \
  69. | tee $rcvd \
  70. > $back \
  71. &
  72. }
  73. trigger() {
  74. echo adding + removing $user
  75. useradd $user
  76. userdel -r $user
  77. }
  78. $use_tcpdump && start_tcpdump
  79. $use_nc_proxy && start_nc_proxy
  80. sleep $delay
  81. trigger
  82. echo cleaning up
  83. $use_nc_proxy && killall ncat
  84. $use_tcpdump && killall tcpdump
  85. rm -rf $tmp