springboot 访问远程服务器文件,springboot使用JSch远程读取sshd服务器上的文件
JSch 是SSH2的一个纯Java实现。它容许你链接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你能够将它的功能集成到你本身的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器。实现一个java工具类。html
引用:java
com.jcraft
jsch
web
java工具类:shell
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);
}
}
springboot 访问远程服务器文件,springboot使用JSch远程读取sshd服务器上的文件相关推荐
- vue上传文件到php,vue+axios+php如何实现上传文件功能?,formdata上传文件附加参数...
vue+axios+php如何实现上传文件功能?Vue Axios PHP如何实现上传文件的功能?, 推荐:<PHP视频教程> 当我们提交表单时,我们经常会遇到一些表单提交要求.vue的a ...
- 学习日志day41(2021-09-03)(1、文件的上传 2、文件的查看 3、文件的下载 4、使用工具类上传文件 5、基于servlet3.0以上的文件上传 )
学习内容:学习JavaWeb(Day41) 1.文件的上传 2.文件的查看 3.文件的下载 4.使用工具类上传文件 5.基于servlet3.0以上的文件上传 1.文件的上传 (1)实现文件的上传需要 ...
- 在不root手机的情况上读取Data目录上的文件
在不root手机的情况下读取Data目录下的文件 使用adb命令时的错误 如果直接使用adb命令会产生以下错误: 127|shell@android:/ $ cd /data cd /data she ...
- c#如何通过ftp上传文件_定时上传文件到ftp,如何使用工具定时上传文件到ftp
IIS7服务器管理工具-FTP客户端 作为FTP客户端,它支持批量管理FTP站点 具体功能: 1.自动重连:2.自动重传:3.定时任务(定时上传.定时下载):4.自定义传输模式.线程.编码:5.删除到 ...
- java ee 上传文件_17.《JavaEE 学习笔记》Servlet 上传文件
功能介绍: 利用 Servlet 和表单提交,选择本地文件上传到服务器.上传的文件可以是文本文件或图像文件或任何文档,不能是文件夹. 需要引入的 jar 文件:commons-fileupload-1 ...
- linux文件列表的理解,如何深刻理解 Linux 上的文件列表和排序?
在 Linux 系统上,有许多方法可以列出文件并显示它们的信息.这篇文章回顾了一些提供文件细节的命令,并提供了自定义文件列表的选项,以满足你的需求. 大多数命令都会列出单个目录中的文件,而其他命令则可 ...
- 使用 Apache sshd sftp 上传文件
添加依赖 <dependency><groupId>org.apache.sshd</groupId><artifactId>sshd-core< ...
- python 文件追加写入_Python写入文件–解释了打开,读取,追加和其他文件处理功能
python 文件追加写入 欢迎 (Welcome) Hi! If you want to learn how to work with files in Python, then this arti ...
- 安卓读取mysql数据库文件路径_Android开发实现读取assets目录下db文件的方法示例...
本文实例讲述了Android开发实现读取assets目录下db文件的方法.分享给大家供大家参考,具体如下: 最近准备打算写一个关于天气预报的app,偶然的机会在一大神的博客上看到了一个获取天气的api ...
- python读写文件绝对路径_[Spark][Python]对HDFS 上的文件,采用绝对路径,来读取获得 RDD...
对HDFS 上的文件,采用绝对路径,来读取获得 RDD: In [102]: mydata=sc.textFile("file:/home/training/test.txt") ...
最新文章
- 2021年大数据Flink(二十八):Flink 容错机制 自动重启策略和恢复
- Microsoft宣称Visual Studio Installer将退役
- MicroPython-TPYBoard开发板DIY小型家庭气象站
- Mybatis 实现SQL拦截并在控制台打印SQL和参数
- bash问题引起的centos系统不能启动
- matlab为自定义后缀文件设置图标_【V3.0更新】| 这可能是全网最好用的文件管理神器了......
- 终章 - 软件工程实践总结作业
- flow使用_使用Microsoft Flow进行文本分析
- Linq 三表 left join 的实现
- tts语音合成小玩具
- 基于任务分析的非递归遍历二叉树
- ios dat 文件读写_玩转你的iphone, IOS 13 NFC标签读写详细步骤
- 直播APP源码网络基础部分处理:
- Mega网盘来下载外国友人分享的资源
- IDEA 创建文件类型与预期文件类型不符时,如何更改
- 2014年个人工作愿景
- 【Python基础】初识-与君初相识,犹如故人归
- 实训一#1.7F1方程式冠军
- 使用jupyter环境在数据集处理中遇到.ipynb_checkpoints no such file or directory的问题
- 邮箱客户端 gmail支持_如何在新的Gmail中启用离线支持
热门文章
- Shell子程序结构,函数
- 《项目百态》读感系列”苏式风格“
- 荣之联:生物云仅仅是开始
- 03_后台管理页面商品列表查询
- Luogu5607 [Ynoi2013] 无力回天 NOI2017
- BZOJ1096[ZJOI2007] 仓库建设
- element ui的时间选择器
- python实现k-shell复杂网络_企业网络结构复杂,如何高效、简单实现异地组网?...
- linux重定向文件容加时间,linux – Shell重定向和文件I / O持续时间
- windows mysql memcached,Windows上的Memcached(不是memcache)PHP扩展