参考网址:就特么忘记参考的那个网址了,好长时间之前写的

pom.xml添加依赖

com.jcraft

jsch

0.1.53

org.apache.commons

commons-io

1.3.2

ssh连接测试代码:

直接在main方法中调用即可

//查看文件个数

String commond = "ls -lR /share | grep \"^-\" | wc -l";

String ls = SSHUtils.execCommandByJSch(ip, port,user, pwd,commond);

//查看文件大小String fileSize = SSHUtils.execCommandByJSch(ip, port,user, pwd, "du -sm /share |awk '{print $1}'");

ssh连接liunx工具类:

package cn.com.taiji.commons.utils;

import java.io.IOException;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import org.apache.commons.lang3.StringUtils;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

/**

* @desc

* @para

* @author

* @version 2018年6月6日 下午2:39:07

*/

public class SSHUtils {

private static final String ENCODING = "UTF-8";

private static final int timeout = 60 * 60 * 1000;

private static final int defaultPort = 22;

public static Session getJSchSession(String ip,int port,String user,String pwd,int timeout) throws JSchException {

JSch jsch = new JSch();

Session session = jsch.getSession(user, ip, port);

session.setPassword(pwd);

session.setConfig("StrictHostKeyChecking", "no");  // 第一次访问服务器时不用输入yes

session.setTimeout(timeout);

session.connect();

return session;

}

public static String execCommandByJSch(String ip,String user,String pwd,String command) throws IOException, JSchException {

return execCommandByJSch(ip, defaultPort, user, pwd, timeout, command, ENCODING);

}

public static String execCommandByJSch(String ip,int port,String user,String pwd,String command) throws IOException, JSchException {

return execCommandByJSch(ip, port, user, pwd, timeout, command, ENCODING);

}

public static String execCommandByJSch(String ip,int port,String user,String pwd,int timeout, String command, String resultEncoding) throws IOException, JSchException {

Session session = getJSchSession(ip, port, user, pwd, timeout);

String result = execCommandByJSch(session, command, resultEncoding);

session.disconnect();

return result;

}

public static String execCommandByJSch(Session session, String command) throws IOException, JSchException {

return execCommandByJSch(session, command, ENCODING);

}

public static String execCommandByJSch(Session session, String command, String resultEncoding) throws IOException, JSchException {

ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

InputStream in = channelExec.getInputStream();

channelExec.setCommand(command);

channelExec.setErrStream(System.err);

channelExec.connect();

String result = IOUtils.toString(in, resultEncoding);

channelExec.disconnect();

return result;

}

}

ssh 协议 java_java代码之SSH协议连接linux相关推荐

  1. ldap协议 java_java如何调用ldap协议【LdapContext】

    背景: 做了个系统需要用集团的用户id登录.而集团用户系统是用ldap做的. 关键知识点 1. 设置连接 ctx = new InitialLdapContext(env, connCtls); 2. ...

  2. 主机远程虚拟机linux,如何使用SSH来远程连接Linux虚拟主机?

    这个不难,首先,Linux虚拟机需要开启ssh服务,接着你就可以借助xshell等软件利用ssh来远程连接Linux虚拟机了,下面我简单介绍一下实验过程,主要内容如下: 1.开启Linux虚拟机的ss ...

  3. 通过cmder使用SSH协议远程连接Linux服务器 虚拟机

    通过cmder远程连接Linux服务器 安装cmder 在本机生成SSH秘钥对 上传公玥到linux服务器 本机连接 修改配置config文件免密登录 安装cmder •cmder下载地址:https ...

  4. 【线程池】自行准备linux环境,带你手写线程池,只需仅仅150行代码|内存池|API|连接池|应用协议丨C/C++Linux服务器开发

    [线程池]自行准备linux环境,带你手写线程池,只需仅仅150行代码 视频讲解如下,点击观看: [线程池]自行准备linux环境,带你手写线程池,只需仅仅150行代码|内存池|API|连接池|应用协 ...

  5. ubuntu 允许ssh访问_Ubuntu开启远程SSH连接

    前言 今天突然想到反正我实验室的主机一般都不会关,这样的话我不在实验室时也可以通过远程连接来控制我的机器跑代码或者做别的事情.于是我马上查找Ubuntu远程SSH连接的教程,实现了在宿舍远程登录我的实 ...

  6. MQTT客户端连接服务器协议,mqtt客户端和服务器长连接

    mqtt客户端和服务器长连接 内容精选 换一换 介绍设置客户端和服务器的安全认证方式的相关参数.参数说明:表明与服务器建立链接后,不进行任何操作的最长时间.参数类型:USERSET取值范围:整型,0- ...

  7. Loadrunner通过ssh连接linux进行hadoop基准测试

    版权声明:本文为博主原创文章,未经博主允许不得转载.欢迎访问我的博客 https://blog.csdn.net/smooth00/article/details/73796622 Loadrunne ...

  8. linux ssh抓包,如何在SSH连接Linux系统的环境下使用wireshark抓包?

    TSINGSEE青犀视频云边端架构EasyNVR.EasyDSS.EasyGBS等都是有两种操作系统的版本,一种是linux,一种是windows.而大多数开发者用户都会使用linux版本进行安装. ...

  9. wireshark linux远程,如何在SSH连接Linux的环境下使用wireshark抓包云边端架构?

    原标题:如何在SSH连接Linux的环境下使用wireshark抓包云边端架构? TSINGSEE青犀视频云边端架构EasyNVR.EasyDSS.EasyGBS等都是有两种操作系统的版本,一种是li ...

最新文章

  1. java文件用editplus乱码,EditPlus设置编码后,编译时仍然出现乱码
  2. 个人所得税计算,计算器.
  3. Linux下挂载与解除挂载U盘
  4. java中ssh如何理解
  5. TJA1050只能RX不能TX
  6. LiveVideoStackCon技术培训 限量买1赠1
  7. python爬取qq音乐周杰伦_Python爬取QQ音乐url及批量下载
  8. oracle 快速关闭_快速关闭
  9. java编程输入学生成绩 判断最高分_从键盘输入本次Java考试五位学生的成绩,求考试成绩最高分_学小易找答案...
  10. (三)python(基于面向对象)使用Requests+Xpath提取HTML内容(妹子网MM图片)
  11. 2021中国机器人操作系统(ROS)暑期学校-转载
  12. Apache之搭建静态网站
  13. 阿里大文娱深耕电影赛道,推出多部爆款电影
  14. Javascrit通过百度地图API获取客户端IP、地址
  15. 技术干货 | 基于 MindSpore 实现图像分割之平均表面距离
  16. 服务器存储系统的模式,服务器的三种存储方式
  17. C/C++更新linux系统时间和硬件时间的方法
  18. 正则 环视 oracle,环视正则 - travler的个人空间 - OSCHINA - 中文开源技术交流社区...
  19. python制作英汉词典_Python基于有道实现英汉字典功能
  20. 深圳计算机中级职称入户,深圳积分入户中级职称积分,这些证书助你轻松入户!...

热门文章

  1. spring什么版本支持java8_升级spring4.1.6和支持java8
  2. altera fpga sdi输出方案_高段位攻城狮是这样解决SI分析、DDR、FPGA国产化问题的! | 电巢直播答疑汇总...
  3. FTP服务器构建与维护,ftp服务器的搭建与三种访问途径
  4. python入职要求_Python入职面试,可能会被企业HR问到的问题,你准备好了吗
  5. python turtle画动物_如何用python画简单的动物
  6. Rendering Linear lighting and color
  7. MYSQL相关学习-常用语句
  8. Predicate函数式接口
  9. redis数据库的安装配置
  10. 字符设备驱动笔记(二)