当前位置:网站首页>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();
}
}
边栏推荐
- Yolov5 network structure + code + application details | CSDN creation punch in
- Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
- 在PyCharm中配置使用Anaconda环境
- 乾元通多卡聚合路由器的技术解析
- Gbase8s unique index and non unique index
- Common methods of JS array
- Redis breakdown penetration avalanche
- 1099 build a binary search tree (30 points)
- XML Configuration File
- 1106 lowest price in supply chain (25 points)
猜你喜欢

Training method of grasping angle in grasping detection

Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément

Online VR model display - 3D visual display solution

Actual combat 8051 drives 8-bit nixie tube

(perfect solution) how to set the position of Matplotlib legend freely

How to connect the network: Chapter 1 CSDN creation punch in

Overview of basic knowledge of C language
![[research materials] 2021 China's game industry brand report - Download attached](/img/b7/a377b0b7c742078e2feb28ebfbca62.jpg)
[research materials] 2021 China's game industry brand report - Download attached

Three representations of signed numbers: original code, inverse code and complement code

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. Appium inspector after X version needs to be installed separately
Online VR model display - 3D visual display solution
Making coco datasets
"Pthread.h" not found problem encountered in compiling GCC
联想R7000显卡的拆卸与安装
BIO、NIO、AIO区别
Objects. Requirenonnull method description
3dslam with 16 line lidar and octomap
C language program ideas and several commonly used filters
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
1118 birds in forest (25 points)
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
Chapter II program design of circular structure
Go language interface learning notes
2022-02-11 daily clock in: problem fine brush
1106 lowest price in supply chain (25 points)
Principles of BTC cryptography
(perfect solution) how to set the position of Matplotlib legend freely
ROS Compilation Principle