通过 ADB 以 root 身份启动脚本

发布于 2024-12-25 13:45:18 字数 709 浏览 0 评论 0原文

我创建了一个脚本来挂载分区并在我的 Android 系统中执行一些操作。我将脚本保存为Android的/bin文件夹中的install.sh。

我想从 ADB 调用脚本,它本身是从 Windows 上的批处理文件调用的,但需要以 root 身份执行。

我尝试的第一个解决方案是使用调用脚本

adb shell "su -c sh /bin/script.sh"

,但它不起作用,因为它为我提供了 shell 访问权限(具有 root 权限),但没有执行任何操作。 我也尝试打电话

adb root "sh /bin/script.sh"

,但出现以下错误,

adbd cannot run as root in production builds

然后我尝试在脚本中编写

su -c "command"

所有需要根访问权限的命令,但我遇到了同样的问题。 当我运行该脚本时,我只获得一个 root shell,并且没有执行任何操作。

如果我手动使用第一个解决方案(例如,我调用 adb shell su,然后调用我的脚本),它就可以工作。然而,重点是使该过程自动化,以便可以从另一个脚本调用 adb shell。

您知道我如何实现这一目标吗?

谢谢 !

I have created a script to mount partitions and do some stuff in my Android system. I saved the script as install.sh in the /bin folder of Android.

I want to call the script from ADB, which is itself called from a batch file on Windows, but it needs to be executed as root.

The first solution I tried was to call the script using

adb shell "su -c sh /bin/script.sh"

but it does not work as it gives me a shell access (with root permissions), but nothing is executed.
I also tried to call

adb root "sh /bin/script.sh"

but I got the following error

adbd cannot run as root in production builds

I then tried to write

su -c "command"

for all the commands which need a root access in my script, but I have the same problem.
When I run the script I only obtain a root shell and nothing is executed.

If I use the first solution by hand (e.g. I call adb shell su, then my script), it works. However the whole point is to automate the process, so that adb shell can be called from another script.

Do you have any idea of how I could achieve this ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

攒一口袋星星 2025-01-01 13:45:18

这对我有用:

创建 myscript.bat 并将其放入其中(注意要在超级用户模式下执行的命令周围的单引号):

adb shell "su -c 'command1; command2; command3'"

然后从 DOS shell 运行 myscript.bat。

注意:在这种情况下,DOS 行继续符 (^) 似乎不起作用。换句话说,以下内容对我不起作用:

adb shell "su -c '^
command1; ^
command2; ^
command3'"

这会导致“语法错误:未终止的引号字符串”

This works for me:

Create myscript.bat and put into it (note the single quotes around the commands to be executed in superuser mode):

adb shell "su -c 'command1; command2; command3'"

then run myscript.bat from a DOS shell.

Note: it doesn't appear that the the DOS line continuation character (^) works in this situation. In other words, the following doesn't work for me:

adb shell "su -c '^
command1; ^
command2; ^
command3'"

This results in "Syntax error: Unterminated quoted string"

本王不退位尔等都是臣 2025-01-01 13:45:18

这有效:

adb shell echo command which needs root privileges \| su

如果您需要重定向:

adb shell echo 'echo anytext > /data/data/aforbiddenfolder/file' \| su

将本地文件“复制”到需要root权限的android路径(但本地文件不得包含'):

cat alocalfile | adb shell echo "echo '`cat`' > /data/data/aforbiddenfolder/file" \| su

如果您有更好的方式(即使对于没有 -csu 版本),我很感兴趣。

This works :

adb shell echo command which needs root privileges \| su

If you need redirection:

adb shell echo 'echo anytext > /data/data/aforbiddenfolder/file' \| su

For "copying" a local file to an android path needing root privileges (but alocalfile must not contain '):

cat alocalfile | adb shell echo "echo '`cat`' > /data/data/aforbiddenfolder/file" \| su

If you have a better way (even for su versions which don't have -c), I am interested.

无风消散 2025-01-01 13:45:18

这对我有用:

adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"

This works for me:

adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"
橪书 2025-01-01 13:45:18

我不确定我是否提供了解决方案或要求更好的解决方案。
我想以批处理模式运行大约 200 个命令发送到 adb
我遵循了这种方法

    adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "

,并将它们保存在批处理文件中。

该命令

adb shell "su -c 'command1; command2; command3'"

超过特定的最大大小, 将无法工作。没用

error: service name too long

I am not sure if I provided a solution or asked for a better one.
I wanted to run some 200 command in batch mode to be sent to adb
I followed this approach

    adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "

and I saved them in a batch file

This command

adb shell "su -c 'command1; command2; command3'"

will not work beyond a certain max size . It did not work

error: service name too long
兔姬 2025-01-01 13:45:18

但它不起作用,因为它为我提供了 shell 访问权限(具有 root 权限),但没有执行任何操作。

您如何知道您已获得 root 权限?我假设您正在尝试在设备上执行脚本?您的设备已经root了吗?

您可能需要通过 chmod 向该文件授予执行权限。

chmod ugo=rwx /bin/script.sh

but it does not work as it gives me a shell access (with root permissions), but nothing is executed.

How do you know that you are given root permissions? I assume you are attempting to execute the script on a device? Has your device been rooted?

You may need to give execute permissions via chmod to the file.

chmod ugo=rwx /bin/script.sh
三人与歌 2025-01-01 13:45:18

看来我使用的是一个非常简单的 su 版本,它不接受 -c 参数。
我复制了另一个有效的 su 。 AndyD 是完全正确的,所以我接受他的答案而不是我的:)

It appears that I was using a very simple version of su which did not accept the -c argument.
I copied another su which did work. AndyD is totally right though, so I am accepting his answer instead of mine :)

阪姬 2025-01-01 13:45:18

使用 Android API 32 模拟器的工作解决方案:

adb shell "su root 'command'"

由于某种原因,所有其他方法都不起作用(或返回错误)。

Working solution with Android API 32 emulator:

adb shell "su root 'command'"

For some reason all other methods do not work (or return errors).

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