Create file containing ssh tunnel information

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

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

"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 [email protected] -L 10000: -N;;
    "${options[1]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10001: -N;;
    "${options[2]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10002: -N;;
    "${options[3]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10003: -N;;
    "${options[4]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10004: -N;;
    "${options[5]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10005: -N;;
    *) zenity --error --text="Choose again";;

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.

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:

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

Recommended Reading: Zenity create a list dialog

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 [email protected]

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

-L 10000: -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.


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.

(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: Introducing ssh tunnel application in Linux MintNext part: Create script file to delete ssh tunnel »»

You may also like

Notify of
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.



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

© 2021-2024 – All rights reserved.

Please write sources “” 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

Would love your thoughts, please comment.x

Adblock Detected

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