在Android上用Java执行挂载命令
我正在尝试编写执行一些终端命令的 Java 代码。代码应执行此命令sudo mount -o Loop system.img system
。但也存在几个问题。首先,要执行此命令,我必须是 root 身份。我知道我可以通过 sudo su
进行操作,但是当我关闭终端窗口时如何保持 root
身份?如果我使用命令sudo mount -o loop system.img system
我如何在Java代码中提供密码?
第二个问题是:我可以执行下面的命令吗?
File f2 = new File("/home/user1/Desktop/aDirectory");
String[] commands = new String[]{"sudo mount", "-o", "loop", "/home/user1/Desktop/aDirectory/system.img"};
Runtime.getRuntime().exec(commands, null, f2);
我想我不能。那么我该怎么做呢?有什么想法吗?
注释: system.img
是编译后的 Android 操作系统文件。 system
是一个空目录。我想做的是将 system.img
文件挂载到 system
目录中。
I'm trying to write Java code that executes some terminal commands. The code should execute this command sudo mount -o loop system.img system
. But there are several problems. First, to execute this command I have to be root
. I know that I can be by sudo su
, but how can I stay as root
when I close the terminal window? If I use the command sudo mount -o loop system.img system
how can I provide the password in the Java code?
The second issue is: can I execute the command as below?
File f2 = new File("/home/user1/Desktop/aDirectory");
String[] commands = new String[]{"sudo mount", "-o", "loop", "/home/user1/Desktop/aDirectory/system.img"};
Runtime.getRuntime().exec(commands, null, f2);
I think I can't. So how can I do it? Any ideas?
Notes: system.img
is a compiled Android os file. and the system
is an empty directory. The thing I'm trying to do is mount the system.img
file into the system
directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像
sudo
这样的程序直接从终端设备读取密码,而不是从stdin
读取密码,因此不幸的是,这不是一件小事。我不确定这对于 Android 是否现实,但在通用 UNIX 系统上,最简单的解决方案是使用expect
,这是一个用于模拟终端并从而自动化此类交互的库。它通常用作嵌入在Tcl
中的独立程序,并且我将 Java 启动的系统组合在一起expect
与sudo
等工具进行交互,而且效果很好。expect
包含一种声明性脚本语言,告诉它如何运行另一个程序以及如何对该程序的输出做出反应。您要做的就是使用 Runtime.exec() 执行expect 程序,提供一个仅运行“sudo mount”的脚本,监视对于密码提示,并提供密码。该脚本可能看起来像这样(G4rb4geB4rg3 是密码):
Programs like
sudo
read the password directly from the terminal device, not fromstdin
, so this is unfortunately not a trivial thing to do. I'm not sure if this is realistic for Android or not, but on a general UNIX system the easiest solution is to useexpect
, which is a library for simulating a terminal and thereby automating these kinds of interactions. It's often used as a standalone program embedded inTcl
, and I've thrown together systems in which Java launchedexpect
to talk to tools likesudo
, and it works fine.expect
includes a sort of declarative scripting language that tells it how to run another program and how to react to that program's output.What you would do is use
Runtime.exec()
to execute theexpect
program, supplying a script that just runs "sudo mount
", watches for the password prompt, and provides the password. The script would probably just look something like (G4rb4geB4rg3 is the password):通过使用shell脚本,问题得到解决。
我写了一个脚本,只包含这一行:
然后我在我的java代码中运行它,例如:
The problem was solved, by using shell script.
I wrote a script includes just this line :
then I run it in my java code, such :
我很确定“sudo”和“mount”是分开的,因为它不是您调用的单个可执行文件。另外,如果您使用 -S 命令行开关启动 sudo,它可以直接从 stdin 获取密码,因此您只需启动该进程并将配置为输入流的任何密码传递进去。
I'm pretty sure 'sudo' and 'mount' would be separate, since it's not a single executable you're invoking. Also, if you start sudo with the -S command line switch it can take the password directly from stdin, so you just need to start the process and pass in whatever the password's configured as to the input stream.