mina-sshd/mina-sftp实现远程执行ssh命令或文件上传

mina-sshd/mina-sftp实现远程执行ssh命令或文件上传

场景&需求

  • 系统需要时不时向一台远程主机发送ssh命令,并且需要上传文件
  • 希望可以像使用数据库一样使用ssh(配置化)
  • 不希望发送一次命令创建一个连接,能实现ssh连接池
  • 不使用ganymed-ssh2或jsch,因为这两个工具年代久远,活跃度较低,出现问题解决困难
  • apache mina-ssh活跃度很高,但是工具较新,文档不是很完善

选型

最后选择了apache mina-ssh,一是社区活跃度较高,另外依托于apache mina,性能可以保障,并且mina生态下还有sftp/http/git/scp等工具,让系统有更多的可能性。

代码

clyoudu/ssh

使用方法

ssh

public class SshTest {

    public static void main(String[] args) throws Exception {
        // ssh client
        SshClient client = ClientBuilder.builder().build();
        CoreModuleProperties.IDLE_TIMEOUT.set(client, Duration.ZERO);
        client.start();

        // ssh config
        SshConfig sshConfig = new SshConfig();
        sshConfig.setIp("127.0.0.1");
        sshConfig.setAuthType("PASSWORD");
        sshConfig.setPort(22);
        sshConfig.setUsername("root");
        sshConfig.setPassword("root");

        // ssh connection factory
        SshConnectionFactory sshConnectionFactory = new SshConnectionFactory(sshConfig, client);

        // ssh connection pool
        GenericObjectPoolConfig<ClientSession> config = new GenericObjectPoolConfig<>();
        config.setMaxIdle(sshConfig.getMaxIdle());
        config.setMaxTotal(sshConfig.getMaxTotal());
        config.setMinIdle(sshConfig.getMinIdle());
        config.setTestWhileIdle(sshConfig.getTestWhileIdle());
        config.setTestOnCreate(sshConfig.getTestOnCreate());
        config.setTestOnBorrow(sshConfig.getTestOnBorrow());
        config.setTestOnReturn(sshConfig.getTestOnReturn());
        config.setMinEvictableIdleTimeMillis(sshConfig.getMinEvictableIdleTimeMillis());
        config.setTimeBetweenEvictionRunsMillis(sshConfig.getTimeBetweenEvictionRunsMillis());
        config.setBlockWhenExhausted(sshConfig.getBlockWhenExhausted());
        config.setMaxWaitMillis(sshConfig.getMaxWaitMillis());
        config.setJmxEnabled(false);

        AbandonedConfig abandonedConfig = new AbandonedConfig();
        abandonedConfig.setLogAbandoned(true);

        SshConnectionPool sshConnectionPool = new SshConnectionPool(sshConnectionFactory, config, abandonedConfig);

        SshExecutor sshExecutor = new SshExecutor(sshConnectionPool, sshConfig);

        Scanner scanner = new Scanner(System.in);
        String line;
        while (true) {
            line = scanner.nextLine();
            if (StringUtils.isNotBlank(line) && !line.equalsIgnoreCase("exit")) {
                System.out.println(sshExecutor.exec(line));
            } else {
                sshConnectionPool.close();
                client.close();
                break;
            }
        }
    }

}

sftp

public class SftpTest {

    public static void main(String[] args) throws Exception {
        // ssh client
        SshClient client = ClientBuilder.builder().build();
        CoreModuleProperties.IDLE_TIMEOUT.set(client, Duration.ZERO);
        client.start();

        // ssh config
        SshConfig sshConfig = new SshConfig();
        sshConfig.setIp("127.0.0.1");
        sshConfig.setAuthType("PASSWORD");
        sshConfig.setPort(22);
        sshConfig.setUsername("root");
        sshConfig.setPassword("root");

        // ssh connection factory
        SshConnectionFactory sshConnectionFactory = new SshConnectionFactory(sshConfig, client);

        // ssh connection pool
        GenericObjectPoolConfig<ClientSession> config = new GenericObjectPoolConfig<>();
        config.setMaxIdle(sshConfig.getMaxIdle());
        config.setMaxTotal(sshConfig.getMaxTotal());
        config.setMinIdle(sshConfig.getMinIdle());
        config.setTestWhileIdle(sshConfig.getTestWhileIdle());
        config.setTestOnCreate(sshConfig.getTestOnCreate());
        config.setTestOnBorrow(sshConfig.getTestOnBorrow());
        config.setTestOnReturn(sshConfig.getTestOnReturn());
        config.setMinEvictableIdleTimeMillis(sshConfig.getMinEvictableIdleTimeMillis());
        config.setTimeBetweenEvictionRunsMillis(sshConfig.getTimeBetweenEvictionRunsMillis());
        config.setBlockWhenExhausted(sshConfig.getBlockWhenExhausted());
        config.setMaxWaitMillis(sshConfig.getMaxWaitMillis());
        config.setJmxEnabled(false);

        AbandonedConfig abandonedConfig = new AbandonedConfig();
        abandonedConfig.setLogAbandoned(true);

        SshConnectionPool sshConnectionPool = new SshConnectionPool(sshConnectionFactory, config, abandonedConfig);

        SshExecutor sshExecutor = new SshExecutor(sshConnectionPool, sshConfig);

        Scanner scanner = new Scanner(System.in);
        String line;
        while (true) {
            line = scanner.nextLine();
            if (StringUtils.isNotBlank(line) && !line.equalsIgnoreCase("exit")) {
                File file = new File(line);
                if (file.exists() && file.isFile()) {
                    sshExecutor.upload("/tmp", file.getName(), new FileInputStream(file));
                } else {
                    System.err.println("file not exists: " + line);
                }
            } else {
                sshConnectionPool.close();
                client.close();
                break;
            }
        }
    }

}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值