Created
November 21, 2014 22:00
-
-
Save harschware/056200934ffdc7fa8242 to your computer and use it in GitHub Desktop.
A class for connecting via public key exchange using SSHJ
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
package harschware.sandbox; | |
import java.io.IOException; | |
import java.security.Security; | |
import java.util.concurrent.TimeUnit; | |
import net.schmizz.sshj.SSHClient; | |
import net.schmizz.sshj.common.IOUtils; | |
import net.schmizz.sshj.connection.channel.direct.Session; | |
import net.schmizz.sshj.connection.channel.direct.Session.Command; | |
import net.schmizz.sshj.transport.verification.PromiscuousVerifier; | |
/** | |
* | |
* @see https://github.com/hierynomus/sshj/issues/113 | |
* @see http://stackoverflow.com/questions/27067499/scala-ssh-connecting-to-hosts-with-public-key-exchange | |
* | |
*/ | |
public class SSHJConnet { | |
public static void main(String... args) throws IOException { | |
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); | |
SSHClient client = new SSHClient(); | |
client.addHostKeyVerifier(new PromiscuousVerifier()); | |
client.connect("server"); | |
client.authPublickey("user"); | |
final Session session = client.startSession(); | |
final Command cmd = session.exec("whoami"); | |
String response = IOUtils.readFully(cmd.getInputStream()).toString(); | |
cmd.join(10, TimeUnit.SECONDS); | |
System.out.println(response); | |
session.close(); | |
client.disconnect(); | |
client.close(); | |
} // end main | |
} // end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment