|
|
@@ -0,0 +1,47 @@ |
|
|
|
#!/bin/bash |
|
|
|
if [[ $# -ne 1 || "$1" == '-h' || "$1" == '--help' ]] |
|
|
|
then |
|
|
|
echo "Usage: kill-connections PID" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
if ! command -v tcp-closer &>/dev/null |
|
|
|
then |
|
|
|
echo "Error: could not find tcp-closer" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
declare -i pid="$1" |
|
|
|
if ! kill -0 ${pid} &>/dev/null |
|
|
|
then |
|
|
|
echo "Error: no process ${pid}" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
kill -STOP ${pid} |
|
|
|
|
|
|
|
echo "Open connections:" >&2 |
|
|
|
lsof -a -p ${pid} -i TCP -n >&2 |
|
|
|
echo >&2 |
|
|
|
|
|
|
|
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')" |
|
|
|
if [[ "${v4sports}" ]] |
|
|
|
then |
|
|
|
echo "Killing IPv4 connections" >&2 |
|
|
|
#TODO tcp-closer only supports up to 128 sports at once; split it up if there are more. |
|
|
|
#TODO This may also kill connections we want to keep. tcp-closer does not allow specifying the full (src, sport, dst, dport) tuple... |
|
|
|
tcp-closer -4 -s ${v4sports} |
|
|
|
echo >&2 |
|
|
|
fi |
|
|
|
v6sports="$(lsof -a -p ${pid} -i6 -i TCP -n -F nP0 | grep -Pao '\x00n\[[^\]]+\]:\K\d+' | tr '\n' ' ' | sed 's/,$//; s/ / -s /g')" |
|
|
|
if [[ "${v6sports}" ]] |
|
|
|
then |
|
|
|
echo "Killing IPv6 connections" >&2 |
|
|
|
tcp-closer -6 -s ${v6sports} |
|
|
|
echo >&2 |
|
|
|
fi |
|
|
|
|
|
|
|
echo "Open connections:" >&2 |
|
|
|
lsof -a -p ${pid} -i TCP -n >&2 |
|
|
|
|
|
|
|
kill -CONT ${pid} |