当前位置:网站首页>Obtenir et surveiller les journaux du serveur distant
Obtenir et surveiller les journaux du serveur distant
2022-07-03 05:18:00 【Deux chiens anxieux】
TestsJSch
Adresse originale:https://www.freesion.com/article/3557457395/
POMConfiguration
<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>
Entrée de la fonction
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";
// Exécuter les informations d'entrée
JSchUtil js = new JSchUtil(username, password, host, 22);
js.Connection();
js.executeCmd(command1);
}
}
UTILCatégorie
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 {
// Initialiser les variables internes de la classe
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;
}
/*
Connectez - vous à la désignationIP
*/
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();
}
/*
Fermer le lien d'instance
*/
public void disconnection() {
if (session != null && session.isConnected()) {
session.disconnect();
}
}
/*
Mise en œuvresshInterface de commande
*/
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("**********************************");
// Les résultats des tests peuvent être retournés pour faire une correspondance régulière pour un jugement logique
System.out.println("ReturnResult:" + "\n" + buffer.toString());
channel.disconnect();
}
}
边栏推荐
- Go practice -- closures in golang (anonymous functions, closures)
- Introduction to redis and explanation of data types
- 乾元通多卡聚合路由器的技术解析
- 1118 birds in forest (25 points)
- Burp suite plug-in based on actual combat uses tips
- Introduction to deep learning (II) -- univariate linear regression
- [set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
- Messy change of mouse style in win system
- Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
- appium1.22.x 版本後的 appium inspector 需單獨安裝
猜你喜欢
![[basic grammar] Snake game written in C language](/img/cb/83631ef3ccd7047ca42d33dc49bf90.jpg)
[basic grammar] Snake game written in C language

JS scope

Yolov5 network structure + code + application details | CSDN creation punch in

Basic knowledge of reflection (detailed explanation)

微服务常见面试题

Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN

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

Technical analysis of qianyuantong multi card aggregation router

DEX net 2.0 for crawl detection

Win10 install pytullet and test
随机推荐
谷歌 | 蛋白序列的深度嵌入和比对
1086 tree traversals again (25 points)
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Redis 入門和數據類型講解
BTC-密码学原理
Common interview questions of microservice
JS scope
@Autowired 导致空指针报错 解决方式
Yolov5 input (I) -- mosaic data enhancement | CSDN creative punch in
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
appium1.22.x 版本後的 appium inspector 需單獨安裝
最大连续子段和(动态规划,递归,递推)
Realize file download through the tag of < a > and customize the file name
The IntelliJ platform completely disables the log4j component
appium1.22. Appium inspector after X version needs to be installed separately
1115 counting nodes in a BST (30 points)
Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
ROS Compilation Principle
Technical analysis of qianyuantong multi card aggregation router
Audio Focus Series: write a demo to understand audio focus and audiomananger