#!/bin/sh
#######################################################################
#            Reset all connections, using the local port,             #
# and the destruction of all the processes, associated with this port #
#                                                                     #
#         Сброс всех соединений, использующих локальный порт,         #
#        и уничтожение всех процессов, связанных с этим портом        #
#######################################################################

if [ $# != 1 ]; then
  echo "Usage $0 port"
  exit
fi

port=`echo "$1" | grep -E '^[0-9]+$'`

if [ -z "$port" ]; then
  echo "Usage $0 port"
  exit
fi

arg=`sockstat -c | grep ":$port" | awk '{print $6,$7}' | sed -e 's/ /|/g'`
for a in $arg; do
  host1=`echo $a | cut -f1 -d'|'`
  host2=`echo $a | cut -f2 -d'|'`
  port1=`echo $host1 | cut -f2 -d':'`
  port2=`echo $host2 | cut -f2 -d':'`
  host1=`echo $host1 | cut -f1 -d':'`
  host2=`echo $host2 | cut -f1 -d':'`
  
  if [ "$port1" -eq "$port" ] && [ -n "$host1" ] && [ -n "$port1" ] && [ -n "$host2" ] && [ -n "$port2" ] ; then 
    tcpdrop $host1 $port1 $host2 $port2
  fi
done

pid=`sockstat -l | grep ":$port" | awk '{print $3}'`
for a in $pid; do
  kill $a
done
