在Android上用Java执行挂载命令

发布于 2024-12-29 18:24:22 字数 736 浏览 1 评论 0原文

我正在尝试编写执行一些终端命令的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娜些时光,永不杰束 2025-01-05 18:24:22

sudo 这样的程序直接从终端设备读取密码,而不是从 stdin 读取密码,因此不幸的是,这不是一件小事。我不确定这对于 Android 是否现实,但在通用 UNIX 系统上,最简单的解决方案是使用 expect,这是一个用于模拟终端并从而自动化此类交互的库。它通常用作嵌入在 Tcl 中的独立程序,并且我将 Java 启动的系统组合在一起 expectsudo 等工具进行交互,而且效果很好。

expect 包含一种声明性脚本语言,告诉它如何运行另一个程序以及如何对该程序的输出做出反应。

您要做的就是使用 Runtime.exec() 执行expect 程序,提供一个仅运行“sudo mount”的脚本,监视对于密码提示,并提供密码。该脚本可能看起来像这样(G4rb4geB4rg3 是密码):

spawn sudo mount -o loop /home/user1/Desktop/aDirectory/system.img
expect "password:"
send "G4rb4geB4rg3\r"
expect eof

Programs like sudo read the password directly from the terminal device, not from stdin, 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 use expect, which is a library for simulating a terminal and thereby automating these kinds of interactions. It's often used as a standalone program embedded in Tcl, and I've thrown together systems in which Java launched expect to talk to tools like sudo, 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 the expect 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):

spawn sudo mount -o loop /home/user1/Desktop/aDirectory/system.img
expect "password:"
send "G4rb4geB4rg3\r"
expect eof
忆离笙 2025-01-05 18:24:22

通过使用shell脚本,问题得到解决。

我写了一个脚本,只包含这一行:

echo myPassword | sudo -S mount -o loop system.img system

然后我在我的java代码中运行它,例如:

Runtime.getRuntime().exec("sh 1.sh");

The problem was solved, by using shell script.

I wrote a script includes just this line :

echo myPassword | sudo -S mount -o loop system.img system

then I run it in my java code, such :

Runtime.getRuntime().exec("sh 1.sh");
吖咩 2025-01-05 18:24:22

我很确定“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文