Search in sources :

Example 81 with Connection

use of org.geotoolkit.sml.xml.v100.Connection in project eladmin by lWoHvYe.

the class ScpClientUtil method putFile.

public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) {
    Connection conn = new Connection(ip, port);
    File file = new File(localFile);
    if (file.isDirectory()) {
        throw new RuntimeException(localFile + "  is not a file");
    }
    String fileName = file.getName();
    try {
        conn.connect();
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (!isAuthenticated) {
            System.err.println("authentication failed");
        }
        var client = conn.createSCPClient();
        if ((mode == null) || (mode.length() == 0))
            mode = "0600";
        var scpOutputStream = client.put(StrUtil.isNotEmpty(remoteFileName) ? remoteFileName : fileName, file.length(), remoteTargetDirectory, mode);
        String content = IOUtils.toString(new FileInputStream(file), String.valueOf(StandardCharsets.UTF_8));
        scpOutputStream.write(content.getBytes());
        scpOutputStream.flush();
        scpOutputStream.close();
    } catch (IOException ex) {
        Logger.getLogger(ScpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        conn.close();
    }
}
Also used : Connection(ch.ethz.ssh2.Connection) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 82 with Connection

use of org.geotoolkit.sml.xml.v100.Connection in project eladmin by lWoHvYe.

the class ScpClientUtil method getFile.

public void getFile(String remoteFile, String localTargetDirectory) {
    Connection conn = new Connection(ip, port);
    try {
        conn.connect();
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (!isAuthenticated) {
            System.err.println("authentication failed");
        }
        var client = conn.createSCPClient();
        var scpInputStream = client.get(remoteFile);
        FileUtil.writeFromStream(scpInputStream, localTargetDirectory);
    } catch (IOException ex) {
        Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        conn.close();
    }
}
Also used : SCPClient(ch.ethz.ssh2.SCPClient) Connection(ch.ethz.ssh2.Connection) IOException(java.io.IOException)

Example 83 with Connection

use of org.geotoolkit.sml.xml.v100.Connection in project eladmin by lWoHvYe.

the class RemoteShellExecutor method login.

/**
 * 登录
 * @return
 * @throws IOException
 */
private boolean login() throws IOException {
    conn = new Connection(ip);
    conn.connect();
    return conn.authenticateWithPassword(osUsername, password);
}
Also used : Connection(ch.ethz.ssh2.Connection)

Example 84 with Connection

use of org.geotoolkit.sml.xml.v100.Connection in project cosmic by MissionCriticalCloud.

the class SshHelper method scpTo.

public static void scpTo(final String host, final int port, final String user, final File pemKeyFile, final String password, final String remoteTargetDirectory, final byte[] data, final String remoteFileName, final String fileMode, final int connectTimeoutInMs, final int kexTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                final String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                final String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        scpClient = conn.createSCPClient();
        if (fileMode != null) {
            scpClient.put(data, remoteFileName, remoteTargetDirectory, fileMode);
        } else {
            scpClient.put(data, remoteFileName, remoteTargetDirectory);
        }
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 85 with Connection

use of org.geotoolkit.sml.xml.v100.Connection in project cosmic by MissionCriticalCloud.

the class SshHelper method scpTo.

public static void scpTo(final String host, final int port, final String user, final File pemKeyFile, final String password, final String remoteTargetDirectory, final String localFile, final String fileMode, final int connectTimeoutInMs, final int kexTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                final String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                final String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        scpClient = conn.createSCPClient();
        if (fileMode != null) {
            scpClient.put(localFile, remoteTargetDirectory, fileMode);
        } else {
            scpClient.put(localFile, remoteTargetDirectory);
        }
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)68 Connection (org.ovirt.engine.sdk4.Connection)64 Connection (com.trilead.ssh2.Connection)61 Connection (org.osate.aadl2.Connection)57 Session (com.trilead.ssh2.Session)33 Connection (org.jboss.remoting3.Connection)33 Connection (com.google.cloud.bigquery.connection.v1.Connection)31 Test (org.junit.Test)30 VmsService (org.ovirt.engine.sdk4.services.VmsService)30 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)29 Connection (okhttp3.Connection)28 Connection (ch.ethz.ssh2.Connection)24 Request (okhttp3.Request)20 Subcomponent (org.osate.aadl2.Subcomponent)19 FeatureGroupConnection (org.osate.aadl2.FeatureGroupConnection)18 PortConnection (org.osate.aadl2.PortConnection)18 VmService (org.ovirt.engine.sdk4.services.VmService)18 ArrayList (java.util.ArrayList)17 Response (okhttp3.Response)17