Windows系統下如何使用Java代碼操作Linux

Windows系統下如何使用Java代碼操作Linux?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!

創新互聯公司是一家集網站建設,薌城企業網站建設,薌城品牌網站建設,網站定制,薌城網站建設報價,網絡營銷,網絡優化,薌城網站推廣為一體的創新建站企業,幫助傳統企業提升企業形象加強企業競爭力。可充分滿足這一群體相比中小企業更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們為更多的企業打造出實用型網站。

一、場景描述:

主項目(Web)部署在Windows下,算法項目(TensorFlow)部署在Linux環境下。

二、依賴環境(Jar)

        <!--Java SSH插件-->
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh3</artifactId>
            <version>build210</version>
        </dependency>
        <dependency>
            <groupId>sshtools</groupId>
            <artifactId>j2ssh-core</artifactId>
            <version>0.2.9</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

三、后端代碼

package cn.virgo.audio.utils;


import ch.ethz.ssh3.ChannelCondition;
import ch.ethz.ssh3.Connection;
import ch.ethz.ssh3.Session;
import ch.ethz.ssh3.StreamGobbler;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.sftp.SftpFile;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class RemoteShellExecutor {

    private Connection conn;

    private String ip;

    private String userName;

    private String password;

    private String charset = Charset.defaultCharset().toString();

    private static final int TIME_OUT = 1000 * 5 * 60;

    /**
     * 構造函數
     *
     * @param ip
     * @param userName
     * @param password
     */
    public RemoteShellExecutor(String ip, String userName, String password) {
        this.ip = ip;
        this.userName = userName;
        this.password = password;
    }

    /**
     * 鏈接遠程桌面
     *
     * @return
     * @throws IOException
     */
    private boolean login() throws IOException {
        conn = new ch.ethz.ssh3.Connection(ip);
        conn.connect();
        return conn.authenticateWithPassword(userName, password);
    }

    /**
     * 執行shell
     *
     * @param cmds
     * @return
     * @throws Exception
     */
    public int exec(String cmds) throws Exception {
        InputStream stdOut = null;
        InputStream stdErr = null;
        int ret = -1;
        try {
            if (login()) {
                Session session = conn.openSession();
                session.execCommand(cmds);
                stdOut = new StreamGobbler(session.getStdout());
                processStream(stdOut, charset);
                stdErr = new StreamGobbler(session.getStderr());
                processStream(stdErr, charset);
                session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
                ret = session.getExitStatus();
            } else {
                throw new Exception("遠程鏈接失敗:" + ip);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
            IOUtils.closeQuietly(stdOut);
            IOUtils.closeQuietly(stdErr);
        }
        return ret;
    }

    /**
     * 獲取執行過程輸出
     *
     * @param in
     * @param charset
     * @return
     * @throws IOException
     */
    private void processStream(InputStream in, String charset) throws IOException {
        byte[] buf = new byte[1024];
        while (in.read(buf) != -1) {
            System.out.println(new String(buf, charset));
        }
    }

    /**
     *  獲取Linux下某個文件數據,將其拷貝到本地tmpPath下
     */
    public List<String> getCaleResByFileFromSSH(String filePath, String filename, String tmpPath) {
        List<String> resList = new ArrayList<>();
        SshClient client = new SshClient();
        try {
            client.connect(this.ip);
            //設置用戶名和密碼
            PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
            pwd.setUsername(this.userName);
            pwd.setPassword(this.password);
            int result = client.authenticate(pwd);
            if (result == AuthenticationProtocolState.COMPLETE) {//如果連接完成
                List<SftpFile> list = client.openSftpClient().ls(filePath);
                for (SftpFile f : list) {
                    if (f.getFilename().equals(filename)) {
                        OutputStream os = new FileOutputStream(tmpPath + f.getFilename());
                        client.openSftpClient().get(f.getAbsolutePath(), os);
                        //以行為單位讀取文件start
                        File file = new File(tmpPath + f.getFilename());
                        BufferedReader reader = null;
                        try {
                            reader = new BufferedReader(new FileReader(file));
                            String tempString = null;
                            int line = 1;//行號
                            //一次讀入一行,直到讀入null為文件結束
                            while ((tempString = reader.readLine()) != null) {
                                //顯示行號
                                System.out.println("line " + line + ": " + tempString);
                                resList.add(tempString);
                                line++;
                            }
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (reader != null) {
                                try {
                                    reader.close();
                                } catch (IOException e1) {
                                }
                            }
                        }
                        //以行為單位讀取文件end
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resList;
    }
}

感謝各位的閱讀!看完上述內容,你們對Windows系統下如何使用Java代碼操作Linux大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注創新互聯行業資訊頻道。

本文題目:Windows系統下如何使用Java代碼操作Linux
本文鏈接:http://m.kartarina.com/article4/gecpoe.html

成都網站建設公司_創新互聯,為您提供標簽優化網頁設計公司網站導航App開發企業網站制作網站制作

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司
主站蜘蛛池模板: 色综合久久久无码网中文| 久久午夜无码鲁丝片| 无码人妻少妇伦在线电影 | 久久午夜无码免费| 中文字幕无码无码专区| 亚洲国产精品无码成人片久久| 久久99久久无码毛片一区二区| 精品无码人妻一区二区三区不卡| 国产成人精品无码一区二区三区| 国产精品ⅴ无码大片在线看| 日韩精品无码一区二区中文字幕| 日韩免费无码一区二区三区| 中文字幕av无码专区第一页| 国产成人无码午夜福利软件| 国产成A人亚洲精V品无码| 日本无码一区二区三区白峰美| 日韩精品无码免费专区午夜 | 免费无码国产在线观国内自拍中文字幕 | 国产Av激情久久无码天堂| 人妻少妇伦在线无码| 中文无码精品A∨在线观看不卡| 免费无码作爱视频| 亚洲精品无码鲁网中文电影| 久久亚洲AV永久无码精品| 精品久久久无码中字| 国产成人无码网站| 亚洲人成影院在线无码观看| 精品乱码一区内射人妻无码| 亚洲AV无码成人精品区狼人影院| 无码人妻久久一区二区三区| 久久久久亚洲AV片无码| 97人妻无码一区二区精品免费| 久久久久亚洲av无码尤物| 久久AV无码精品人妻出轨| 无码任你躁久久久久久| 国产日韩精品无码区免费专区国产 | 秋霞鲁丝片无码av| 国精品无码一区二区三区在线蜜臀| 野花在线无码视频在线播放| 亚洲中文无码mv| 国内精品人妻无码久久久影院|