码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA中使用JSch库实现SSH功能

时间:2020-03-10 13:46:24      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:int   项目   创建   off   实现   ons   lte   lan   vax   

一、简介

  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窗口中运行)效果

   技术图片

 

   技术图片

 

  技术图片

 

  技术图片

 

 

 

 

  

 

JAVA中使用JSch库实现SSH功能

标签:int   项目   创建   off   实现   ons   lte   lan   vax   

原文地址:https://www.cnblogs.com/songxingzhu/p/12454976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!