当前位置:网站首页>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();
}
}
边栏推荐
- Promise
- leetcode406. Rebuild the queue based on height
- Objects. Requirenonnull method description
- Introduction to rust Foundation (basic type)
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
- Go language interface learning notes Continued
- Redis Introduction et explication des types de données
- 1086 tree traversals again (25 points)
- Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
猜你喜欢

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

Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +

SimpleITK学习笔记

Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in

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

Progressive multi grasp detection using grasp path for rgbd images

Why should we rewrite hashcode when we rewrite the equals method?

Disassembly and installation of Lenovo r7000 graphics card

穀歌 | 蛋白序列的深度嵌入和比對

Deep embedding and alignment of Google | protein sequences
随机推荐
Celebrate the new year together
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
JS scope
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
Basic knowledge of reflection (detailed explanation)
Skip table: principle introduction, advantages and disadvantages of skiplist
Gbase8s composite index (I)
XML配置文件
Progressive multi grasp detection using grasp path for rgbd images
[basic grammar] C language uses for loop to print Pentagram
Why should we rewrite hashcode when we rewrite the equals method?
2022-02-11 daily clock in: problem fine brush
Botu uses peek and poke for IO mapping
Redis 击穿穿透雪崩
study hard and make progress every day
C language program ideas and several commonly used filters
[basic grammar] Snake game written in C language
Chapter II program design of circular structure
The IntelliJ platform completely disables the log4j component
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram