Kill TCP connections on a Mac in Terminal

I just want to kill a specific tcp connection on my mac (similar to tcpkill on linux). By port or ip, preferably port but i'll settle. I don't want to pay for a service or download another software, I want the simple terminal command that will allow me to do this like tcpkill.

I've tried fixing up the rules on my firewall, and tried to make a bash script so it would run but to no success because the tutorial I found was very old.

Why does this seem to be such a difficult task to find information on, or do?


Solution 1:

Option 1

You can install tcpkill. It is part of the dsniff package which is in MacPorts. You said you don't want to install other software. Nevertheless MacPorts is really useful if you work from the command line. I suggest you check it out.

Option 2

On StackOverflow there is a related post: Find (and kill) process locking port 3000 on Mac. The suggested solution there is to create a function that kills all connection on a specific port:

function killport() { lsof -i TCP:$1 | awk '/LISTEN/ {print $2}' | xargs kill -9 }

If you change the function by taking away the argument the function should kill all applications that have an open TCP connection.

function killport() { lsof -i TCP | awk '/LISTEN/ {print $2}' | xargs kill -9 }

Disclaimer: I haven't tested this.

Disclaimer 2: It's a brute force method and can have some serious side effects. I would recommend against this approach.