Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

java - SSHJ - Keypair login to EC2 instance

I have a pem file that looks like the one in SSHJ tests (though I don't see it being referenced): https://github.com/shikhar/sshj/blob/master/src/test/resources/hostkey.pem . Simply trying to auth in via the pem file to an EC2 instance (read as string), but having trouble. Anyone done this?

    SSHClient ssh = new SSHClient();
    ssh.connect("ec2-XXXXXXX.compute-1.amazonaws.com");
    ssh.authPublickey("ubuntu", getPemAsString("/Users/me/ec2.pem"));
    final Session session = ssh.startSession();
    session.exec("echo -e "test" >> /home/ubuntu/testfile");

Error is below:

INFO [main] (TransportImpl.java:152) - Client identity string: SSH-2.0-SSHJ_0_8
INFO [main] (TransportImpl.java:161) - Server identity string: SSH-2.0-OpenSSH_5.8p1 Debian-7ubuntu1
INFO [main] (KeyExchanger.java:195) - Sending SSH_MSG_KEXINIT
INFO [reader] (KeyExchanger.java:357) - Received SSH_MSG_KEXINIT
INFO [reader] (AbstractDHG.java:110) - Sending SSH_MSG_KEXDH_INIT
INFO [reader] (KeyExchanger.java:370) - Received kex followup data
INFO [reader] (AbstractDHG.java:120) - Received SSH_MSG_KEXDH_REPLY
ERROR [reader] (TransportImpl.java:570) - Dying because - net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify `ssh-rsa` host key with fingerprint `xx:0a:xx:b5:c2:fd:44:1d:e0:e4:fc:d8:5f:f8:dd:f6` for `ec2-XXXX.compute-1.amazonaws.com` on port 22
INFO [reader] (TransportImpl.java:302) - Setting active service to null-service
ERROR [main] (Promise.java:171) - <<kex done>> woke to: net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify `ssh-rsa` host key with fingerprint `xx:0a:xx:b5:c2:fd:44:1d:e0:e4:fc:xx:5f:f8:dd:f6` for `ec2-XXXX.compute-1.amazonaws.com` on port 22

EDIT: Still no luck. Must be doing something wrong with the private key AWS generates for login?

 SSHClient ssh = new SSHClient(); 
 ssh.connect("ec2-XXX.compute-1.amazonaws.com"); 
 ssh.addHostKeyVerifier("dd:9c:XX:fa:6a:XX:32:6a:2b:c3:e7:bd:2b:15:26:5f:76:b6:??c4:fe"); 
 ssh.authPublickey("ubuntu", getRSAPrivateKeyAsString("mypem")); // Must be wrong?

 final Session session = ssh.startSession(); 
 session.exec("echo -e "test" >> /home/ubuntu/testfile");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The example given for connecting to EC2 did not initially work for me until I added the BouncyCastleProvider to the java.security.Security class. The simple example that worked for me (written in Groovy for simplicity) is:

@Grab(group='net.schmizz', module='sshj', version='0.8.1')
@Grab(group='org.bouncycastle', module='bcprov-jdk16', version='1.46')

import net.schmizz.sshj.*
import net.schmizz.sshj.userauth.keyprovider.*
import net.schmizz.sshj.common.*
import net.schmizz.sshj.transport.verification.PromiscuousVerifier
import net.schmizz.sshj.connection.channel.direct.Session
import net.schmizz.sshj.connection.channel.direct.Session.Command

import java.security.*
import java.util.concurrent.TimeUnit

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

client = new SSHClient()
client.addHostKeyVerifier(new PromiscuousVerifier())
client.connect("ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com")

PKCS8KeyFile keyFile = new PKCS8KeyFile()
keyFile.init(new File("/dev/ec2/key/mykey.pem"))
client.authPublickey("ubuntu",keyFile) 

final Session session = client.startSession()
final Command cmd = session.exec("whoami")
String response = IOUtils.readFully(cmd.getInputStream()).toString()
cmd.join(10, TimeUnit.SECONDS)

println response   //ubuntu

session.close()
client.disconnect()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...