PID from background ssh

Hello. I was wondering if someone can help me out with something. To simplify my life, I have written a tiny script to open an ssh tunnel through another linux host so that I can access the esxi hosts on that network using the client. For this I have to tunnel ports 443, 902, and 903. Here is what I have done so far:

#!/bin/bash
# $1 is the address of the esxi server. Tunnels ports 443, 902, and 903

ssh -f ccox@someserver.somedomain.com -L 443:$1:443 -N
ssh -f ccox@someserver.somedomain.com -L 902:$1:902 -N
ssh -f ccox@someserver.somedomain.com -L 903:$1:903 -N

What I would like to do is have each ssh line record its PID so that I can run a kill script that will get rid of those connections instantly. This will make it easy to jump to the next esxi host.

Does anyone have suggestions? Or even ways to improve this script? lol

Hi,

It's possible to get the pid of the last bakcground process with the command '$!'. I don't know if can be useful for your problem.

$ cat script.sh
#!/usr/bin/env bash

sleep 10 & bg_pid=$!
echo "$bg_pid"
wait
$ bash script.sh
3927

Regards,
Birei