Create file containing ssh tunnel information

Share this post to your friends !

This entry is part 2 of 4 in the series Build SSH tunnel app with Zenity

WriteBash - In this article, I will show you how to create a file containing ssh tunnel information.

This file will contain information to help you connect to servers inside your system.

The script file contains ssh tunnel commands

First, create a script file named tunnel-info.sh and place it in the directory you want. For example, I put it in the writebash directory.

Look at the contents of the script file below.

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

while opt=$(zenity --width=300 --height=320 --title="SSH TUNNEL" --text="Choose Tunnel" --list --column="List" "${options[@]}"); do
    case "$opt" in
    "${options[0]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10 -L 10000:192.168.1.10:3389 -N;;
    "${options[1]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10 -L 10001:192.168.1.11:3389 -N;;
    "${options[2]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10 -L 10002:192.168.1.12:22 -N;;
    "${options[3]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10 -L 10003:192.168.1.13:22 -N;;
    "${options[4]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10 -L 10004:192.168.1.14:22 -N;;
    "${options[5]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10 -L 10005:192.168.1.15:22 -N;;
    *) zenity --error --text="Choose again";;
    esac
    break
done

Explain the commands in the script

When run, the script will look like this.

create-file-containing-ssh-tunnel-information Create file containing ssh tunnel information
Create file containing ssh tunnel information.

Options command

The options command will declare a list of the names of the servers you want the ssh tunnel to.

It will have the following command structure:

options=(
"src-port : server-name : dstport"
)

src-port here is local port on your computer, when the tunnel connection is finished, you will connect to this port.

dst-port is the remote port on the server you want to connect to, the example here is port 3389 of the server named SERVER RDP 01.

The list is top-down and has numbers from small to large, starting from zero. Ex: 0, 1, 2, 3…

while command

The while command will create a loop, reading the list from the options command above.

The zenity command will create a window of 300×320 px size. In this window will display the list of servers.

Read more below to learn more about this zenity command.

The variable "$ {options [0]}" will represent the servers in the list of options commands. It is assigned a number corresponding to the order of the list, from 0 onwards.

ssh -i ~/.ssh/id_rsa -p 22 -f danie@192.168.100.10

The above command allows you to ssh to your gateway server, for example, 192.168.100.10 and use ssh key.

-L 10000:192.168.1.10:3389 -N

Use the above command to execute ssh tunnel. 10000 is local (or src – source) port. 3389 is the remote (or dst – destination) port.

Add a new tunnel connection

Creating a new connection is very simple, you just need to copy 1 more line in the options = () section and copy 1 more line in the section "$ {options [0]}". Replacing the information of the connection is complete.

Conclusion

This article has shown you how to create a list of ssh tunnel connections. And use zenity to show that list in the user interface.

Follow the next article in this serie.

Continue reading the series«« Previous part: Introducing ssh tunnel application in Linux MintNext part: Create script file to delete ssh tunnel »»
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.