Search in sources :

Example 6 with SCPException

use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.

the class SCPClientImpl method connect.

public void connect(String user, String host, int port, File pemFile, String passphrase) throws SCPException {
    if (connection != null) {
        throw new SCPException("It is already connected!");
    }
    if (!pemFile.exists()) {
        throw new SCPException("The pemFile not exists! path=" + pemFile);
    }
    FileInputStream fis = null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String pem = null;
    try {
        fis = new FileInputStream(pemFile);
        int length = 0;
        byte[] buf = new byte[1024];
        while ((length = fis.read(buf)) != -1) {
            baos.write(buf, 0, length);
        }
        pem = new String(baos.toByteArray());
    } catch (IOException e) {
        throw new SCPException("It failed to read pemFile! path=" + pemFile, e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
    connection = new Connection(host, port);
    try {
        if (isTcpNoDelay != null) {
            connection.setTCPNoDelay(isTcpNoDelay.booleanValue());
        }
        if (serverHostKeyAlgorithms != null) {
            connection.setServerHostKeyAlgorithms(serverHostKeyAlgorithms);
        }
        connection.connect(null, connectionTimeout, keyExchangeTimeout);
        if (!connection.authenticateWithPublicKey(user, pem.toCharArray(), passphrase)) {
            throw new SCPException("It failed to authenticate!");
        }
        scpClient = connection.createSCPClient();
    } catch (IOException e) {
        scpClient = null;
        connection.close();
        connection = null;
        throw new SCPException("It failed to authenticate!", e);
    }
}
Also used : SCPException(jp.ossc.nimbus.service.scp.SCPException) Connection(ch.ethz.ssh2.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

IOException (java.io.IOException)6 SCPException (jp.ossc.nimbus.service.scp.SCPException)6 File (java.io.File)4 RecurciveSearchFile (jp.ossc.nimbus.io.RecurciveSearchFile)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 Connection (ch.ethz.ssh2.Connection)2 Session (ch.ethz.ssh2.Session)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1