bash passing arguments invalid (adb) - android

sorry for my English.
I write bash-file, that uses variable: shared_var=/system/xbin
My script-file:
exec="./adb shell chmod 644 $shared_var/$2"
echo $exec
$exec
Let's run this script:
>bash gapp.sh misc su
./adb shell chmod 644 /system/xbin/su
: No such file or directory n/su
Let's run string "./adb shell chmod 644 /system/xbin/su" without script:
> ./adb shell chmod 644 /system/xbin/su
(No output, OK)
I have few questions:
Why are script and direct input in terminal have different results?
Why instead of "No such file or directory /system/xbin/su" adb returns "No soch file or directoryn/su" (adb result looks distorted)
What is solution of my problem?

does it work, if your shell script only has
echo ./adb shell chmod 644 $shared_var/$2
./adb shell chmod 644 $shared_var/$2
ie, not assigning it to another variable, and no quoted strings
Also, if you are editing the file in Windows, make sure you save the file with Unix linefeeds.

Related

Android Run script in ADB shell

I am trying to fallow this tutorial in order to install SSL certificate on Android emulator.
I need to start the emulator from command line, so I run
emulator -avd myDevice -http-proxy myIp:8888
After device is started I want to copy my certificate file from PC to the device, so I run those commands
mount -o remount,rw /system
cp /sdcard/5ed36f99.0 /system/etc/security/cacerts/
cd /system/etc/security/cacerts/
chmod 644 5ed36f99.0
I bundle them all together using this suggestion
The final command looks like this:
adb shell su -c 'mount -o remount,rw /system; cp /sdcard/5ed36f99.0 /system/etc/security/cacerts/; cd /system/etc/security/cacerts/; chmod 644 5ed36f99.0'
But I am getting an error:
su: invalid uid/gid '-c'
If I do it from the shell it works, but then when I restart the emulator it restore the system to previous state without saving my changes.
How can I solve those two problems?

How to use su command over adb shell?

I need to make a script that executes a lots of thing on Android device, my device is rooted, when I enter on the shell, I can give the command su, and it works but I need pass this command like:
adb shell "
su;
mv /sdcard/Download/app_test /data/local;
cd /data/local;
./app_test;
exit;
exit;
"
when I put some commands before the su it works, according what I read su creates a new shell that return immediately, but I need give commands on the su shell, how can I do that?
Well, if your phone is rooted you can run commands with the su -c command.
Here is an example of a cat command on the build.prop file to get a phone's product information.
adb shell "su -c 'cat /system/build.prop |grep "product"'"
This invokes root permission and runs the command inside the ' '.
Notice the 5 end quotes, that is required that you close ALL your end quotes or you will get an error.
For clarification the format is like this.
adb shell "su -c '[your command goes here]'"
Make sure you enter the command EXACTLY the way that you normally would when running it in shell.
On my Linux, I see an error with
adb shell "su -c '[your command goes here]'"
su: invalid uid/gid '-c'
The solution is on Linux
adb shell su 0 '[your command goes here]'
The su command does not execute anything, it just raise your privileges.
Try adb shell su -c YOUR_COMMAND.
By default CM10 only allows root access from Apps not ADB. Go to Settings -> Developer options -> Root access, and change option to "Apps and ADB".
1. adb shell su
win cmd
C:\>adb shell id
uid=2000(shell) gid=2000(shell)
C:\>adb shell 'su -c id'
/system/bin/sh: su -c id: inaccessible or not found
C:\>adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
C:\>adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
win msys bash
msys2#bash:~$ adb shell 'su -c id'
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
2. adb shell -t
if want run am cmd, -t option maybe required:
C:\>adb shell su -c am stack list
cmd: Failure calling service activity: Failed transaction (2147483646)
C:\>adb shell -t su -c am stack list
Stack id=0 bounds=[0,0][1200,1920] displayId=0 userId=0
...
shell options:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable pty allocation
-t: allocate a pty if on a tty (-tt: force pty allocation)
-x: disable remote exit codes and stdout/stderr separation
Android Debug Bridge version 1.0.41
Version 30.0.5-6877874
for my use case, i wanted to grab the SHA1 hash from the magisk config file. the below worked for me.
adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""
I opt for the following:
adb shell su root <your command>
e.g., to access the external storage (sd card):
adb shell su root ls storage/0/emulated

Attempting to gain r/w access to android /system

Working on a root script for the Nexus 4 with the latest stock rom .img for google (occam) and I have the following code snippet:
./adb wait-for-device
echo "remounting system"
./adb shell "mount -o remount,rw /system"
./adb push su /system/bin/
echo "pushing super user"
./adb push Superuser.apk /system/app/
echo "pushing busybox"
./adb push busybox /system/xbin/
./adb shell "chmod 06755 /system/bin/su"
./adb shell "chmod 0644 /system/app/Superuser.apk"
./adb shell "chmod 04755 /system/xbin/busybox"
./adb shell "cd /system/xbin"
./adb shell "busybox --install /system/xbin/"
I keep getting the error
mount: Operation not permitted
failed to copy 'su' to '/system/bin//su': Read-only file system
pushing super user
failed to copy 'Superuser.apk' to '/system/app//Superuser.apk': Read-only file system
pushing busybox
failed to copy 'busybox' to '/system/xbin//busybox': Read-only file system
Unable to chmod /system/bin/su: No such file or directory
Unable to chmod /system/app/Superuser.apk: No such file or directory
Unable to chmod /system/xbin/busybox: No such file or directory
/system/bin/sh: busybox: not found
I've tried using multiple methods of obtaining r/w access, but nothing seems to be working. I have to automate this process due to the fact that other people will use the script so it needs to be automation friendly, but I just can't figure this out.
I've also tried the
#su
#mount
#mount | grep system
followed by inputting the partition with the system mount and changing it to r/w access, but that also hasn't worked.
Really frustrated at this point. Can anyone help?
It gives the error because you aren't root. The system partition is mounted read-only. You can try to push the binary to /data/local/tmp. Then you can make su executable and eventual run it. But it doesn't mean you can have root. To become root you need to push an exploit like psneuter to /data/local/tmp and run it. It crashes the shell and reopen a new one with root rights. Then you can remount the system-partition read-write and install su.
Try the commands below
adb shell "su -c mount -o remount,rw /system"
adb shell "su -c chmod 06755 /system/bin/su"
and so on.

Why can't I push sqlite into Android using adb command?

I have a rooted Nexus S and it doesn't have Sqlite installed so I googled it and found I need to use
this command:
adb push sqlite3 /sdcard/
However it gave me this error:
failed to copy 'sqlite3' to '/sdcard//sqlite3': Read-only file system
So that means the /system is read only. Then I searched it and found that I need to remount my /system folder as rw. I used this command in the adb shell:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock5 /system
I still cannot push Sqlite3 and it produces the same error as before in the command line window.
I then typed
root#android:/ # mount
But all I can see is the /system is mounted differently:
/dev/block/platform/s3c-sdhci.0/by-name/system /system ext4 rw,relatime,barrier=
1,data=ordered 0 0
How can I mount my system folder as "rw" and push the sqlite3 into my Android phone?
Maybe you can try another location. The script I used to root my phone was placing its stuff on the /data/local/ after creating a tmp folder:
adb shell "cd /data/local && mkdir tmp"
adb push sqlite3 /data/local/tmp/.
adb shell "chmod 755 /data/local/tmp/sqlite3"
adb shell "cp /data/local/tmp/sqlite3 /system/bin/sqlite3"
adb shell "cd /data/local/tmp && rm *"
In fact, the two answers are mandatory. If we set only
adb shell "cd /data/local && mkdir tmp"
adb push sqlite3 /data/local/tmp/.
adb shell "chmod 755 /data/local/tmp/sqlite3"
adb shell "cp /data/local/tmp/sqlite3 /system/bin/sqlite3"
adb shell "cd /data/local/tmp && rm *"
We have the same error
Read-only file system
So, before copy the sqlite3 to /system/bin/sqlite3, we must add the following line :
mount -o rw,remount -t yaffs2 /dev/block/mtdblock5 /system

Executing shell commands programmatically

I want to execute this shell commands by program. How can I do it?
cd C:\android-sdk\platform-tools
adb shell
su
mount -t rfs -o remount,rw /dev/block/stl9 /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk
We can execute shell comands by using Runtime class.
Runtime.getRuntime().exec("ls");
The above piece of code will create a native process for given command ls, will return same process as a Process object.
For more details about it Check here
You Should write the exact syntax you used here in a .bat file, and then just execute it.
It seems you are on a Microsoft station so considering using batch would give you this :
1st method : Stay on your station and send usefull commands
cd C:\android-sdk\platform-tools
adb shell "su -c 'mount -o rw,remount /system'"
adb shell "su -c 'cp /sdcard/MyApp.apk /system/app/MyApp.apk'"
adb shell "su -c 'mount -o ro,remount /system'"
The only thing is you will launch and close 3 shells but its not really and issue.
2nd method : Stay on your station send a sh script on sdcard and execute it
cd C:\android-sdk\platform-tools
adb push myscript.sh /sdcard/
adb shell "su -c 'sh /sdcard/myscript.sh'"
with "myscript.sh" containing :
#!/system/bin/sh
mount -o rw,remount /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk
mount -o ro,remount /system
Remember that Android shell scripts created on Microsoft station have CRLF line ending !
You need to get LF only ending your lines on UNIX like systems !

Categories

Resources