springboot 访问远程服务器文件,springboot使用JSch远程读取sshd服务器上的文件

JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器。实现一个java工具类。

引用:

com.jcraft

jsch

java工具类:

package com.citipub_zxsy.count.utils;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

/**

* @decription 执行远程shell命令,并获取结果--实现分析日志

* @author zhangwenchao

* @date 2018/6/8

*

*/

public class ShellUtils {

/**配置连接

* @param user

* @param passwd

* @param host

* @param post

* @throws Exception

*/

public static Session connect(String user, String passwd, String host,int post) throws Exception {

JSch jsch = new JSch();

Session session = jsch.getSession(user, host, post);

if (session == null) {

throw new Exception("session is null");

}

session.setPassword(passwd);

java.util.Properties config = new java.util.Properties();

//第一次登陆

config.put("StrictHostKeyChecking", "no");

session.setConfig(config);

try {

session.connect(30000);

} catch (Exception e) {

throw new Exception("连接远程端口无效或用户名密码错误");

}

return session;

}

/**

* @description 执行shell命令

* @param command shell 命令

* @param user 用户名

* @param passwd 密码

* @param host ip地址

* @param post 端口号

* @throws Exception

*/

public static void execCmd(String command, String user, String passwd, String host, int port) throws Exception {

System.out.println(command);

Session session= connect(user, passwd,host,port);

BufferedReader reader = null;

Channel channel = null;

try {

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));

String buf = null;

//返回数据

while ((buf = reader.readLine()) != null) {

System.out.println(buf);

}

} catch (IOException e) {

e.printStackTrace();

} catch (JSchException e) {

e.printStackTrace();

} finally {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

channel.disconnect();

session.disconnect();

}

}

public static List execCmdAndOutput(String command, String user, String passwd, String host, int port) throws Exception {

Session session= connect(user, passwd,host,port);

BufferedReader reader = null;

Channel channel = null;

try {

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));

List output = new ArrayList();

String buf = null;

//返回数据

while ((buf = reader.readLine()) != null) {

output.add(buf);

}

return output;

} catch (IOException e) {

e.printStackTrace();

} catch (JSchException e) {

e.printStackTrace();

} finally {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

channel.disconnect();

session.disconnect();

}

return null;

}

/**

*

* @param logPath

* @param fileList

* @param user

* @param passwd

* @param host

* @param port

* @return

* @throws Exception

*/

public static Set execCmdAndgetOlderUserIDs(Set userIDSet, String logPath, List fileNames, String user, String passwd, String host, int port) throws Exception {

Session session= connect(user, passwd,host,port);

try {

for (String fileName : fileNames) {

String command = "cat "+fileName+"|grep '/login'";

Channel channel = null;

BufferedReader reader = null;

try {

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,"UTF-8"));

String buf = null;

//返回数据

while ((buf = reader.readLine()) != null) {

analyseLogOneLine(userIDSet, buf);

}

} finally {

reader.close();

channel.disconnect();

}

}

} finally {

session.disconnect();

}

return userIDSet;

}

/**

* 解析一行日志,获取老用户uid存放到Set中并返回

* @param map

* @param str

* @return

*/

private static Set analyseLogOneLine(Set userIDSet, String str) {

String[] logLine = str.split("\\s+\\|\\s+");

String userToken = null;

String cacheResult=null;

if(logLine!=null && logLine.length==12){

userToken= logLine[6].trim();

cacheResult = logLine[10].trim();

}

if(StringUtils.isNotEmpty(cacheResult)){

JSONObject jsonObject = JSON.parseObject(cacheResult);

JSONObject body = (JSONObject)jsonObject.get("body");

if(body!=null){

String userID = (String)body.get("userID");

if(StringUtils.isNotEmpty(userID)&&StringUtils.isNotEmpty(userToken)&&userToken.contains("anony")){

userToken = userToken.substring(0, userToken.length()-6);

if(!userToken.equals(userID)){

userIDSet.add(userID);

}

}

}

}

return userIDSet;

}

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

execCmd("cd /web/logs/sc_zxsy; cat api-2018-06-07.0.log","yunpub","XXXXXXX","xxx.xxx.xx.xxx",22);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值