Java Code Examples for net.schmizz.sshj.SSHClient#close()

The following examples show how to use net.schmizz.sshj.SSHClient#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetSFTPClient() throws Exception {
  path = testFolder.getRoot().getAbsolutePath();
  setupSSHD(path);
  SSHClient sshClient = createSSHClient();
  ChrootSFTPClient sftpClient = new ChrootSFTPClient(sshClient.newSFTPClient(), path, false, false, false);

  sftpClient.ls();
  // Close the SSH client so it's no longer usable and the SFTP client will get an exception
  sshClient.close();
  try {
    sftpClient.ls();
    Assert.fail("Expected a TransportException");
  } catch (TransportException se) {
    Assert.assertEquals("Socket closed", se.getMessage());
  }
  // Set a new SSH client and try again
  sftpClient.setSFTPClient(createSSHClient().newSFTPClient());
  sftpClient.ls();
}
 
Example 2
Source File: SshjHolderTests.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
public static void sshjConnectTest() throws IOException, InterruptedException {
	SSHClient ssh = new SSHClient();
	ssh.addHostKeyVerifier(new PromiscuousVerifier());
	ssh.connect("10.0.0.160");
	KeyProvider keyProvider = ssh.loadKeys(new String(PRIVATE_KEY), null, null);
	ssh.authPublickey("root", keyProvider);
	Session session = ssh.startSession();
	// TODO
	// session.allocateDefaultPTY();
	Session.Shell shell = session.startShell();
	String command = "mvn -version\n";

	OutputStream outputStream = shell.getOutputStream();
	outputStream.write(command.getBytes());
	outputStream.flush();
	outputStream.close();

	InputStream inputStream = shell.getInputStream();
	InputStream errorStream = shell.getErrorStream();

	Thread.sleep(1000);
	shell.close();
	if (nonNull(inputStream)) {
		String message = readFullyToString(inputStream);
		System.out.println(message);
	}
	if (nonNull(errorStream)) {
		String errmsg = readFullyToString(errorStream);
		System.out.println(errmsg);
	}

	session.close();
	ssh.close();

}
 
Example 3
Source File: SFTPSessionTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAllHMAC() throws Exception {
    final DefaultConfig defaultConfig = new DefaultConfig();
    defaultConfig.setMACFactories(
        new HMACSHA2256.Factory(),
        new HMACSHA2512.Factory()
    );
    for(net.schmizz.sshj.common.Factory.Named<MAC> mac : defaultConfig.getMACFactories()) {
        final DefaultConfig configuration = new DefaultConfig();
        configuration.setMACFactories(Collections.singletonList(mac));
        final SSHClient client = session.connect(new DisabledHostKeyCallback(), configuration);
        assertTrue(client.isConnected());
        client.close();
    }
}
 
Example 4
Source File: SFTPSessionTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAES256CTRCipher() throws Exception {
    final DefaultConfig configuration = new DefaultConfig();
    configuration.setCipherFactories(Collections.singletonList(new AES256CTR.Factory()));
    final SSHClient client = session.connect(new DisabledHostKeyCallback(), configuration);
    assertTrue(client.isConnected());
    client.close();
}
 
Example 5
Source File: SFTPSessionTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testECDHNistPKeyExchange() throws Exception {
    final DefaultConfig configuration = new DefaultConfig();
    configuration.setKeyExchangeFactories(Collections.singletonList(new ECDHNistP.Factory256()));
    final SSHClient client = session.connect(new DisabledHostKeyCallback(), configuration);
    assertTrue(client.isConnected());
    client.close();
}
 
Example 6
Source File: AwsYcloudHybridCloudTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void testShhAuthenticationFailure(String username, String host) throws IOException {
    SSHClient client = null;
    try {
        client = getSshClient(host);
        client.authPassword(username, MOCK_UMS_PASSWORD_INVALID);
        fail("SSH authentication passed with invalid password.");
    } catch (UserAuthException ex) {
        //Expected
    }
    if (client != null) {
        client.close();
    }
}
 
Example 7
Source File: SshJExample.java    From ExpectIt with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier(
            new HostKeyVerifier() {
                @Override
                public boolean verify(String s, int i, PublicKey publicKey) {
                    return true;
                }
            });
    ssh.connect("sdf.org");
    ssh.authPassword("new", "");
    Session session = ssh.startSession();
    session.allocateDefaultPTY();
    Shell shell = session.startShell();
    Expect expect = new ExpectBuilder()
            .withOutput(shell.getOutputStream())
            .withInputs(shell.getInputStream(), shell.getErrorStream())
            .withEchoInput(System.out)
            .withEchoOutput(System.err)
            .withInputFilters(removeColors(), removeNonPrintable())
            .withExceptionOnFailure()
            .build();
    try {
        expect.expect(contains("[RETURN]"));
        expect.sendLine();
        String ipAddress = expect.expect(regexp("Trying (.*)\\.\\.\\.")).group(1);
        System.out.println("Captured IP: " + ipAddress);
        expect.expect(contains("login:"));
        expect.sendLine("new");
        expect.expect(contains("(Y/N)"));
        expect.send("N");
        expect.expect(regexp(": $"));
        expect.send("\b");
        expect.expect(regexp("\\(y\\/n\\)"));
        expect.sendLine("y");
        expect.expect(contains("Would you like to sign the guestbook?"));
        expect.send("n");
        expect.expect(contains("[RETURN]"));
        expect.sendLine();
    } finally {
        expect.close();
        session.close();
        ssh.close();
    }
}
 
Example 8
Source File: AwsYcloudHybridCloudTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private void testShhAuthenticationSuccessfull(String username, String host) throws IOException, UserAuthException {
    SSHClient client = getSshClient(host);
    client.authPassword(username, MOCK_UMS_PASSWORD);
    client.close();
}