Create script file to delete ssh tunnel

Share this post to your friends !

This entry is part 3 of 4 in the series Build SSH tunnel app with Zenity
Create script file to delete ssh tunnel
5 (100%) 1 vote[s]

WriteBash - 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.

create-file-to-delete-ssh-tunnel Create script file 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.

Continue reading the series«« Previous part: Create file containing ssh tunnel informationNext part: Add scripts to the Start menu of Linux Mint »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.