java linux ssh连接工具,java linux ssh工具类

1. maven 配置

com.jcraft

jsch

0.1.54

2.代码

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;

public class SSHUtils {

final static String user = "root";// 用户名

final static String pwd = "131420.com";// 密码

final static String ip = "47.107.168.139";// 服务器地址

final static int port = 22;// 端口号

final static int timeout = 60000000;

public static void main(String[] args) throws Exception {

//查看文件个数

String commond = "free";

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

System.out.println(ls);

}

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.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");

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;

}

}cf08c81590ec8ca780696098b3fb4719.gif

扫码关注我们

微信号:SRE实战

拒绝背锅 运筹帷幄

×

选择打赏方式:

微信

QQ钱包

支付宝

打赏

打赏

打赏

多少都是心意!谢谢大家!!!

×

选择分享方式:

微信扫一扫,分享朋友圈

Or

手机扫一扫,精彩随身带

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值