当前位置:网站首页>獲取並監控遠程服務器日志
獲取並監控遠程服務器日志
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();
}
}
边栏推荐
- "Hands on deep learning" pytorch edition Chapter II exercise
- 2022-02-12 daily clock in: problem fine brush
- 【实战项目】自主web服务器
- Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
- Pan details of deep learning
- Burp suite plug-in based on actual combat uses tips
- [backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
- BIO、NIO、AIO区别
- Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
- Deep embedding and alignment of Google | protein sequences
猜你喜欢

Intégration profonde et alignement des séquences de protéines Google

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

【实战项目】自主web服务器

appium1.22.x 版本后的 appium inspector 需单独安装

"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

Go practice -- gorilla / websocket used by gorilla web Toolkit
![[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached](/img/95/833f5ec20207ee5d7e6cdfa7208c5e.jpg)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached

微服务常见面试题

Why is go language particularly popular in China

JS scope
随机推荐
BIO、NIO、AIO区别
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
最大连续子段和(动态规划,递归,递推)
1114 family property (25 points)
JS string and array methods
2022-02-12 daily clock in: problem fine brush
聊聊如何利用p6spy进行sql监控
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
(perfect solution) how to set the position of Matplotlib legend freely
1103 integer factorization (30 points)
1111 online map (30 points)
1118 birds in forest (25 points)
Pessimistic lock and optimistic lock of multithreading
1095 cars on campus (30 points)
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
"Pthread.h" not found problem encountered in compiling GCC
leetcode860. Lemonade change
Audio Focus Series: write a demo to understand audio focus and audiomananger
Yolov5 input (I) -- mosaic data enhancement | CSDN creative punch in