Created
May 6, 2020 22:48
-
-
Save aaronanderson/a84426dafe445ee505806aef15c5390c to your computer and use it in GitHub Desktop.
JSCH with OpenSSH keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#generate a new SSH key in OpenSSH format | |
ssh-keygen -f /tmp/ossh.priv -t rsa -b 4096 | |
cat /tmp/ossh.priv | |
-----BEGIN OPENSSH PRIVATE KEY----- | |
... | |
cat /tmp/ossh.priv.pub | |
ssh-rsa XXXXXXXXXXXXXXX | |
#use putty conversion tools to convert private key to ssh.com format BEGIN SSH2 ENCRYPTED PRIVATE KEY | |
ssh-keygen -e -f /tmp/ossh.priv > /tmp/ssh.pub | |
cat /tmp/ssh.pub | |
---- BEGIN SSH2 PUBLIC KEY ---- | |
... | |
#these are the public and private key formats that JSCH supports | |
ssh-keygen -i -f /tmp/ssh.pub > /tmp/ssh_pub | |
cat /tmp/ssh.pub | |
ssh-rsa XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
#if one needs to convert from BEGIN SSH2 ENCRYPTED PRIVATE KEY to BEGIN OPENSSH PRIVATE KEY then puttygen and it's conversion options will be needed because ssh-keygen doesn't support it. | |
chmod 600 /tmp/ossh.priv | |
ssh-keygen -p -f /tmp/ossh.priv -m pem -P XXXXX -N XXXXX | |
cat /tmp/ossh.priv | |
-----BEGIN RSA PRIVATE KEY----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String result = ""; | |
String user = "XXXX"; | |
String host = "XXXX.XXX"; | |
int port = 22; | |
String directory = "/"; | |
String sshPrivateKeyPath ="id-rsa.priv"; | |
String sshPrivateKeyPassword ="XXXXX"; | |
String sshPublicKeyPath ="id-rsa.pub"; | |
int timeout = 10000; | |
try { | |
JSch.setLogger(new JSCHLogger()); | |
JSch jsch = new JSch(); | |
URL privateKey = AssemblyUtils.locateResource(context.getBaseURL(), sshPrivateKeyPath); | |
byte[] privateKeyBytes = Files.readAllBytes(Paths.get(privateKey.toURI())); | |
URL publicKey = AssemblyUtils.locateResource(context.getBaseURL(), sshPublicKeyPath); | |
byte[] publicKeyBytes = Files.readAllBytes(Paths.get(publicKey.toURI())); | |
jsch.addIdentity(user, privateKeyBytes, publicKeyBytes, sshPrivateKeyPassword.getBytes()); | |
Session session = jsch.getSession(user, host, port); | |
Properties config = new Properties(); | |
config.put("StrictHostKeyChecking", "no"); | |
session.setConfig(config); | |
session.setTimeout(timeout); | |
try { | |
session.connect(timeout); | |
Channel channel = session.openChannel("sftp"); | |
channel.connect(timeout); | |
ChannelSftp c = (ChannelSftp) channel; | |
result = ((Vector<LsEntry>) c.ls(directory)).stream().map(e -> e.getFilename() + " Size: " + e.getAttrs().getSize() + " Modified: " + e.getAttrs().getMtimeString()).collect(Collectors.joining("\n")); | |
channel.disconnect(); | |
System.out.format("SFTP List: %d - %s\n", directory, result); | |
} finally { | |
session.disconnect(); | |
} | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I couldn't follow this. there's mismatch between java and text file, and the instructions on text file are not clear and I couldn't solve my problem following them. Can you elaborate which private/public keys could be used by JSch?