@@ -123,6 +123,19 @@ start_tunnel() {
123123 fi
124124}
125125
126+ # Function to get SSH process ID for a tunnel
127+ get_ssh_pid () {
128+ local name=$1
129+ local config=$( get_tunnel_config " $name " )
130+ local local_port=$( echo " $config " | cut -d: -f2)
131+ local remote_host=$( echo " $config " | cut -d: -f3)
132+ local remote_port=$( echo " $config " | cut -d: -f4)
133+ local ssh_server=$( echo " $config " | cut -d: -f5)
134+
135+ # Look for SSH process with our specific tunnel parameters
136+ ps aux | grep " ssh -L ${local_port} :${remote_host} :${remote_port} ${ssh_server} " | grep -v grep | awk ' {print $2}'
137+ }
138+
126139# Function to stop a single tunnel
127140stop_tunnel () {
128141 local name=$1
@@ -132,21 +145,67 @@ stop_tunnel() {
132145 return 1
133146 fi
134147
135- if ! screen -list | grep -q " ssh_tunnel_${name} " ; then
148+ local screen_running=false
149+ local ssh_pids=" "
150+
151+ # Check if screen session exists
152+ if screen -list | grep -q " ssh_tunnel_${name} " ; then
153+ screen_running=true
154+ fi
155+
156+ # Get SSH process IDs
157+ ssh_pids=$( get_ssh_pid " $name " )
158+
159+ if [ " $screen_running " = false ] && [ -z " $ssh_pids " ]; then
136160 echo -e " ${YELLOW} Tunnel '${name} ' is not running${NC} "
137161 return 1
138162 fi
139163
140164 echo -e " ${BLUE} Stopping tunnel: ${name}${NC} "
141- screen -S " ssh_tunnel_${name} " -X quit
165+
166+ # First, try to quit the screen session gracefully
167+ if [ " $screen_running " = true ]; then
168+ echo -e " ${BLUE} Stopping screen session...${NC} "
169+ screen -S " ssh_tunnel_${name} " -X stuff " ^C"
170+ sleep 1
171+ screen -S " ssh_tunnel_${name} " -X quit
172+ sleep 1
173+ fi
174+
175+ # Kill any remaining SSH processes
176+ if [ -n " $ssh_pids " ]; then
177+ echo -e " ${BLUE} Terminating SSH processes...${NC} "
178+ for pid in $ssh_pids ; do
179+ if kill -0 " $pid " 2> /dev/null; then
180+ echo -e " ${BLUE} Killing SSH process $pid ${NC} "
181+ kill " $pid " 2> /dev/null
182+ sleep 1
183+ # If still running, force kill
184+ if kill -0 " $pid " 2> /dev/null; then
185+ kill -9 " $pid " 2> /dev/null
186+ fi
187+ fi
188+ done
189+ fi
142190
143191 sleep 1
144192
145- if ! screen -list | grep -q " ssh_tunnel_${name} " ; then
193+ # Verify tunnel is stopped
194+ local still_running=false
195+ if screen -list | grep -q " ssh_tunnel_${name} " ; then
196+ still_running=true
197+ fi
198+
199+ ssh_pids=$( get_ssh_pid " $name " )
200+ if [ -n " $ssh_pids " ]; then
201+ still_running=true
202+ fi
203+
204+ if [ " $still_running " = false ]; then
146205 echo -e " ${GREEN} ✓ Tunnel '${name} ' stopped successfully${NC} "
147206 return 0
148207 else
149- echo -e " ${RED} ✗ Failed to stop tunnel '${name} '${NC} "
208+ echo -e " ${RED} ✗ Failed to completely stop tunnel '${name} '${NC} "
150209 return 1
151210 fi
152211}
@@ -158,8 +217,24 @@ show_status() {
158217
159218 for config in " ${TUNNEL_CONFIGS[@]} " ; do
160219 local name=$( echo " $config " | cut -d: -f1)
220+ local screen_running=false
221+ local ssh_running=false
222+
223+ # Check screen session
161224 if screen -list | grep -q " ssh_tunnel_${name} " ; then
225+ screen_running=true
226+ fi
227+
228+ # Check SSH process
229+ local ssh_pids=$( get_ssh_pid " $name " )
230+ if [ -n " $ssh_pids " ]; then
231+ ssh_running=true
232+ fi
233+
234+ if [ " $screen_running " = true ] && [ " $ssh_running " = true ]; then
162235 echo -e " ${name} : ${GREEN} RUNNING${NC} "
236+ elif [ " $screen_running " = true ] || [ " $ssh_running " = true ]; then
237+ echo -e " ${name} : ${YELLOW} PARTIAL${NC} (screen: $screen_running , ssh: $ssh_running )"
163238 else
164239 echo -e " ${name} : ${RED} STOPPED${NC} "
165240 fi
@@ -168,6 +243,10 @@ show_status() {
168243 echo " "
169244 echo -e " ${BLUE} Active screen sessions:${NC} "
170245 screen -list | grep " ssh_tunnel_" || echo " None"
246+
247+ echo " "
248+ echo -e " ${BLUE} Active SSH tunnel processes:${NC} "
249+ ps aux | grep " ssh -L" | grep -v grep || echo " None"
171250}
172251
173252# Function to start all tunnels
0 commit comments