当前位置:网站首页>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
边栏推荐
- Actual combat 8051 drives 8-bit nixie tube
- 谷歌 | 蛋白序列的深度嵌入和比对
- Configure and use Anaconda environment in pycharm
- Win10 install pytullet and test
- Transferring images using flask
- appium1.22. Appium inspector after X version needs to be installed separately
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- [basic grammar] Snake game written in C language
- Chapter II program design of circular structure
- Go practice -- use redis in golang (redis and go redis / redis)
猜你喜欢

微服务常见面试题

"Hands on deep learning" pytorch edition Chapter II exercise

Gbase8s composite index (I)

Audio Focus Series: write a demo to understand audio focus and audiomananger

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

Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn

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

Deep embedding and alignment of Google | protein sequences
![[basic grammar] C language uses for loop to print Pentagram](/img/9e/021c6c0e748e0981d4233f74c83e76.jpg)
[basic grammar] C language uses for loop to print Pentagram

Celebrate the new year together
随机推荐
JQ style, element operation, effect, filtering method and transformation, event object
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
最大连续子段和(动态规划,递归,递推)
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
Transferring images using flask
Technical analysis of qianyuantong multi card aggregation router
1103 integer factorization (30 points)
Go practice -- design patterns in golang's singleton
Rust基础入门之(基本类型)
Basic introduction of redis and explanation of eight types and transactions
XML Configuration File
乾元通多卡聚合路由器的技术解析
Pan details of deep learning
Basic knowledge of reflection (detailed explanation)
Why should we rewrite hashcode when we rewrite the equals method?
联想R7000显卡的拆卸与安装
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
BIO、NIO、AIO区别
Deploy crawl detection network using tensorrt (I)
Redis Introduction et explication des types de données