当前位置:网站首页>Get and monitor remote server logs
Get and monitor remote server logs
2022-07-03 05:18:00 【Anxious two dogs】
test JSch
Original address :https://www.freesion.com/article/3557457395/
POM To configure
<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>
Function entrance
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";
// Execute input information
JSchUtil js = new JSchUtil(username, password, host, 22);
js.Connection();
js.executeCmd(command1);
}
}
UTIL class
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 {
// Initialize variables in class
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;
}
/*
Connect to the specified 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();
}
/*
Close instance link
*/
public void disconnection() {
if (session != null && session.isConnected()) {
session.disconnect();
}
}
/*
perform ssh The command interface
*/
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("**********************************");
// The test results can be returned for regular matching and logical judgment
System.out.println("ReturnResult:" + "\n" + buffer.toString());
channel.disconnect();
}
}
java Remote monitoring server memory 、 disk 、 Swap space 、cpu It's about the utilization of
边栏推荐
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
- Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
- (完美解决)matplotlib图例(legend)如何自由设置其位置
- BTC-密码学原理
- 2022-02-12 daily clock in: problem fine brush
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- Classification and discussion of plane grab detection methods based on learning
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- Why is go language particularly popular in China
- Self introduction and objectives
猜你喜欢

Why is go language particularly popular in China

Technical analysis of qianyuantong multi card aggregation router

SimpleITK学习笔记

谷歌 | 蛋白序列的深度嵌入和比对

音频焦点系列:手写一个demo理解音频焦点与AudioMananger

Go practice - gorilla / handlers used by gorilla web Toolkit

联想R7000显卡的拆卸与安装

(perfect solution) how to set the position of Matplotlib legend freely

How to connect the network: Chapter 1 CSDN creation punch in

appium1.22.x 版本後的 appium inspector 需單獨安裝
随机推荐
[practical project] autonomous web server
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
XML Configuration File
[research materials] 2021 China's game industry brand report - Download attached
Win10 install pytullet and test
appium1.22. Appium inspector after X version needs to be installed separately
Go language interface learning notes Continued
Disassembly and installation of Lenovo r7000 graphics card
Chapter II program design of circular structure
Messy change of mouse style in win system
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
Why should we rewrite hashcode when we rewrite the equals method?
Why is go language particularly popular in China
1095 cars on campus (30 points)
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
SimpleITK学习笔记
1099 build a binary search tree (30 points)