The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

55 lines
1.2 KiB

  1. #!/bin/bash
  2. if [[ $# -ne 1 || "$1" == '-h' || "$1" == '--help' ]]
  3. then
  4. echo "Usage: kill-connections PID" >&2
  5. exit 1
  6. fi
  7. if ! command -v tcp-closer &>/dev/null
  8. then
  9. echo "Error: could not find tcp-closer" >&2
  10. exit 1
  11. fi
  12. declare -i pid="$1"
  13. if ! kill -0 ${pid} &>/dev/null
  14. then
  15. echo "Error: no process ${pid}" >&2
  16. exit 1
  17. fi
  18. kill -STOP ${pid}
  19. echo "Open connections:" >&2
  20. lsof -a -p ${pid} -i TCP -n >&2
  21. echo >&2
  22. readarray -t v4sports < <(lsof -a -p ${pid} -i4 -i TCP -n -F nP0 | grep -Pao '\x00n\d{1,3}(\.\d{1,3}){3}:\K\d+' | sed 's,^,-s ,')
  23. if [[ ${#v4sports[@]} -gt 0 ]]
  24. then
  25. echo "Killing IPv4 connections" >&2
  26. #TODO This may also kill connections we want to keep. tcp-closer does not allow specifying the full (src, sport, dst, dport) tuple...
  27. for ((i=0; i<${#v4sports[@]}; i+=64))
  28. do
  29. tcp-closer -4 ${v4sports[@]:${i}:64}
  30. done
  31. echo >&2
  32. fi
  33. readarray -t v6sports < <(lsof -a -p ${pid} -i6 -i TCP -n -F nP0 | grep -Pao '\x00n\[[^\]]+\]:\K\d+' | sed 's,^,-s ,')
  34. if [[ ${#v6sports[@]} -gt 0 ]]
  35. then
  36. echo "Killing IPv6 connections" >&2
  37. for ((i=0; i<${#v6sports[@]}; i+=64))
  38. do
  39. tcp-closer -6 ${v6sports[@]:${i}:64}
  40. done
  41. echo >&2
  42. fi
  43. sleep 1
  44. echo "Open connections:" >&2
  45. lsof -a -p ${pid} -i TCP -n >&2
  46. kill -CONT ${pid}