Skip to content

Instantly share code, notes, and snippets.

@derde
Last active July 15, 2022 17:40
Show Gist options
  • Save derde/2b758e13301a5964acc61f506a418a4f to your computer and use it in GitHub Desktop.
Save derde/2b758e13301a5964acc61f506a418a4f to your computer and use it in GitHub Desktop.
Reset idle TCP connection using gdb to send shutdown()
#! /bin/sh
# Reset TCP connections with gdb
PORT="$1"
HOST="$2"
[ "$1" ] || {
echo "Usage: $0 local-port host-ip-address"
echo "e.g. $0 192.168.0.55 80 # reset tcp connection from 192.168.0.55 to local port 80"
exit 1;
}
REGEXMATCH=""
for R in "$@" ; do
case "$R" in
*.*) REGEXMATCH="$REGEXMATCH -e $R:" ;;
*) REGEXMATCH="$REGEXMATCH -e :$R" ;;
esac
done
# users:(("tgtd",2338,40)) # older style ss output
# users:(("brave",pid=77829,fd=43)) # newer style ss output, tagged
ss -etpn |
egrep $REGEXMATCH |
sed '
/users:/ {
s/.*users:(("\([^"]*\)".*,pid=\([0-9]*\).*,fd=\([0-9]*\).*/\1\t\2\t\3/;
s/.*users:(("\([^"]*\)",\([0-9]*\),\([0-9]*\).*/\1\t\2\t\3/;
p; }; d ' |
{
while read _NAME _PID _FD ; do
echo "gdb -p $_PID --batch -ex 'call (int)shutdown(${_FD}u, 2)'; # Shut down socket $_FD in PID $_PID \"$_NAME\""
HITS=$((HITS+1))
done
[ "$HITS" ] || {
echo 1>&2 "No connections to port/host $*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment