当前位置:网站首页>获取并监控远程服务器日志
获取并监控远程服务器日志
2022-07-03 05:18:00 【焦虑的二狗】
测试JSch
原文地址:https://www.freesion.com/article/3557457395/
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>
函数入口
import com.jcraft.jsch.JSchException;
import java.io.IOException;
public class Hellossh {
public static String host = "10.211.55.3";
public static String username = "parallels";
public static String password = "WW731298";
public static void main(String[] args) throws IOException, JSchException {
String command1 = "head -n 1 /home/admin/logs/*.log";
//执行输入信息
JSchUtil js = new JSchUtil(username, password, host, 22);
js.Connection();
js.executeCmd(command1);
}
}
UTIL类
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;
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.setTimeout(3000);
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();
}
}
边栏推荐
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- @Autowired 导致空指针报错 解决方式
- leetcode435. Non overlapping interval
- Learn libcef together -- set cookies for your browser
- Why is go language particularly popular in China
- Shallow and first code
- Redis 过期淘汰机制
- appium1.22. Appium inspector after X version needs to be installed separately
- Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
- leetcode452. Detonate the balloon with the minimum number of arrows
猜你喜欢

es7创建索引容易犯的错误

cookie session jwt

Principles of BTC cryptography

BTC-密码学原理

Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in

Introduction to deep learning (II) -- univariate linear regression

"Hands on deep learning" pytorch edition Chapter II exercise

(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)

Kept hot standby and haproxy

Basic knowledge of reflection (detailed explanation)
随机推荐
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
Go practice -- closures in golang (anonymous functions, closures)
leetcode452. Detonate the balloon with the minimum number of arrows
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
穀歌 | 蛋白序列的深度嵌入和比對
50 practical applications of R language (36) - data visualization from basic to advanced
Overview of basic knowledge of C language
Burp suite plug-in based on actual combat uses tips
Make your own dataset
Redis 入门和数据类型讲解
Go practice -- design patterns in golang's singleton
Go language interface learning notes Continued
Go practice -- use redis in golang (redis and go redis / redis)
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
appium1.22.x 版本後的 appium inspector 需單獨安裝
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
JS scope
1107 social clusters (30 points)
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计