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

所属分类: 软件编程 / java 阅读数: 40
收藏 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

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

JAVA多线程和并发基础面试问答(翻译)

多线程和并发问题是Java技术面试中面试官比较喜欢问的问题之一。在这里,从面试的角度列出了大部分重要的问题,但是你仍然应该牢固的掌握Java多线程基础知识来对应日后碰到的问题
收藏 0 赞 0 分享

Java List双击事件实现方法

这篇文章主要介绍了Java List双击事件实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Java开发者结合Node.js编程入门教程

这篇文章主要介绍了Java开发者结合Node.js编程入门教程,我将先向您展示如何使用Java EE创建一个简单的Rest服务来读取 MongoDB数据库。然后我会用node.js来实现相同的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Java数组操作的10大方法

下面是精心整理的Java数组操作的10大方法,大部分代码都来自Stack Overflow,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中的StringBuilder性能测试

这篇文章主要介绍了Java中的StringBuilder性能测试,本文包含测试代码和测试结果,最后得出结论,需要的朋友可以参考下
收藏 0 赞 0 分享

Java基于高精度整型实现fibonacci数列的方法

这篇文章主要介绍了Java基于高精度整型实现fibonacci数列的方法,是比较典型的算法,需要的朋友可以参考下
收藏 0 赞 0 分享

Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享

这篇文章主要介绍了Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享,需要的朋友可以参考下
收藏 0 赞 0 分享

Java实现的连续奇数(n+2*x)是合数的算法题暴力算法

这篇文章主要介绍了Java实现的连续奇数(n+2*x)是合数的算法题暴力算法,本文包含运算结果和实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

java异常机制分析

这篇文章主要介绍了java异常机制,包括异常机制的捕获、抛出及常见的异常机制总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Java使用JDBC连接数据库的实现方法

这篇文章主要介绍了Java使用JDBC连接数据库的实现方法,包括了详细的加载步骤以及完整实现示例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多