how to close an established network connection in linux and osx
Solution 1
You could attach to your process using gdb and close()
the file descriptor corresponding to the connection. You can find out the file descriptor number using lsof -a -i4 -p $PID
.
Solution 2
tcpkill -i eth0 { expression }
(basic command/options)tcpkill -i eth0 port 21
(Kill all outgoing ftp (port 21) connection)tcpkill host 192.168.1.2
or
tcpkill host hostname.domainname.com
(Kill all all packets arriving at or departing from host 192.168.1.2)tcpkill ip host 192.168.1.2 and not 192.168.1.111
(To kill all IP packets between 192.168.1.2 and any host except 192.168.1.111)
Related videos on Youtube
Rodrigo Gonzalez
Updated on September 18, 2022Comments
-
Rodrigo Gonzalez over 1 year
I used to do this in the Windows world with tool called TCPView: just select a established connection (ESTABLISHED), then select "Close Connection". I can't find a way to this on Mac OSX or Linux (I need both). The only way I know of is to kill the process that has the port open.
My main use case is to simulate interruptions in connectivity to test failover, like unplugging a cable or losing a router or middleware situations.
-
terdon about 11 yearsKilling the process is not good enough?
-
-
Rodrigo Gonzalez about 11 yearsworks! too bad gdb pauses the program, but I'll look more into that