欢迎来到我的的博客园,祝大家学有所成,早点实现自己的人生理想。

JAVA中使用JSch库实现SSH功能

一、简介

  JSch库可以实现Java连接Linux服务器并操作命令、文件等,支持常用的各种授权模式。网址:http://www.jcraft.com/jsch/

  类似C#中的SSH.Net框架。在Java中,类似的库还有:Apache Mina SSHD  http://mina.apache.org/sshd-project/

 

二、案例

  1、新建Maven项目,在pom.xml中添加下列依赖:

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>
    </dependencies>

  2、创建类ShellTest,内容如下:

package com.test;

import com.jcraft.jsch.*;

import javax.swing.*;
import java.io.FilterInputStream;
import java.io.IOException;

/**
 * @author Sindrol 2020/3/10 11:16
 */
public class ShellTest {

    public static void main(String[] args) throws JSchException {
        JSch jsch = new JSch();
        //jsch.setKnownHosts("C:\\Users\\XXX\\.ssh\\known_hosts");
        String host = JOptionPane.showInputDialog("Enter hostname", "192.168.213.134");
        int port = 22;
        String user = "root";
        Session session = jsch.getSession(user, host, 22);
        String passwd = JOptionPane.showInputDialog("Enter password", "123456");
        session.setPassword(passwd);
        session.setUserInfo(new MyUserInfo());
        session.connect(30000);
        Channel channel = session.openChannel("shell");
        //((ChannelShell)channel).setAgentForwarding(true);
        //使用Window的问题
        channel.setInputStream(new FilterInputStream(System.in) {
            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                return in.read(b, off, (len > 1024 ? 1024 : len));
            }
        });
        //channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        //去除控制台彩色输出
        ((ChannelShell) channel).setPtyType("vt102");
        //((ChannelShell) channel).setEnv("LANG", "zh_CN");
        channel.connect(3 * 1000);
    }

    public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
        @Override
        public String getPassword() {
            return null;
        }

        @Override
        public boolean promptYesNo(String message) {
            Object[] options = {"yes", "no"};
            int foo = JOptionPane.showOptionDialog(null,
                    message,
                    "Warning",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.WARNING_MESSAGE,
                    null, options, options[0]);
            return foo == 0;
        }

        @Override
        public String getPassphrase() {
            return null;
        }

        @Override
        public boolean promptPassphrase(String message) {
            return false;
        }

        @Override
        public boolean promptPassword(String message) {
            return false;
        }

        @Override
        public void showMessage(String message) {
            JOptionPane.showMessageDialog(null, message);
        }

        @Override
        public String[] promptKeyboardInteractive(String destination,
                                                  String name,
                                                  String instruction,
                                                  String[] prompt,
                                                  boolean[] echo) {
            return null;
        }
    }
}

  3、直接运行(或者打成Jar后在cmd窗口中运行)效果

   

 

   

 

  

 

  

 

 

 

 

  

 

posted @ 2020-03-10 12:30  宋兴柱  阅读(6269)  评论(2编辑  收藏  举报