当前位置:网站首页>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();
}
}
边栏推荐
- Gan network thought
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- Explanation of several points needing attention in final (tested by the author)
- Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
- Introduction to deep learning - definition Introduction (I)
- Overview of basic knowledge of C language
- ROS Compilation Principle
- appium1.22.x 版本后的 appium inspector 需单独安装
- Unity tool Luban learning notes 1
- Dynamic programming - related concepts, (tower problem)
猜你喜欢
Audio Focus Series: write a demo to understand audio focus and audiomananger
Principles of BTC cryptography
Detailed explanation of yolov5 training own data set
Go practice -- gorilla / websocket used by gorilla web Toolkit
3dslam with 16 line lidar and octomap
Botu uses peek and poke for IO mapping
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
Burp suite plug-in based on actual combat uses tips
Gan network thought
leetcode406. Rebuild the queue based on height
随机推荐
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
leetcode860. Lemonade change
穀歌 | 蛋白序列的深度嵌入和比對
Classification and discussion of plane grab detection methods based on learning
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
Common methods of JS array
Shallow and first code
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
最大连续子段和(动态规划,递归,递推)
1119 pre- and post order traversals (30 points)
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
(subplots用法)matplotlib如何绘制多个子图(轴域)
Go language interface learning notes Continued
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
1106 lowest price in supply chain (25 points)
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times