You heave a sigh of relief, as the QA has approved a long-awaited feature for deployment on Prod. However, as a part of the process, it is first deployed on the UAT env, where there are test accounts that can be used to certify the feature works outside of the local developer and QA testing systems.
But... running the test suite results in a lot of failures!
Reply with the always relevant "But it works on my machine!"
What do you do? You're sure that the code works. Defintely sure. Maybe the problem is with the UAT environment then? But what could be the problem with the environment? Maybe the test accounts which are newly created, are configured incorrectly? Probably yes, you think. You have access to the logs for the environment, but the scenario which it is failing for, has limited to no logs to identify the problem. You internally curse your ancestral developers.
Another option? Remote debugging! While this is a good idea, it is seldom practical for environments where the apps are under constant use. If your code is in the "hot path", good luck figuring out what requests are yours. Also it may slow down the app significantly.
Essentially, what you want is to debug the application, as if it was deployed on your local machine, but the database of the UAT server. But since UAT server is not directly accessible to your local application, you're out of luck.
Or are you? Fret not, because an SSH Tunnel is here to your rescue.
SSH Tunnel
What is an SSH Tunnel? And how do I use it?
Short answer:
SSH Tunneling will allow your application to behave as if it is deployed on a remote system, through which the UAT database is accessible.
Long answer:
The Linux ssh command provides a functionality to "port forward". Admittedly, the term "port forward" is pretty non-descriptive. Hence, I'll let StackOverflow provide to you a detailed, and a far easier explanation of SSH Tunneling than what I can here. You can read it here: https://unix.stackexchange.com/a/115906. I recommend you to read the answer because it has diagrams that are far easy to understand than the text based answers.
However, I'll still copy the relevant sections here.
ssh -L 123:farawayhost:456 remotehostlocal: -L Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.
ssh -L sourcePort:forwardToHost:onPort connectToHost means: connect with ssh to connectToHost, and forward all connection attempts to the local sourcePort to port onPort on the machine called forwardToHost, which can be reached from the connectToHost machine.
Example
For our use case, our sample command will be:
ssh -L <local port>:<uat database ip>:<uat database port> <jumphost ip> </jumphost></uat></uat></local>
Note, here the JumpHost is a system which can connect to
Once we have this figured out, everything else is a cakewalk!
You can simply run the command, and you'll be able to access the UAT Database on your localhost:
If you need to connect to multiple databases, you will need to run this command multiple times. Or you can write a bash script which can dynamically read a config file to open multiple SSH Tunnels.
I, being a Java programmer, would rather deal with statically compiled Java code, than worry about a dynamically typed language like bash. So, I utilized the jsch library to whip up an extensible project, which creates and maintains multiple SSH Tunnels. You can check it out here: https://github.com/darshitpp/java-ssh-tunnel
Structure
java-ssh-tunnel └── src └── main ├── java │ └── dev │ └── darshit │ └── java_ssh_tunnel │ ├── Main.java │ ├── MultiTunneler.java │ ├── Tunneler.java │ └── ssh │ ├── TunnelDetails.java │ └── UserDetails.java └── resources
Usage
- Download the project
- Load up in your IDE
- Run mvn clean install
- Change Main.java with required details like SSH username, SSH password, JumpHost sshHost
- (Optional) Maybe load up localPort, remoteHost, and remotePort details from a file
- Run Main.java
If all things go well, you'll see the following output on your stdout
Starting tunneling... <remotehost>:<remoteport> is available on localhost:<localport> Press Enter to terminate the tunnels... </localport></remoteport></remotehost>
Caveats
While the above is very convenient, DO NOT USE IT TO CONNECT TO PROD. Yes, that had to be written in bold with emphasis. Shall I print the message to stderr too? Comment below.
The above is the detailed content of SSH Tunnelling with Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools