Solved

Java remote SSH

Posted on 2012-04-01
15
403 Views
Last Modified: 2012-04-02
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.
0
Comment
Question by:kerzner
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37794350
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
 
LVL 1

Author Comment

by:kerzner
ID: 37794369
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37794373
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 1

Author Comment

by:kerzner
ID: 37794376
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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37794386
Yes, if so, then you need to understand that, but I'm sure it is doable.
At least with user/password it works
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37794388
there are many examples here - hopefully you can find something suitablefor your situation:
http://www.jcraft.com/jsch/examples/
0
 
LVL 1

Author Comment

by:kerzner
ID: 37794391
Perhaps yes. I will try this one, http://www.jcraft.com/jsch/examples/UserAuthPubKey.java, and report the results
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37794398
Sure, makes sense
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 37794893
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
 
LVL 1

Author Comment

by:kerzner
ID: 37795959
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
 
LVL 7

Expert Comment

by:garypfirstech
ID: 37797732
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
 
LVL 7

Expert Comment

by:garypfirstech
ID: 37797780
PS Use AmazonEC2Client.runInstances (instead of startInstances) for instances not backed by EBS.

Gary
0
 
LVL 1

Author Comment

by:kerzner
ID: 37797833
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
 
LVL 7

Expert Comment

by:garypfirstech
ID: 37797910
So, why not switch to the supported Amazon Java SDK for this and use AmazonElasticMapReduceClient.runJobFlow?

Gary
0
 
LVL 1

Author Comment

by:kerzner
ID: 37797925
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 39
HashTable highest marks enumeration alternative 9 38
SHA2 certs for IIS AND Java? 2 92
Tomcat: Unable to run tomcat service. 2 20
Hello to you all, I hear of many people congratulate AWS (Amazon Web Services) on how easy it is to spin up and create new EC2 (Elastic Compute Cloud) instances, but then fail and struggle to connect to them using simple tools such as SSH (Secure…
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question