Running command after sudo login

前端 未结 1 1259
星月不相逢
星月不相逢 2020-12-06 22:43

I\'m trying to execute below code to execute sudo commands but I do not know how execute commands after sudo login

String[] command         


        
相关标签:
1条回答
  • 2020-12-06 23:23

    The sudo su executes a new shell.

    To provide a command to the shell you either:

    • specify the command on su command-line, like the official JSch Sudo.java example shows:

      ((ChannelExec)channel).setCommand("sudo -S -p '' "+command);
      
    • or feed the command to the shell using its standard input, i.e. the same way you provide the password:

      out.write(("command\n").getBytes());
      

      See also Executing multiple bash commands using a Java JSch program after sudo login.

    In general, I recommend the first approach as it uses a better defined API (command-line argument).

    0 讨论(0)
提交回复
热议问题