Browse Source

Add kill-connections

master
JustAnotherArchivist 2 years ago
parent
commit
d193637e5e
1 changed files with 47 additions and 0 deletions
  1. +47
    -0
      kill-connections

+ 47
- 0
kill-connections View File

@@ -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}

Loading…
Cancel
Save