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.
 
 
 

48 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. v4sports="$(lsof -a -p ${pid} -i4 -i TCP -n -F nP0 | grep -Pao '\x00n\d{1,3}(\.\d{1,3}){3}:\K\d+' | tr '\n' ' ' | sed 's/ $//; s/ / -s /g')"
  23. if [[ "${v4sports}" ]]
  24. then
  25. echo "Killing IPv4 connections" >&2
  26. #TODO tcp-closer only supports up to 128 sports at once; split it up if there are more.
  27. #TODO This may also kill connections we want to keep. tcp-closer does not allow specifying the full (src, sport, dst, dport) tuple...
  28. tcp-closer -4 -s ${v4sports}
  29. echo >&2
  30. fi
  31. v6sports="$(lsof -a -p ${pid} -i6 -i TCP -n -F nP0 | grep -Pao '\x00n\[[^\]]+\]:\K\d+' | tr '\n' ' ' | sed 's/ $//; s/ / -s /g')"
  32. if [[ "${v6sports}" ]]
  33. then
  34. echo "Killing IPv6 connections" >&2
  35. tcp-closer -6 -s ${v6sports}
  36. echo >&2
  37. fi
  38. echo "Open connections:" >&2
  39. lsof -a -p ${pid} -i TCP -n >&2
  40. kill -CONT ${pid}