Create script file to delete ssh tunnel

by Daniel Pham
Published: Updated:
This entry is part 3 of 4 in the series Build SSH tunnel app with Zenity

In this article, I will show you how to create a file to delete ssh tunnel.

You understand this simple, in the previous article, you successfully created the file containing the ssh tunnel information. You use it to create tunnels to use.

So when you’re done using it, you need to delete that tunnel (although this is not required).

Script to delete ssh tunnel

Below is the demo content of the script file used to delete the ssh tunnels.

#!/bin/bash
options=(
"KILL 10000 : SERVER RDP 01 : 3389"
"KILL 10001 : SERVER RDP 02 : 3389"
"KILL 10002 : SERVER SSH 01 : 22"
"KILL 10003 : SERVER SSH 02 : 22"
"KILL 10004 : SERVER SSH 03 : 22"
"KILL 10005 : SERVER SSH 04 : 22"
)

while opt=$(zenity --width=300 --height=320 --title="KILL SSH TUNNEL" --text="Choose Tunnel" --list --column="List" "${options[@]}"); do
       case "$opt" in
       "${options[0]}" ) kill -9 $(ps aux | grep ssh | grep 10000: | awk '{print $2}');;
       "${options[1]}" ) kill -9 $(ps aux | grep ssh | grep 10001: | awk '{print $2}');;
       "${options[2]}" ) kill -9 $(ps aux | grep ssh | grep 10002: | awk '{print $2}');;
       "${options[3]}" ) kill -9 $(ps aux | grep ssh | grep 10003: | awk '{print $2}');;
       "${options[4]}" ) kill -9 $(ps aux | grep ssh | grep 10004: | awk '{print $2}');;
       "${options[5]}" ) kill -9 $(ps aux | grep ssh | grep 10005: | awk '{print $2}');;
        *) zenity --error --text="Choose again";;
       esac
   break
done

Explain this script

You see, this script is similar to the script to create ssh tunnel for using Zenity.

script to delete ssh tunnel
Script to delete ssh tunnel.

The only difference here is the command used in the options.

Here, we use the ps command and find the port we created the tunnel. From there, we find its PID and kill that PID process.

This command will grep process with service ssh and port 10000. For example, we created ssh tunnel with port 10000.

ps aux | grep ssh | grep 10000

This pipe will get PID of this tunnel.

awk '{print $2}'

And finally, we use kill command to kill this tunnel.

kill -9 $()

Conclusion

This article is still stopping at writing script files to create a GUI for the program. In the following articles, I will guide you to add scripts to the menu for ease of use.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Create file containing ssh tunnel informationNext part: Add scripts to the Start menu of Linux Mint »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.