One Weird Trick to SSH into your JVM

If you’re anything at all like me, you write perfect code.  And once you’ve written that code, it runs flawlessly and you never have any reason to look at look at it while it’s actually running.  I mean, seriously, you’ve written tests that show everything working, and besides that, when you’re as handsome as we are, who would want to even try to tear your eyes away from a mirror?

Since you’re reading this, I’m going to go out on a limb and guess that you’ve only landed on my blog in order to enjoy a quick moment of mirth at the expense of the imperfect proles who actually need my advice.  Drink it in.  You’ve earned it.

If you don’t fit into that camp, don’t worry!  You’re exactly the person that they had in mind when they designed Ruby!  Or Python!  Or any one of a dozen other languages with a REPL.  Don’t worry, I know you know what a REPL is, but I’m going to prove that I know what it is, so that you can skip straight to agreeing with me, thereby proving that you knew what I was talking about the whole time.

“REPL” is just technobabble that means “Read-eval-print loop”.  Still too much technobabble?  The REPL is the cool little tool that lets you run big-boy code from an interactive shell and it gives you the ability to get inside the computer and poke around while your program is running.  It’s so simple.  It’s IN the computer…:

I'm (not) a computer

I’m (not) a computer

The above screenshot gives a tiny example of the sorts of things you can do with your Ruby REPL.  I picked Ruby for this example, but you’d see something similar in any language that provides a REPL.  You can do simple stuff like print a string, or, even better, you can dig into your application and just mess with things!  After all, who doesn’t like to mess with things?!  That couldn’t possibly have side-effects, right?

Anytime you can use an interactive mode like the ones I mentioned above, it’s going to be easy to get close to the application that you’re developing.  These tools are extremely powerful, especially when you combine them with some sweet Dependency Injection action.

One day, you’ll see the light that is Java and you will be amazed.  With a healthy supply of semi-colons, you’ll migrate your application to Java while shedding a tear for all the lost time that you could have spent together.  Migrating from one language to another is easy, so you’ll immediately want to fire up your Java interpreter and mess with all of the things in your new Java app.

Unfortunately for you, Me-from-the-past is going to tell you “No dice.”  Java doesn’t come with an interactive mode out-of-the-box.  The closest that you could come to a REPL in Java was using the Java Management Extensions (JMX) to examine registered resources.  After you register a few Beans, you’ll hopefully be able to run something like JConsole to look at your application’s guts.  If you did everything right, you’ll probably see some objects that you can look at… and you’ll immediately begin thinking that maybe you should migrate back, if only to get your REPL back.

Fortunately for you, Me-from-right-now is here to tell you that our own brilliant Marshall Pierce has felt your pain and has leveraged the arcane magics of Groovy and SSH to provide you with a mega-hammer that he assumes you are not going to use to hit yourself in the face: a interactive prompt for your JVMs, both local and remote! A local REPL for exploring your app in dev mode is great, but a remote REPL for poking around in your live app is even better. (Where by better, I mean dangerous and awesome.)

“Wh—…H—…How did he do that?” you might ask, ever so eloquently.  Basically, you’ll embed a tiny SSH server that exposes a Groovy shell that can execute arbitrary code inside a running JVM.  Marshall has helpfully provided a small demo app that shows exactly what this looks like.  Seriously, go run the example project.  When you do and you realize how you have now become the master of time and space, you won’t have anything to say except, “That was easy.”

Hopefully, you skipped right over that part about executing arbitrary code in your JVM, because that sounds terrifying.  But, if that made you a little queasy, you can rest easy knowing that your precious JVM is safe and sound.  With only a few more characters, you can tell your app where to find a set of public keys (RSA or DSA) that it can use to authenticate SSH requests.  Now, when you run your Groovy shell, you just have to specify the location of your private key file, and you’ll automatically get the secure handshake that feels ever-so-good.

If you’re looking for more detail, you should check out the project’s public GitHub page, where you will find helpful instructions on how you can start poking around where James Gosling never intended for you to be…

Posted by Ace Ellett

Ace's time at Palomino Labs has seen him immersed in iOS and aptly transitioning between server-side technologies (including Java, Ruby on Rails, and StackMob). Before joining Palomino Labs, Ace was a developer on the Boku Accounts team that built an ecosystem for consumers to use their mobile phone to make purchases at brick and mortar merchants, leading the POS integration. Prior to Boku, Ace was a developer at CRC where he led the project to modernize their 15+ year old technology stack and migrate from VB6 to C# and .NET.

About Palomino Labs

Palomino Labs unlocks the potential of software to change people and industries. Our team of experienced software developers, designers, and product strategists can help turn any idea into reality.

See the Palomino Labs website for more information, or send us an email and let's start talking about how we can work together.