Java执行sudo命令

public static void main(String[] args) throws IOException {

    String[] cmd = {"/bin/bash","-c","echo password| sudo -S ls"};
    Process pb = Runtime.getRuntime().exec(cmd);

    String line;
    BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    input.close();
}

 

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * <p>
 * 这个类用于在Java程序中构建和执行Linux中的sudo命令。
 * 用法1:
 *    1. 执行buildCommands(...)方法构造sudo命令串。有两种方法可以构造sudo命令串:
 *
 *       若调用builderCommands(String cmd)方法构造sudo命令串,则此前应先修改/etc/sudoers文件,
 *       在其中添加一行:
 *          username  ALL=(ALL) NOPASSWD:ALL
 *       其中,"username"是需要运行这个程序的用户名。
 *
 *       若不想修改/etc/sudoers文件,则需要调用builderCommands(String cmd, String passwd)方法
 *       构造sudo命令串,
 *
 *       注意:无论使用哪种方法,形参cmd中均值包含需要以sudo方式执行的命令,不包含"sudo"命令本身。
 *
 *    2. 调用run(String[] cmds)执行由buildCommands返回的命令串数组。
 *
 * 用法2:
 *    1. 修改/etc/sudoers文件,在其中添加一行:
 *          username  ALL=(ALL) NOPASSWD:ALL
 *       其中,"username"是需要运行这个程序的用户名。
 *
 *    2. 调用run(String cmd)方法执行命令。
 *
 *    注意:形参cmd中仅包含需要以sudo方式执行的命令字符串,不要包含"sudo"命令本身。
 * </p>
 */

/**
 * @author kingfox
 *
 */
public class SudoExecutor
{
   public static void run(String cmd) throws IOException, InterruptedException
   {
      String sudoCmd = "sudo " + cmd;
      System.out.println(sudoCmd);
      Process process = Runtime.getRuntime().exec(sudoCmd);
      InputStreamReader ir = new InputStreamReader(process.getInputStream());
      LineNumberReader input = new LineNumberReader(ir);
      String line;
      while((line = input.readLine()) != null)
      {
         System.out.println(line);
      }
   }

   public static void run(String[] cmds) throws IOException, InterruptedException
   {
      //      /* __debug_code__
      for(String cmd : cmds)
      {
         System.out.print(cmd);
         System.out.print(' ');
      }
      System.out.println();
      //      */
      Process process = Runtime.getRuntime().exec(cmds);
      InputStreamReader ir = new InputStreamReader(process.getInputStream());
      LineNumberReader input = new LineNumberReader(ir);
      String line;
      while((line = input.readLine()) != null)
      {
         System.out.println(line);
      }
   }

   /**
    *
    * @param cmd
    * @return
    */
   public static String[] buildCommands(String cmd)   // to use this method, you should modify /etc/sudoers
   {
      String[] cmds = {shellName, shellParam, sudoCmd + " " + cmd};
      return cmds;
   }

   public static String[] buildCommands(String cmd, String sudoPasswd)
   {
      String[] cmds = {shellName, shellParam, "echo \"" + sudoPasswd + "\" | " + sudoCmd + " -S " + cmd};
      return cmds;
   }

   protected static String sudoCmd = "sudo";
   protected static String shellName = "/bin/bash";
   protected static String shellParam = "-c";

   /**
    * @param args
    * @throws InterruptedException
    * @throws IOException
    */
   public static void main(String[] args) throws IOException, InterruptedException
   {
      SudoExecutor se = new SudoExecutor();
      se.testExecuteCommand();
   }

   private void testExecuteCommand() throws IOException, InterruptedException
   {
      String cmd = "cat /etc/sudoers";
      //      SudoExecutor.run(cmd);  // should modify /etc/sudoers
      //      SudoExecutor.run(buildCommands(cmd));  // should modify /etc/sudoers
      SudoExecutor.run(buildCommands(cmd, "Abcd123456"));    // don't need modify /etc/sudoers
   }

}

 

转载于:https://my.oschina.net/u/2391658/blog/1057740

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值