从Java运行UNIX命令作为不同的用户 [英] Running UNIX commands as different user, from Java

查看:344
本文介绍了从Java运行UNIX命令作为不同的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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:

bob@host$ su jim && ls ~bob

问题是,我们收到jim密码的提示。因为这是从Java程序运行的,即

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

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

我们收到jim的密码提示并挂断。 我们知道jim的密码。但是,我无法输入它。

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

此外,我们不能使用Expect脚本(没有安装它),我们不能成为超级用户。我也研究过使用SSH来试试这个,因为我们可以在技术上做到这一点

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

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

但由于我没有设置无密码SSH的权限,因此也无效。

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

我最后的努力是尝试使用Java库的Java库,因为密码可供Java程序使用,我可以用它登录(并执行适当的命令)。但是因为我要在同一台主机上运行,​​所以看起来有点矫枉过正。

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.

有什么建议吗?

P.S:Java版本1.4.2,无法升级; AIX UNIX 5.3。

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

推荐答案

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

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!

这篇关于从Java运行UNIX命令作为不同的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆