Search in sources :

Example 1 with SshConnectionFactoryRegistry

use of com.jn.agileway.ssh.client.SshConnectionFactoryRegistry in project agileway by fangjinuo.

the class SshCommandTest method testUseAgilewayJschAPI.

@Test
public void testUseAgilewayJschAPI() throws Throwable {
    SshConnectionFactoryRegistry registry = new SshConnectionFactoryRegistry();
    SshConnectionFactory connectionFactory = registry.get("jsch");
    SshConnectionConfig connectionConfig = connectionFactory.newConfig();
    String password = "fjn13570";
    String username = "fangjinuo";
    // String host = "192.168.1.79";
    String host = "192.168.234.128";
    int port = 22;
    connectionConfig.setHost(host);
    connectionConfig.setPort(port);
    connectionConfig.setPassword(password);
    connectionConfig.setUser(username);
    SshConnection connection = connectionFactory.get(connectionConfig);
    executeAndDump(connection, "route");
    executeAndDump(connection, "ifconfig");
    executeAndDump(connection, "cd ~/.java;ls -al");
    connection.close();
}
Also used : SshConnectionFactory(com.jn.agileway.ssh.client.SshConnectionFactory) SshConnection(com.jn.agileway.ssh.client.SshConnection) SshConnectionConfig(com.jn.agileway.ssh.client.SshConnectionConfig) SshConnectionFactoryRegistry(com.jn.agileway.ssh.client.SshConnectionFactoryRegistry) Test(org.junit.Test)

Example 2 with SshConnectionFactoryRegistry

use of com.jn.agileway.ssh.client.SshConnectionFactoryRegistry in project agileway by fangjinuo.

the class SftpFileProvider method doCreateFileSystem.

@Override
protected FileSystem doCreateFileSystem(FileName rootName, FileSystemOptions fileSystemOptions) throws FileSystemException {
    // Create the file system
    final GenericFileName root = (GenericFileName) rootName;
    SshConnectionFactory sshConnectionFactory = new SshConnectionFactoryRegistry().getDefault();
    SshConnectionConfig connectionConfig = sshConnectionFactory.newConfig();
    connectionConfig.setHost(root.getHostName());
    connectionConfig.setPort(root.getPort());
    connectionConfig.setUser(root.getUserName());
    connectionConfig.setPassword(root.getPassword());
    SftpFileSystemConfigBuilder configBuilder = (SftpFileSystemConfigBuilder) getConfigBuilder();
    final File knownHostsFile = configBuilder.getKnownHosts(fileSystemOptions);
    if (knownHostsFile != null) {
        connectionConfig.setKnownHostsPath(knownHostsFile.getAbsolutePath());
    }
    // JSCH 特有属性:
    Integer timout = configBuilder.getTimeout(fileSystemOptions);
    if (timout != null) {
        connectionConfig.setProperty("ConnectTimeout", timout);
    }
    String strictHostKeyChecking = configBuilder.getStrictHostKeyChecking(fileSystemOptions);
    if (Strings.isNotEmpty(strictHostKeyChecking)) {
        connectionConfig.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
    }
    final String preferredAuthentications = configBuilder.getPreferredAuthentications(fileSystemOptions);
    if (preferredAuthentications != null) {
        connectionConfig.setProperty("PreferredAuthentications", preferredAuthentications);
    }
    // set compression property
    final String compression = configBuilder.getCompression(fileSystemOptions);
    if (compression != null) {
        connectionConfig.setProperty("compression.s2c", compression);
        connectionConfig.setProperty("compression.c2s", compression);
    }
    SshConnection sshConnection = sshConnectionFactory.get(connectionConfig);
    SftpSession session = sshConnection.openSftpSession();
    return new SftpFileSystem(root, session, null, fileSystemOptions);
}
Also used : SshConnectionFactory(com.jn.agileway.ssh.client.SshConnectionFactory) SshConnection(com.jn.agileway.ssh.client.SshConnection) GenericFileName(org.apache.commons.vfs2.provider.GenericFileName) File(java.io.File) SshConnectionConfig(com.jn.agileway.ssh.client.SshConnectionConfig) SshConnectionFactoryRegistry(com.jn.agileway.ssh.client.SshConnectionFactoryRegistry) SftpSession(com.jn.agileway.ssh.client.sftp.SftpSession)

Aggregations

SshConnection (com.jn.agileway.ssh.client.SshConnection)2 SshConnectionConfig (com.jn.agileway.ssh.client.SshConnectionConfig)2 SshConnectionFactory (com.jn.agileway.ssh.client.SshConnectionFactory)2 SshConnectionFactoryRegistry (com.jn.agileway.ssh.client.SshConnectionFactoryRegistry)2 SftpSession (com.jn.agileway.ssh.client.sftp.SftpSession)1 File (java.io.File)1 GenericFileName (org.apache.commons.vfs2.provider.GenericFileName)1 Test (org.junit.Test)1