鉴于 Web 应用程序没有 su 权限,我想执行一个需要 sudo 的 shell 脚本。我还想避免让用户输入密码。我有哪些选择?这基本上就是我想要做的。
Wicket Application(更改IP的表格)
@Override
protected void onSubmit() {
System.out.println("Submitted");
try {
Shell.updateIp("eth0", "192.168.217.129");
} catch (IOException e) {
e.printStackTrace();
}
}
shell .java
public static void updateIp(String ethernetInterface, String ip) throws IOException {
ProcessBuilder builder = new ProcessBuilder("/home/keeboi/Desktop/iptool.sh", ethernetInterface, ip);
Process child = builder.start();
Network.readOutput(child);
child.destroy();
}
iptool.sh 执行可运行的 jar。
iptool.sh
#!/bin/sh
sudo java -jar changeip.jar $1 $2
我得到:
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
再次,我想强调的是,网络应用程序没有被授予任何 su 权限,我想避免要求用户输入密码。
编辑:
我已经尝试将 keeboi ALL = NOPASSWD:/home/keeboi/Desktop/iptool.sh
添加到我的 /etc/sudoers
但它仍然需要密码.
更新
也添加了 keeboi ALL = NOPASSWD:/home/keeboi/Desktop/changeip.jar
,没有骰子。
最佳答案
只需在/etc/sudoers 中添加 NOPASSWD
:
user ALL = NOPASSWD: /home/keeboi/Desktop/iptool.sh
这会关闭密码检查。
关于java - 从 Web 应用程序执行需要 sudo 的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336952/