国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

JSch連接linux

2024-08-27 23:56:18
字體:
來源:轉載
供稿:網友
package cn.com.servyou.sdi.web.utils;import cn.com.servyou.sdi.stream.SdiException;import cn.com.servyou.sdi.web.vo.SshServer;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelExec;import com.jcraft.jsch.Channelshell;import com.jcraft.jsch.JSch;import com.jcraft.jsch.JSchException;import com.jcraft.jsch.Session;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;/** * java利用jsch遠程連接linux進行管理 **/public class SSHUtils {    private static final Logger LOGGER = LoggerFactory.getLogger(SSHUtils.class);    public static Session getJschSession(SshServer sshServer) {        JSch jsch = new JSch();        try {            Session session = jsch.getSession(sshServer.getSshServerUser(), sshServer.getSshServerUrl());            session.setPassword(sshServer.getSshServerPassword());            //第一次訪問服務器不用輸入yes            session.setConfig("StrictHostKeyChecking", "no");            session.setTimeout(60 * 60 * 1000);            session.connect();            return session;        } catch (JSchException e) {            LOGGER.error("連接SSH服務器失敗", e);            return null;        }    }    public static String executeCommand(SshServer sshServer, String command) {        // 執行單句命令        Session session = getJschSession(sshServer);        if (null == session) {            throw new Exception("3", "登錄SSH服務器失敗");        }        String result = "";        try {            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");            InputStream in = channelExec.getInputStream();            channelExec.setCommand(command);            channelExec.setErrStream(System.err);            channelExec.connect();            result = getResult(in, channelExec);            channelExec.disconnect();        } catch (JSchException | IOException e) {            LOGGER.error("執行指令失敗", e);            throw new Exception("5", "執行指令失敗");        }        session.disconnect();        return result;    }    public static String executeShell(SshServer sshServer, String[] commands) {        if (null == commands || commands.length == 0) {            throw new Exception("12", "無指令");        }        String result = "";        Session session = getJschSession(sshServer);        if (null == session) {            throw new Exception("00003", "登錄SSH服務器失敗");        }        try {            ChannelShell channelShell = (ChannelShell) session.openChannel("shell");            //從遠端到達的數據  都能從這個流讀取到            InputStream inputStream = channelShell.getInputStream();            channelShell.setPty(true);            channelShell.connect();            //寫入該流的數據  都將發送到遠程端            OutputStream outputStream = channelShell.getOutputStream();            PrintWriter printWriter = new PrintWriter(outputStream);            for (String command : commands) {                printWriter.println(command);            }            //把緩沖區的數據強行輸出            printWriter.flush();            result = getResult(inputStream, channelShell);            outputStream.close();            inputStream.close();            channelShell.disconnect();        } catch (JSchException | IOException e) {            LOGGER.error("執行指令失敗", e);        }        session.disconnect();        return result;    }    private static String getResult(InputStream in, Channel channel) throws IOException {        StringBuilder sb = new StringBuilder();        byte[] tmp = new byte[1024];        while (!channel.isClosed()) {            while (in.available() > 0) {                int i = in.read(tmp, 0, 1024);                if (i < 0) {                    break;                }                sb.append(new String(tmp, 0, i));            }        }        return sb.toString();    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合川市| 奇台县| 宝鸡市| 定州市| 扬中市| 阿拉善左旗| 腾冲县| 泸州市| 肥西县| 开江县| 扎赉特旗| 象山县| 应用必备| 永兴县| 兰考县| 威远县| 临城县| 天津市| 藁城市| 永善县| 慈利县| 灵山县| 梁山县| 新疆| 河南省| 鞍山市| 师宗县| 秭归县| 芷江| 广丰县| 尼玛县| 唐海县| 绍兴市| 宽城| 阿巴嘎旗| 德令哈市| 永平县| 杭锦后旗| 广宗县| 凤台县| 新闻|