执行带有“sudo”命令的linux shell命令。使用java,无需输入所需的密码 [英] Execute a linux shell command with "sudo" using java, without entering the required password
问题描述
您好我使用ganymed-ssh2 java库在linux工作站执行远程命令成功。
但是现在有一种情况,我需要执行命令,但这需要我给一些密码...像:
sudo / usr / bin / some_process
输入您的密码:
我用这种方式实现远程cmd执行:
会话sess = conn.openSession();
sess.execCommand(uname -a&&&&&&&&&& who);
System.out.println(这是有关远程主机的一些信息:);
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while(true)
{
String line = br.readLine();
if(line == null)
break;
System.out.println(line);
}
System.out.println(ExitCode:+ sess.getExitStatus());
sess.close();
conn.close();
恐怕不可能执行需要此库的密码的命令。 p>
有人可以给我一个解决方案或替代方案来允许这个
Thx!
假设有一个 sess.getStdin()
,你应该能够管道sudo命令的密码。
Hi I'm using ganymed-ssh2 java library to execute remote commands in linux workstation with success.
But now there is a situation where I need to execute a command but this require that i put some password...like:
sudo /usr/bin/some_process
please enter your password:
I used to implement remote cmd execution in this way:
Session sess = conn.openSession();
sess.execCommand("uname -a && date && uptime && who");
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
conn.close();
I'm afraid that it's not possible to execute commands that require a password with this library.
Someone could give me a solution or alternative to allow this ???
Thx!
assuming there is a sess.getStdin()
, you should be able to pipe in a password to the sudo command.
这篇关于执行带有“sudo”命令的linux shell命令。使用java,无需输入所需的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!