How to send user input to terminal from java program (i.e by programmatically)?

后端 未结 1 888
独厮守ぢ
独厮守ぢ 2021-01-21 16:59

I am executing command from java program like

Process myProcess = Runtime.getRuntime().exec(\"sudo cat /etc/sudoers\"); //It asks for password so I send passwor         


        
相关标签:
1条回答
  • 2021-01-21 17:46

    You can do it using the -S option of sudo :

    String[] cmd = {"/bash/bin","-c","echo yourpassword| sudo -S your command"}; 
    Runtime.getRuntime.exec(cmd); 
    

    But I'm not sure it's recommendable.

    I'm not the author, I found this there : http://www.coderanch.com/t/517209/java/java/provide-password-prompt-through-Java

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