以不同的用户身份从 Java 运行 UNIX 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1012792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:47:59  来源:igfitidea点击:

Running UNIX commands as different user, from Java

javaunixsshpasswordsaix

提问by Glen Balliet

Trying to write a Java program capable of running a UNIX command as a different UNIX user. I have the user's password, and I know the command I want to run, but the command has to be run as that user - so I have to login as that user first.

尝试编写一个能够以不同的 UNIX 用户身份运行 UNIX 命令的 Java 程序。我有用户的密码,我知道我想运行的命令,但命令必须以该用户身份运行 - 所以我必须先以该用户身份登录。

For example: say we have a user, jim, who wants to see what's in bob's home directory, and (for whatever reason) jim has access to execute ls whereas bob does not. We are currently logged in as bob. Here is what we (could) do:

例如:假设我们有一个用户 jim,他想查看 bob 的主目录中的内容,并且(无论出于何种原因)jim 有权执行 ls 而 bob 则没有。我们目前以 bob 身份登录。这是我们(可以)做的:

bob@host$ su jim && ls ~bob

Problem is, we get prompted for jim's password. Since this is run from a Java program, i.e.

问题是,我们被提示输入 jim 的密码。由于这是从 Java 程序运行的,即

Process p = Runtime.getRuntime().exec("su jim && ls ~bob");

we get prompted for jim's password and hung up. We know jim's password. However, I can't enter it.

我们被提示输入 jim 的密码并挂断了电话。我们知道吉姆的密码。但是,我无法输入它。

Additionally, we can't use an Expect script (don't have it installed) and we can't become the superuser. I also looked into using SSH to try this, since we could technically do

此外,我们不能使用 Expect 脚本(不要安装它),也不能成为超级用户。我还考虑使用 SSH 来尝试这个,因为我们在技术上可以做到

bob@host$ ssh jim@host "ls ~bob"

but this also doesn't work since I don't have permission to setup passwordless SSH.

但这也不起作用,因为我无权设置无密码 SSH。

My last-ditch effort is to try and use an SSH library for Java, since the password is available to the Java program and I would be able to login with that (and execute the proper command). But since I'm going to be running on the same host, it seems like overkill.

我最后的努力是尝试使用 Java 的 SSH 库,因为密码可用于 Java 程序,我将能够使用它登录(并执行正确的命令)。但由于我将在同一台主机上运行,​​这似乎有点矫枉过正。

Any suggestions?

有什么建议?

P.S: Java version 1.4.2, can't upgrade; AIX UNIX 5.3.

PS:Java 1.4.2版本,无法升级;AIX UNIX 5.3。

采纳答案by Glen Balliet

Problem solved. Used JSch (http://www.jcraft.com/jsch/) to SSH into the server with known username and password, and execute command. Thanks all for your suggestions!

问题解决了。使用 JSch ( http://www.jcraft.com/jsch/) 以已知的用户名和密码通过 SSH 连接到服务器,并执行命令。谢谢大家的建议!

回答by DevSolar

Have sudo installed, have the user running the Java program entered in /etc/sudoers for the commands in question, and use sudo -u jim ls ~bob.

安装 sudo,让运行 Java 程序的用户在 /etc/sudoers 中输入相关命令,然后使用sudo -u jim ls ~bob.

回答by lucas

Possibly a java implementation of Expect? ExpectJcomes up when googling but I couldn't find any documentation regarding running under 1.4.2.

可能是 Expect 的 Java 实现? 歌搜索时会出现ExpectJ,但我找不到任何关于在 1.4.2 下运行的文档。

回答by Stephan

Have you tried redirecting the sudo commands input and writing to that. I haven't used Java in a while but I believe there is a way to get the input stream and write to it. You could use that to write the password followed by a new line and sudo or su should accept the password.

您是否尝试过重定向 sudo 命令输入并写入该命令。我有一段时间没有使用 Java,但我相信有一种方法可以获取输入流并写入其中。您可以使用它来写密码,后跟一个新行,sudo 或 su 应该接受密码。

Use getInputStream() and write your password out to that.

使用 getInputStream() 并将您的密码写出来。

su jim -c ls ~Bob

回答by Adam Paynter

Perhaps this would work:

也许这会奏效:

Process process = Runtime.getRuntime().exec("su jim && ls ~bob");

OutputStream standardInput = process.getOutputStream();
Writer standardInputWriter = new OutputStreamWriter(standardInput);
standardInputWriter.write("password\n");
standardInputWriter.close();

回答by dfa

I'm not sure this code:

我不确定这段代码:

Process p = Runtime.getRuntime().exec("su jim && ls ~bob");

will be executed in a shell, needed to evaluate the &&, that is a shell command (/bin/sh). You should pass the command "ls ~bob" via a command line swith of su. Something like:

将在 shell 中执行,需要对 && 求值,即 shell 命令 (/bin/sh)。您应该通过 su 的命令行开关传递命令“ls ~bob”。就像是:

su jim -c 'ls ~bob'