服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - 如何利用Ganymed SSH-2模拟SSH操作

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

2019-10-11 11:41脚本之家 JAVA教程

这几天看SFTP资料时,无意中看到了Ganymed SSH-2,写了个简单demo,通过,感觉挺好用的,下面就和大家分享下。需要的朋友可以过来参考参考

官方地址: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掩码的几种使用例举

    今天小编就为大家分享一篇关于Java掩码的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    Alan_阿兰1962019-06-23
  • JAVA教程Scala 操作Redis使用连接池工具类RedisUtil

    Scala 操作Redis使用连接池工具类RedisUtil

    这篇文章主要介绍了Scala 操作Redis使用连接池工具类RedisUtil,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    Gavin-Feng6022019-06-29
  • JAVA教程java中ThreadPoolExecutor常识汇总

    java中ThreadPoolExecutor常识汇总

    这篇文章主要介绍了java中ThreadPoolExecutor常识汇总,线程池技术在并发时经常会使用到,java中的线程池的使用是通过调用ThreadPoolExecutor来实现的,需要的朋友...

    有爱jj2092019-06-25
  • JAVA教程Spring Cloud Alibaba Nacos 入门详解

    Spring Cloud Alibaba Nacos 入门详解

    这篇文章主要介绍了Spring Cloud Alibaba Nacos入门详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    幻楚3782019-06-22
  • JAVA教程Java中new关键字和newInstance方法的区别分享

    Java中new关键字和newInstance方法的区别分享

    在初始化一个类,生成一个实例的时候,newInstance()方法和new关键字除了一个是方法一个是关键字外,最主要的区别是创建对象的方式不同...

    JAVA之家3292019-06-18
  • JAVA教程Java跳出多重嵌套循环代码实例

    Java跳出多重嵌套循环代码实例

    这篇文章主要介绍了Java跳出多重嵌套循环,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    程序猿||攻城狮1582019-06-19
  • JAVA教程Java容器类源码详解 Deque与ArrayDeque

    Java容器类源码详解 Deque与ArrayDeque

    这篇文章主要介绍了Java容器类源码详解 Deque与ArrayDeque,Deque 接口继承自 Queue接口,但 Deque 支持同时从两端添加或移除元素,因此又被成为双端队列。,需要...

    Givefine4802019-06-26
  • JAVA教程Spring cloud Feign 深度学习与应用详解

    Spring cloud Feign 深度学习与应用详解

    这篇文章主要介绍了Spring cloud Feign 深度学习与应用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    无涯Ⅱ7052019-07-08