Java remote SSH

Hi, experts,

I need to execute Linux commands on a remote server (actually, EC2 machines) from my Java code. I found two libraries, sshj and jsch. Both are extensively used, but neither is extensively documented.

This is an example of using private/public key, but oh, so hard! http://stackoverflow.com/questions/3686710/sshj-example-of-private-public-key-authentication

Any help or explanation anywhere?

Thanks.
LVL 1
kerznerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I think this is an example of how to execute remote command from Java

http://www.jcraft.com/jsch/examples/Exec.java

using JSch:

http://www.jcraft.com/jsch/
0
kerznerAuthor Commented:
Actually, this example is shorter, https://github.com/shikhar/sshj/blob/master/src/main/java/examples/Exec.java, which pushes me to use sshj, but even so - what does each call mean? And how do I make it use public/private keys?

Thank you,
Mark
0
for_yanCommented:
shorter is not always the simpler
I just used the example in the link which I posted and executed command ps on my linux host from winodws.
I didn't do anything with public/private key
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

kerznerAuthor Commented:
Well, I have to use the keys - that's the only way to connect to EC2 machines. There are not username/passwords there. But you might be right, longer code, if it works, who cares. I might need to understand it, that's the problem :)
0
for_yanCommented:
Yes, if so, then you need to understand that, but I'm sure it is doable.
At least with user/password it works
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
there are many examples here - hopefully you can find something suitablefor your situation:
http://www.jcraft.com/jsch/examples/
0
kerznerAuthor Commented:
Perhaps yes. I will try this one, http://www.jcraft.com/jsch/examples/UserAuthPubKey.java, and report the results
0
for_yanCommented:
Sure, makes sense
0
Sharon SethCommented:
Another thought - if you don't want the authentication process to happen from code , you can always manually configure passwordless ssh b/w the two machines
0
kerznerAuthor Commented:
Thanks, that's a good idea, but in my case I start and stop EC2 machines, and I also must allow login from Windows, so the pure java solution is preferable.
0
garypfirstechCommented:
I'm not sure exactly what you're trying to do.  If you're just trying to start and stop EC2 machines from a java program running on your desktop, you should use Amazon's Java SDK.  The Javadoc is at http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/index.html.
Instructions are at http://docs.amazonwebservices.com/AWSSdkDocsJava/latest/DeveloperGuide/welcome.html?r=1075.

To start or stop an EC2 instance, use class AmazonEC2Client method startInstances.  You pass the constructor for AmazonEC2Client your access key and secret key credentials in an AWSCredentials object and the SDK handles everything.

If this is not what you're trying to do, please ignore the post.  Otherwise, if you have additional questions, please post.

Thanks,
Gary
0
garypfirstechCommented:
PS Use AmazonEC2Client.runInstances (instead of startInstances) for instances not backed by EBS.

Gary
0
kerznerAuthor Commented:
Gary,

I already start and stop instances with Typica. Now I need to set the Hadoop cluster and run my jobs there.

Thank you
0
garypfirstechCommented:
So, why not switch to the supported Amazon Java SDK for this and use AmazonElasticMapReduceClient.runJobFlow?

Gary
0
kerznerAuthor Commented:
Because I am building my own AMI and running hadoop jobs on them. I need to many Linux utilities and other open source software that installing all these on each cluster machine on startup is not feasible. Here the project, http://freeeed.org/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.