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 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 [email protected] -L 10000:192.168.1.10:3389 -N;;
    "${options[1]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10001:192.168.1.11:3389 -N;;
    "${options[2]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10002:192.168.1.12:22 -N;;
    "${options[3]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10003:192.168.1.13:22 -N;;
    "${options[4]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -L 10004:192.168.1.14:22 -N;;
    "${options[5]}" ) ssh -i ~/.ssh/id_rsa -p 22 -f [email protected] -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.

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.

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

(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

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.