当前位置:网站首页>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
边栏推荐
- JS dynamic table creation
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- Introduction to deep learning (II) -- univariate linear regression
- C language program ideas and several commonly used filters
- 50 practical applications of R language (36) - data visualization from basic to advanced
- Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
- @Autowired 导致空指针报错 解决方式
- Yolov5 input (II) | CSDN creative punch in
- Redis 击穿穿透雪崩
- Introduction to rust Foundation (basic type)
猜你喜欢
Why should we rewrite hashcode when we rewrite the equals method?
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
XML配置文件
Celebrate the new year together
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
Pessimistic lock and optimistic lock of multithreading
Why is go language particularly popular in China
Botu uses peek and poke for IO mapping
微服务常见面试题
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
随机推荐
Deep embedding and alignment of Google | protein sequences
[research materials] 2021 China's game industry brand report - Download attached
Common methods of JS array
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
Introduction to deep learning (II) -- univariate linear regression
How to connect the network: Chapter 1 CSDN creation punch in
XML Configuration File
Redis expiration elimination mechanism
1106 lowest price in supply chain (25 points)
Pessimistic lock and optimistic lock of multithreading
Differences among bio, NiO and AIO
"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
Why is go language particularly popular in China
Interview question -- output the same characters in two character arrays
穀歌 | 蛋白序列的深度嵌入和比對
Promise
Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
乾元通多卡聚合路由器的技术解析
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
Making coco datasets