当前位置:网站首页>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
边栏推荐
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- "Pthread.h" not found problem encountered in compiling GCC
- es7创建索引容易犯的错误
- Calculation method of AUC
- [Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
- Promise
- 编译GCC遇到的“pthread.h” not found问题
- 2022-02-11 daily clock in: problem fine brush
- XML配置文件
- Self introduction and objectives
猜你喜欢

【实战项目】自主web服务器
![[practical project] autonomous web server](/img/99/892e600b7203c63bad02adb683c8f2.png)
[practical project] autonomous web server

Principles of BTC cryptography

2022-02-11 daily clock in: problem fine brush

BIO、NIO、AIO区别

Online VR model display - 3D visual display solution

Introduction to deep learning (II) -- univariate linear regression
![[basic grammar] C language uses for loop to print Pentagram](/img/9e/021c6c0e748e0981d4233f74c83e76.jpg)
[basic grammar] C language uses for loop to print Pentagram

About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller

JQ style, element operation, effect, filtering method and transformation, event object
随机推荐
"Pthread.h" not found problem encountered in compiling GCC
SimpleITK学习笔记
(perfect solution) how to set the position of Matplotlib legend freely
Yolov5 input (I) -- mosaic data enhancement | CSDN creative punch in
appium1.22.x 版本後的 appium inspector 需單獨安裝
Actual combat 8051 drives 8-bit nixie tube
MySQL master-slave configuration
动态规划——相关概念,(数塔问题)
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
"Hands on deep learning" pytorch edition Chapter II exercise
Learn libcef together -- set cookies for your browser
Redis 入門和數據類型講解
聊聊如何利用p6spy进行sql监控
BIO、NIO、AIO区别
1106 lowest price in supply chain (25 points)
1111 online map (30 points)
Training method of grasping angle in grasping detection
Force GCC to compile 32-bit programs on 64 bit platform
Basic introduction of redis and explanation of eight types and transactions
Introduction to deep learning - definition Introduction (I)