【Java 接口】Maven配置JSch实现ssh远程操作【一】

接口功能

实现通过调用ssh接口下发shell命令

实现方式

1、Maven配置JSch自动下载
2、依赖JSch 封装Util工具类实现ssh通信基本功能

POM配置
  <dependencies>
    <dependency>
      <groupId>com.jcraft</groupId>
      <artifactId>jsch</artifactId>
      <version>0.1.51</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.archetype</groupId>
      <artifactId>maven-archetype</artifactId>
      <version>3.1.2</version>
    </dependency>
  </dependencies>
效果

在这里插入图片描述

函数入口
package com.testdemo;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import java.io.IOException;

public class hellossh {
  public static void main(String[] args) throws IOException, JSchException {
      //执行输入信息
      JSchUtil js = new JSchUtil("用户名","密码","IP地址",22);
      js.Connection();
      js.executeCmd("ls -ll");
  }
}
Util类
package com.testdemo;
import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class JSchUtil {
   //初始化类内变量
   private int SSH_PORT;
   private String username;
   private String password;
   private String hostip;
   private String charset = "UTF-8";
   private Session session;

   public JSchUtil(String user,String password,String hostip,int port){
       this.username = user;
       this.password = password;
       this.hostip = hostip;
       this.SSH_PORT = port;
   }
   /*
   连接到指定IP
    */
   public void Connection() throws JSchException {
       JSch js = new JSch();
       session = js.getSession(username,hostip,SSH_PORT);
       session.setPassword(password);
       session.setConfig("StrictHostKeyChecking", "no");
       session.connect();
   }
   /*
   关闭实例链接
    */
   public void disconnection(){
       if (session !=null && session.isConnected()){
           session.disconnect();
       }
   }
   /*
   执行ssh命令接口
    */
   public void executeCmd(String command) throws JSchException, IOException {
       BufferedReader reader = null;
       Channel channel = null;
       System.out.println("**********************************");
       System.out.println("InputCommand:"+"【"+command+"】");
       System.out.println("**********************************");
       channel = session.openChannel("exec");
       ((ChannelExec) channel).setCommand(command);
       channel.setInputStream(null);
       ((ChannelExec) channel).setErrStream(System.err);
       channel.connect();
       InputStream in = channel.getInputStream();
       reader = new BufferedReader(new InputStreamReader(in,
               Charset.forName(charset)));
       String buf = null;
       System.out.println("OutPutResult:"+"\n");
       StringBuffer buffer = new StringBuffer();
       while ((buf = reader.readLine()) != null) {
           System.out.println(buf);
           buffer.append(buf);
           buffer.append("\n");
       }
       System.out.println("**********************************");
       //测试结果可返回做正则匹配进行逻辑判断
       System.out.println("ReturnResult:"+"\n"+buffer.toString());
       channel.disconnect();
   }
   /*
   系统输入下发命令
    */
}
问题

Exception in thread “main” com.jcraft.jsch.JSchException: UnknownHostKey: 你登录的IP RSA key fingerprint is 一串key
at com.jcraft.jsch.Session.checkHost(Session.java:787)
at com.jcraft.jsch.Session.connect(Session.java:342)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.testdemo.JSchUtil.Connection(JSchUtil.java:32)
at com.testdemo.hellossh.main(hellossh.java:14)

解决方法

session.setConfig(“StrictHostKeyChecking”, “no”);

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值