2

Assuming I logged in with OpenSSH on port 22 and then also tunneled the ports 3306 and 80.

Port 80 was the last to be tunneled from all three (22, 3306, 80):

$ sudo ssh UWU@IP -L 80:localhost:80

How can I kill only the port 80 tunneling process?

I believe the best way would be to automatically target the port 80 tunnel somehow. An alternative is to target the last one of the 3 tunnels.

Why I ask this:

I can do ps aux | grep ssh, find the process, and then kill it specifically, but it's not automatic.

5
  • You could rely on the greater PID but it's not entirely reliable if it loops, but unlikely to happen on a 64 bit system. It would be much better to keep track of the commands you run and then you can be sure what you're reverting, as I suggested before you deleted the question. Dec 24, 2016 at 22:21
  • I always track commands I run and would usually prefer a specific way on a global one; In this case, from various reasons I will also accept a global way (last of all process). Anyway, I now edited the question and asked for a way to target the port 80 tunnel, in the killing. You are most welcome to answer.
    – user149572
    Dec 24, 2016 at 22:54
  • Are you wanting to create the tunnel and stop it all in the same script?
    – Guy
    Dec 24, 2016 at 23:13
  • Yes: The script runs the tunnel and adds some information, and then after 2 hours, closes the tunnel and deletes the data.
    – user149572
    Dec 24, 2016 at 23:21
  • Yeah and that question already has 2 solutions but OP still wanted other ways to ask for the same thing. Dec 24, 2016 at 23:24

4 Answers 4

4

I would suggest to target the PID of the relevant process with a $! syntax.

Stage 1: start the process but this time with a variable that holds an arbitrary PID value:

sudo ssh blah@blah 80:localhost:80 & arbpid=$!

Stage 2: If the variable arbpid is only saved when creating the tunnel, you could use it to target killing of the process in the end of your script:

kill $arbpid

In other words, what we did here was to mark the ssh tunnel with an arbitrary mark (which is a variable and its value) and we used that to target the killing.


Note that there is a single ampersand between the command and the variable. As with single ampersands after Unix commands, this is to make the command before the ampersand to run in a different sub process (background) while the second command (creating the variable in this case) will run in the current process (foreground).

2
  • That is exactly what I've been telling Benia in comments all day. They need to keep track of the processes they start. Dec 25, 2016 at 2:58
  • Basically you commented twice saying "Track processes"; If you meant that it's best to mark each of these processes in a unique way and then kill accordingly, instead of just killing the last one (without checking what it is) then indeed it's the best way to work and Guy demonstrated an interesting method to do that.
    – user149572
    Dec 25, 2016 at 3:47
3

If you know a unique string in the command line, you can use pkill:

pkill -f "80:localhost:80"

The -f flag causes pkill to search the entire command line; without that flag, it will only kill matches by process name, ignoring arguments.

1

The process providing the tunnel is the process listening on port 80, so use lsof or netstat or ss to find that.

lsof -n -i TCP:80 -sTCP:LISTEN -Fp |sed 's/^p//'
netstat -ntlp | awk '$4 ~ /:80$/ {sub(/\/.*/, "", $7); print $7}'
ss -nlpt 'sport = :80' | awk 'NR>1 {split($6, a, ","); print a[2]}'

Note that if you use connection multiplexing, this will kill all the connections provided by the same process. If you're using multiplexing, then you can tell the master process to stop forwarding with the cancel control command.

ssh UWU@IP -S … -O cancel -L 80:localhost:80
-3

This could use you to kill the last SSH tunneling process:

kill $(history | grep ssh | tail -n 2 | head -n 1| cut -d' ' -f 2)
2
  • This will not kill the right process if any. history returns the sequence number in its history, not the PID. Dec 24, 2016 at 23:27
  • Maybe kill $(pgrep ssh | tail -n 1) ?
    – Guy
    Dec 24, 2016 at 23:34

You must log in to answer this question.