Java 操作linux工具類

Java操作Linux工具類 引言 Linux是一種常用的操作系統,在日常開發和運維工作中,我們經常需要通過Java程序來操作Linux系統的一些工具和命令。本文將介紹如何使用Java操作Linux工具類,包

Java操作Linux工具類

引言

Linux是一種常用的操作系統,在日常開發和運維工作中,我們經常需要通過Java程序來操作Linux系統的一些工具和命令。本文將介紹如何使用Java操作Linux工具類,包括調用系統命令、執行Shell腳本、遠程連接等。

調用系統命令

Java提供了Runtime類和ProcessBuilder類,可以方便地調用系統命令。

使用Runtime類執行系統命令的代碼示例:

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

public class CommandExecutor {
    public static void main(String[] args) {
        String command = "ls -l";
        try {
            Process process = Runtime.getRuntime().exec(command);
            InputStream inputStream = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用ProcessBuilder類執行系統命令的代碼示例:

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

public class CommandExecutor {
    public static void main(String[] args) {
        ProcessBuilder processBuilder = new ProcessBuilder("ls", "-l");
        try {
            Process process = processBuilder.start();
            InputStream inputStream = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

執行Shell腳本

除了調用系統命令,我們還可以使用Java執行Shell腳本。

使用Java執行Shell腳本的代碼示例:

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

public class ShellExecutor {
    public static void main(String[] args) {
        String command = "sh script.sh";
        try {
            Process process = Runtime.getRuntime().exec(command);
            InputStream inputStream = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

遠程連接

有時候我們需要通過Java程序與遠程Linux服務器進行交互,可以借助SSH協議進行遠程連接。

使用Java進行遠程連接的代碼示例:

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class SSHExecutor {
    public static void main(String[] args) {
        String host = "192.168.0.100";
        String username = "root";
        String password = "123456";
        int port = 22;

        try {
            JSch jSch = new JSch();
            Session session = jSch.getSession(username, host, port);
            session.setPassword(password);

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand("ls -l");

            InputStream inputStream = channel.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();

            channel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

類圖

使用Mermaid語法繪制的類圖如下所示:

classDiagram
  class Runtime {
    +exec(String command) : Process
  }
  class Process {
    +getInputStream() : InputStream
    +getErrorStream() : InputStream
  }
  class InputStream {
    +readLine() : String
  }
  class BufferedReader {
    +BufferedReader(InputStreamReader in) : void
    +readLine() : String
    +close() : void
  }
  class ProcessBuilder {
    +ProcessBuilder(String... command) : void
    +start() : Process
  }
  class Channel {
    +getInputStream() : InputStream
    +getOutputStream() : OutputStream
  }
  class ChannelExec {
    +setCommand(String command) : void
  }
  class JSch {
    +getSession(String username, String host, int port) : Session
  }
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 攵女yin乱合集小丹| 99久久无色码中文字幕| 超兴奋的朋…中文字幕| 校花哭着扒开屁股浣肠于柔 | 国产精品社区在线观看| 亚洲系列第一页| av天堂午夜精品一区| 狠狠亚洲婷婷综合色香五月排名| 妈妈的朋友在8完整有限中字5| 午夜无码国产理论在线| 一级毛片不卡片免费观看| 精品小视频在线| 孕妇videos孕交| 免费黄色片网址| av片在线观看永久免费| 熟妇人妻va精品中文字幕| 国内精品伊人久久久久网站 | 久久五月天综合| 青青热久免费精品视频在线观看 | 99精品在线播放| 欧美疯狂性受xxxxx另类| 国产精品自在自线| 亚洲中文字幕久久精品无码a | 97一区二区三区四区久久| 日韩人妻无码免费视频一区二区三区 | 无码任你躁久久久久久| 啦啦啦手机完整免费高清观看| 中国胖女人一级毛片aaaaa| 精品久久久久久中文字幕无碍| 天美麻花视频大全| 亚洲欧美日韩另类在线| 777奇米影视视频在线播放| 日韩欧美中文精品电影| 国产va免费精品高清在线| 一级黄色香蕉视频| 激情六月在线视频观看| 国产精品一卡二卡三卡| 久久精品中文字幕一区| 美国式禁忌5太大了| 埋在老师腿间喝圣水 | 一级特黄录像免费播放中文版|