java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟
更新時(shí)間:2023年09月08日 09:46:01 作者:Roc-xb
本文主要介紹了java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
一、引入依賴
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency>
二、工具類
(1)SSHExecutor類
import com.jcraft.jsch.*;
import java.io.*;
import java.util.concurrent.TimeUnit;
import static java.lang.String.format;
public class SSHExecutor {
private static long INTERVAL = 100L;
private static int SESSION_TIMEOUT = 30000;
private static int CHANNEL_TIMEOUT = 3000;
private JSch jsch = null;
private Session session = null;
public SSHExecutor(SSHInfo sshInfo) throws JSchException {
jsch = new JSch();
session = jsch.getSession(sshInfo.getUser(), sshInfo.getHost(), sshInfo.getPort());
session.setPassword(sshInfo.getPassword());
session.setUserInfo(new MyUserInfo());
session.connect(SESSION_TIMEOUT);
}
/*
* 注意編碼轉(zhuǎn)換
* */
public long shell(String cmd, String outputFileName) throws JSchException, IOException, InterruptedException {
long start = System.currentTimeMillis();
Channel channel = session.openChannel("shell");
PipedInputStream pipeIn = new PipedInputStream();
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
FileOutputStream fileOut = new FileOutputStream(outputFileName, true);
channel.setInputStream(pipeIn);
channel.setOutputStream(fileOut);
channel.connect(CHANNEL_TIMEOUT);
pipeOut.write(cmd.getBytes());
Thread.sleep(INTERVAL);
pipeOut.close();
pipeIn.close();
fileOut.close();
channel.disconnect();
return System.currentTimeMillis() - start;
}
public int exec(String cmd) throws IOException, JSchException, InterruptedException {
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(cmd);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
InputStream in = channelExec.getInputStream();
channelExec.connect();
int res = -1;
StringBuffer buf = new StringBuffer(1024);
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) break;
buf.append(new String(tmp, 0, i));
}
if (channelExec.isClosed()) {
res = channelExec.getExitStatus();
System.out.println( format("Exit-status: %d", res));
break;
}
TimeUnit.MILLISECONDS.sleep(100);
}
System.out.println(buf.toString());
channelExec.disconnect();
return res;
}
public Session getSession() {
return session;
}
public void close() {
getSession().disconnect();
}
/*
* SSH連接信息
* */
public static class SSHInfo {
private String user;
private String password;
private String host;
private int port;
public SSHInfo(String user, String password, String host, int port) {
this.user = user;
this.password = password;
this.host = host;
this.port = port;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
}
/*
* 自定義UserInfo
* */
private static class MyUserInfo implements UserInfo {
@Override
public String getPassphrase() {
return null;
}
@Override
public String getPassword() {
return null;
}
@Override
public boolean promptPassword(String s) {
return false;
}
@Override
public boolean promptPassphrase(String s) {
return false;
}
@Override
public boolean promptYesNo(String s) {
System.out.println(s);
System.out.println("true");
return true;
}
@Override
public void showMessage(String s) {
}
}
}(2)SSHUtil類
import com.jcraft.jsch.JSchException;
import java.io.IOException;
public class SSHUtil {
public static SSHExecutor sshExecutor;
public static void start (){
// 此處配置服務(wù)器信息
SSHExecutor.SSHInfo sshInfo = new SSHExecutor.SSHInfo("用戶名", "密碼", "host", "port");
try {
sshExecutor = new SSHExecutor(sshInfo);
System.err.println("ssh災(zāi)備日志服務(wù)器已鏈接");
} catch (Exception e) {
System.err.println("ssh災(zāi)備日志服務(wù)器鏈接失?。。?);
e.printStackTrace();
sshExecutor.close();
}
}
/**
* 運(yùn)行cmd命令
*/
public static void runCmd (String cmd) throws JSchException, IOException, InterruptedException {
sshExecutor.exec(cmd);
}
}三、使用方法
啟動類中加入
SSHUtil.start();
例如:運(yùn)行cmd命令
SSHUtil.runCmd("cmd");到此這篇關(guān)于java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟的文章就介紹到這了,更多相關(guān)java ssh連接服務(wù)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中BeanFactoryPostProcessors是如何執(zhí)行的
BeanFactoryPostProcessor是Spring容器提供的一個(gè)擴(kuò)展機(jī)制,它允許開發(fā)者在Bean的實(shí)例化和初始化之前對BeanDefinition進(jìn)行修改和處理,這篇文章主要介紹了你知道Spring中BeanFactoryPostProcessors是如何執(zhí)行的嗎,需要的朋友可以參考下2023-11-11
Springboot+Thymeleaf+Jpa實(shí)現(xiàn)登錄功能(附源碼)
最近有學(xué)習(xí)到關(guān)于Springboot+Thymeleaf+Jpa的綜合運(yùn)用知識,因此想寫一個(gè)簡單的登錄界面來嘗試一下,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
使用chatgpt實(shí)現(xiàn)微信聊天小程序的代碼示例
這篇文章主要介紹了使用chatgpt實(shí)現(xiàn)微信聊天小程序(秒回復(fù)),文中有詳細(xì)的代碼示例,對大家了解chatgpt聊天有一定的幫助,感興趣的同學(xué)可以參考閱讀2023-05-05
詳解Jenkins 實(shí)現(xiàn)Gitlab事件自動觸發(fā)Jenkins構(gòu)建及釘釘消息推送
這篇文章主要介紹了Jenkins 實(shí)現(xiàn)Gitlab事件自動觸發(fā)Jenkins構(gòu)建及釘釘消息推送,應(yīng)該會對大家學(xué)習(xí)Jenkins有所啟發(fā)2021-04-04

