DEV Community

cambiph
cambiph

Posted on

Setting up an SSH-tunnel using plink

At our company, we can only connect to the outside world using the standard ports. Outgoing traffic to port 8081 is blocked by the firewall. This is a real pain in the ass. We need to publish our Javascript libraries to a Nexus instance. This instance is running in the cloud on, you guessed it, port 8081.

We are already using Jenkins pipelines to automate our workflow. The decision was made to add a new step to the pipeline: ssh-tunneling. Our Jenkins instance is running in Windows Server 2012 R2. Using bash to create a tunnel cannot be achieved. We took a look at Powershell to set up the SSH tunnel but it seems thatthe implementation of SSH in Powershell is not mature enough. Batch scripts to the rescue!

Most of us are using Putty to handle our SSH-sessions. The people who make Putty also make plink, a command-line interface to the Putty back-end. In combination with batch files we can create an SSH-tunnel like this:

plink -v -x -a -T -C -noagent -ssh -pw “password” -L 8081:our-nexus-instance.com:8081 username@our-nexus-instance.com

Let me explain the different flags used in the command above:

-v : show verbose messages
-x: enable X11 forwarding
-a: enable agent forwarding
-T: disable pty allocation
-C: enable compression
-noagent: disable use of Pageant (only needed when using SSH-keys)
-ssh: use the SSH-protocol
-pw: provide the password
-L: forward local port to remote address

Using the -L flag we can map a local port to a remote port. This is done using the following syntax: ::. This is followed by the hostname we want to connect to, prefixed with the username, used to connect: username@remoteHost.

By inserting this command into our Jenkinsfile, we can open a tunnel to our remote Nexus instance:

stage(‘Publish’) {
 steps {
  bat “plink -v -x -a -T -C -noagent -ssh -pw ‘password -L 8081:our-  
       nexus-instance:8081 username@our-nexus-instance.com”
   }
 }

Discussion (0)