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

Java操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令

 更新時(shí)間:2025年01月02日 09:35:45   作者:一起喝芬達(dá)2010  
這篇文章主要為大家詳細(xì)介紹了Java如何操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

引入依賴

<dependency>
    <groupId>ch.ethz.ganymed</groupId>
    <artifactId>ganymed-ssh2</artifactId>
    <version>262</version>
</dependency>

SSH2Util 工具類封裝

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class SSH2Util {

    //指定默認(rèn)編碼
    private static String DEFAULT_CHARSET = "UTF-8";

    /**
     * 建立SSH2連接
     * @param host 主機(jī)地址
     * @param username 用戶名
     * @param password 密碼
     * @return Connection
     */
    public static Connection openConnection(String host,String username,String password) {
        try {
            Connection connection = new Connection(host);
            //建立ssh2連接
            connection.connect();
            //檢驗(yàn)用戶名
            boolean login = connection.authenticateWithPassword(username,password);
            if (login){
                logger.info(host + " 連接成功");
                return connection;
            }else {
                throw new RuntimeException(host + " 用戶名密碼不正確");
            }
        } catch (Exception e) {
            throw new RuntimeException(host +" "+ e);
        }
    }

    /**
     * 執(zhí)行命令
     * @param connection ssh2連接對(duì)象
     * @param command 命令語(yǔ)句
     * @return 執(zhí)行結(jié)果, 封裝執(zhí)行狀態(tài)和執(zhí)行結(jié)果
     */
    public static ExecCmdResult execCommand(Connection connection,String command){
        ExecCmdResult execCmdResult = new ExecCmdResult();
        Session session = null;
        try{
            session = connection.openSession();
            //執(zhí)行命令
            session.execCommand(command);
            //解析結(jié)果
            String result = parseResult(session.getStdout());
            //解析結(jié)果為空,則表示執(zhí)行命令發(fā)生了錯(cuò)誤,嘗試解析錯(cuò)誤出輸出
            if (result == null||result.length()==0){
                result = parseResult(session.getStderr());

            }else {
                execCmdResult.setSuccess(true);
            }
            //設(shè)置響應(yīng)結(jié)果
            execCmdResult.setResult(result);
            logger.info(command + " ==>> " +execCmdResult.getResult());
            return execCmdResult;

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String parseResult(InputStream inputStream) throws IOException{
        //讀取輸出流內(nèi)容
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,DEFAULT_CHARSET));
        StringBuffer resultSB = new StringBuffer();
        String line;
        while((line = br.readLine()) != null){
            resultSB.append(line+"\n");
        }
        //替換換行符
        String result = resultSB.toString().replaceAll("[\\t\\n\\r]", "");
        return result;

    }
}

ExecCmdResult 定義返回結(jié)果類

public class ExecCmdResult {

    //命令執(zhí)行是否成功
    private boolean flag ;
    //輸出結(jié)果
    private String result;

    public void setFlag(boolean success){
        this.flag = success;
    }

    public boolean getFlag() {
        return flag;
    }

    public String getResult(){
        return result;
    }
    public void setResult(String result){
        this.result = result;
    }

}

SSH2Demo 測(cè)試

import ch.ethz.ssh2.Connection;

public class SSH2Demo {

    public static void main(String[] args) {
        try {
            String host = "168.192.22.7";
            String username = "root";
            String password = "123456";
            Connection connection = SSH2Util.openConnection(host,username,password);
            String cpuInfo = "cat /proc/cpuinfo | grep \"cpu cores\" | uniq"; //服務(wù)器核數(shù)
            ExecCmdResult cup = SSH2Util.execCommand(connection,cpuInfo);
            connection.close();
        }
        catch (Exception a){
            a.printStackTrace();
        }
    }
}

到此這篇關(guān)于Java操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的文章就介紹到這了,更多相關(guān)Java SSH2遠(yuǎn)程執(zhí)行l(wèi)inux命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

玉树县| 沛县| 且末县| 上思县| 囊谦县| 犍为县| 鄢陵县| 西乌珠穆沁旗| 开阳县| 涡阳县| 黑龙江省| 涟源市| 五台县| 汉中市| 二连浩特市| 东海县| 沙雅县| 柳林县| 修武县| 丽江市| 久治县| 会理县| 巫溪县| 时尚| 正蓝旗| 泗阳县| 贵定县| 深水埗区| 五大连池市| 玛沁县| 衡阳市| 广州市| 临湘市| 出国| 博白县| 察隅县| 新晃| 昂仁县| 钟祥市| 成武县| 铁岭市|