一、本地执行 Linux 命令
1. 执行单条命令
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;publicclassShellUtil{publicvoidexecCmd(String cmd)throwsIOException{Runtime run =Runtime.getRuntime();Process proc =null;BufferedReader br =null;InputStream in =null;try{
proc = run.exec(cmd,null,null);
in = proc.getInputStream();
br =newBufferedReader(newInputStreamReader(in));String result;while((result = br.readLine())!=null){System.out.println("job result ["+ result +"]");}
proc.waitFor();}catch(Exception e){
e.printStackTrace();}finally{if(proc !=null)
proc.destroy();if(in !=null)
in.close();if(br !=null)
br.close();}}}
2. 执行含有管道符(|)的多级命令
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.List;publicclassShellUtil{publicList<String>execCmd(String cmd)throwsIOException{List<String> list =newArrayList<>();Runtime run =Runtime.getRuntime();Process proc =null;BufferedReader br =null;InputStream in =null;try{
proc = run.exec(newString[]{"/bin/sh","-c", cmd});
in = proc.getInputStream();
br =newBufferedReader(newInputStreamReader(in));String result;while((result = br.readLine())!=null){System.out.println("job result ["+ result +"]");
list.add(result);}
proc.waitFor();return list;}catch(Exception e){
e.printStackTrace();}finally{if(proc !=null)
proc.destroy();if(in !=null)
in.close();if(br !=null)
br.close();}return list;}}
3. 执行多条命令
importjava.io.*;importjava.util.ArrayList;importjava.util.List;publicclassShellUtil{/**
* 命令集合
*/publicstaticList<String>getCommandList(){String path ="/root";List<String> commands =newArrayList<>();
commands.add("cd "+ path);
commands.add("ls");return commands;}/**
* 执行命令
*/publicstaticList<String>execCommands(List<String> commands)throwsIOException{List<String> list =newArrayList<>();Runtime run =Runtime.getRuntime();Process proc =null;BufferedReader in =null;PrintWriter out =null;try{
proc = run.exec("/bin/bash",null,null);
in =newBufferedReader(newInputStreamReader(proc.getInputStream()));
out =newPrintWriter(newBufferedWriter(newOutputStreamWriter(proc.getOutputStream())),true);// 写入执行命令for(String line : commands){
out.println(line);}// 这个命令必须执行,否则 in 流不结束
out.println("exit");String line;while((line = in.readLine())!=null){System.out.println("readLine: "+ line);
list.add(line);}// 释放资源
proc.waitFor();}catch(Exception e){
e.printStackTrace();}finally{if(proc !=null)
proc.destroy();if(in !=null)
in.close();if(out !=null)
out.close();}return list;}}
二、远程执行 Linux 命令
<dependency><groupId>ch.ethz.ganymed</groupId><artifactId>ganymed-ssh2</artifactId><version>262</version></dependency>
importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;importch.ethz.ssh2.StreamGobbler;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;publicclassLinuxUtil{publicstaticvoidmain(String[] args){String hostname ="127.0.0.1";String username ="root";String password ="123456";try{Connection conn =newConnection(hostname);
conn.connect();boolean isAuthenticated = conn.authenticateWithPassword(username, password);if(!isAuthenticated){thrownewIOException("Authentication failed");}Session sess = conn.openSession();// 命令语句,必须使用绝对路径否则无效(环境变量也不可以)。如:java --version
sess.execCommand("pwd");InputStream stdout =newStreamGobbler(sess.getStdout());BufferedReader br =newBufferedReader(newInputStreamReader(stdout));while(true){String line = br.readLine();if(line ==null){break;}// 输出命令执行结果System.out.println(line);}
sess.close();
conn.close();}catch(IOException e){
e.printStackTrace(System.err);System.exit(2);}}}
本文转载自: https://blog.csdn.net/baihaibo1024/article/details/131399965
版权归原作者 baihb1024 所有, 如有侵权,请联系我们删除。
版权归原作者 baihb1024 所有, 如有侵权,请联系我们删除。