最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • 詳解Java抽象類與普通類的區(qū)別

    詳解Java抽象類與普通類的區(qū)別

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著Java抽象類與普通類的區(qū)別展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • MybatisPlus將自定義的sql列表查詢返回改為分頁查詢

    MybatisPlus將自定義的sql列表查詢返回改為分頁查詢

    本文主要介紹了MybatisPlus將自定義的sql列表查詢返回改為分頁查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • SpringBoot+MySQL實(shí)現(xiàn)讀寫分離的多種具體方案

    SpringBoot+MySQL實(shí)現(xiàn)讀寫分離的多種具體方案

    在高并發(fā)和大數(shù)據(jù)量的場景下,數(shù)據(jù)庫成為了系統(tǒng)的瓶頸。為了提高數(shù)據(jù)庫的處理能力和性能,讀寫分離成為了一種常用的解決方案,本文將介紹在Spring?Boot項(xiàng)目中實(shí)現(xiàn)MySQL數(shù)據(jù)庫讀寫分離的多種具體方案,需要的朋友可以參考下
    2023-06-06
  • 詳解spring cloud hystrix 請求合并collapsing

    詳解spring cloud hystrix 請求合并collapsing

    這篇文章主要介紹了詳解spring cloud hystrix 請求合并collapsing,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • spring boot org.junit.jupiter.api不存在的解決

    spring boot org.junit.jupiter.api不存在的解決

    這篇文章主要介紹了spring boot org.junit.jupiter.api不存在的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解Spring AOP的原理與實(shí)現(xiàn)方式

    詳解Spring AOP的原理與實(shí)現(xiàn)方式

    Spring框架是一個(gè)功能強(qiáng)大且靈活的企業(yè)級應(yīng)用程序開發(fā)框架,其中最重要的特性之一就是面向切面編程(AOP),我們今天這篇文章將從源碼和案例的角度詳細(xì)介紹Spring AOP的思想、原理和實(shí)現(xiàn)方式
    2023-07-07
  • SpringBoot上下文初始器加載過程詳解

    SpringBoot上下文初始器加載過程詳解

    這篇文章主要介紹了SpringBoot上下文初始器加載過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring MVC完全注解方式配置web項(xiàng)目

    Spring MVC完全注解方式配置web項(xiàng)目

    這篇文章主要為大家詳細(xì)介紹了Spring MVC完全注解方式配置web項(xiàng)目的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Spring Boot非Web項(xiàng)目運(yùn)行的方法

    Spring Boot非Web項(xiàng)目運(yùn)行的方法

    這篇文章主要給大家介紹了關(guān)于Spring Boot非Web項(xiàng)目運(yùn)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringBoot應(yīng)用中的多層緩存實(shí)踐

    SpringBoot應(yīng)用中的多層緩存實(shí)踐

    在本文中,我們將深入探討SpringBoot應(yīng)用中多層緩存的實(shí)現(xiàn)思路,通過本地Caffeine緩存(L1)和遠(yuǎn)程Redis緩存(L2)的組合,減少與遠(yuǎn)程服務(wù)的通信次數(shù),提升應(yīng)用性能,感興趣的可以了解一下
    2026-02-02

最新評論

积石山| 天长市| 屯昌县| 都兰县| 肥西县| 专栏| 上虞市| 肇源县| 泗洪县| 辽阳市| 汾西县| 海晏县| 理塘县| 巢湖市| 曲周县| 襄樊市| 栾川县| 巴彦县| 玛曲县| 昂仁县| 宾阳县| 灵璧县| 大关县| 承德县| 巨野县| 拉萨市| 三明市| 威信县| 乌兰浩特市| 隆化县| 广德县| 田阳县| 旬邑县| 彭水| 资源县| 申扎县| 南投市| 河南省| 鹤峰县| 临泉县| 洛宁县|