如何利用Ganymed SSH-2模拟SSH操作

所属分类: 软件编程 / java 阅读数: 67
收藏 0 赞 0 分享

官方地址:http://www.cleondris.ch/en/opensource-ssh2.php

简介:
Ganymed SSH-2 for Java is a library which implements the SSH-2 protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to connect to SSH servers from within Java programs. It supports SSH sessions (remote command execution and shell access), local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP. There are no dependencies on any JCE provider, as all crypto functionality is included.

程序:

复制代码 代码如下:

        @Test
        public void testSsh() {
                String hostname = "192.168.0.1";
                String username = "root";
                String password = "password";
                try {
                        /* Create a connection instance */
                        Connection conn = new Connection(hostname);
                        /* Now connect */
                        conn.connect();
                        System.out.println("connect ok");
                        /*
                         * Authenticate. If you get an IOException saying something like
                         * "Authentication method password not supported by the server at this stage."
                         * then please check the FAQ.
                         */
                        boolean isAuthenticated = conn.authenticateWithPassword(username,password);
                        if (isAuthenticated == false)
                                throw new IOException("Authentication failed.");

                        System.out.println("Authentication ok");
                        /* Create a session */
                        Session sess = conn.openSession();
                        sess.execCommand("uname -a");
                        System.out.println("Here is some information about the remote host:");
                        /*
                         * This basic example does not handle stderr, which is sometimes
                         * dangerous (please read the FAQ).
                         */
                        InputStream stdout = new StreamGobbler(sess.getStdout());
                        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
                        while (true) {
                                String line = br.readLine();
                                if (line == null)
                                        break;
                                System.out.println(line);
                        }
                        /* Show exit status, if available (otherwise "null") */
                        System.out.println("ExitCode: " + sess.getExitStatus());
                        /* Close this session */
                        sess.close();
                        /* Close the connection */
                        conn.close();
                } catch (IOException e) {
                        e.printStackTrace(System.err);
                        System.exit(2);
                }
        }

运行结果:
复制代码 代码如下:

connect ok
Authentication ok
Here is some information about the remote host:
Linux localhost.localdomain 2.6.22 #1 SMP Wed Aug 13 11:24:59 CST 2008 i686 i686 i386 GNU/Linux
ExitCode: 0

更多精彩内容其他人还在看

Springboot+redis+Interceptor+自定义annotation实现接口自动幂等

本篇文章给大家介绍了使用springboot和拦截器、redis来优雅的实现接口幂等,对于幂等在实际的开发过程中是十分重要的,因为一个接口可能会被无数的客户端调用,如何保证其不影响后台的业务处理,如何保证其只影响数据一次是非常重要的,感兴趣的朋友跟随小编一起看看吧
收藏 0 赞 0 分享

java实现时间控制的几种方案

这篇文章主要介绍了java实现时间控制的几种方案,本文从多个方面给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JVM中的本机内存跟踪

在本文里小编给大家整理了一篇关于JVM中的本机内存跟踪的相关知识点内容,有兴趣的朋友们参考学习下。
收藏 0 赞 0 分享

Java的long和bigint长度对比详解

在本文中小编给大家分享了关于Java的long和bigint长度比较的知识点内容,有兴趣的朋友们学习参考下。
收藏 0 赞 0 分享

SpringBoot2.0 整合 Dubbo框架实现RPC服务远程调用方法

这篇文章主要介绍了SpringBoot2.0 整合 Dubbo框架 实现RPC服务远程调用 ,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中BigDecimal类与int、Integer使用总结

这篇文章主要给大家介绍了关于Java中BigDecimal类与int、Integer使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

java图形界面编程实战代码

这篇文章主要介绍了java图形界面编程实战代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java二维数组遍历的2种代码

这篇文章主要介绍了java二维数组遍历的2种代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java怎么连接并访问activemq

这篇文章主要介绍了java怎么连接并访问activemq,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis动态Sql之if标签的用法详解

这篇文章主要介绍了MyBatis动态Sql之if标签的用法,本文给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多