Executing shell commands programmatically - android

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 !

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 can I remount my Android/system as read-write in a bash script using adb?

For info
adb remount
returns "remount failed: Operation not permitted"
adb shell 'su -c mount -o rw,remount /system'
returns unknown option -- o
My device is rooted.
Probable cause that remount fails is you are not running adb as root.
Shell Script should be as follow.
# Script to mount Android Device as read/write.
# List the Devices.
adb devices;
# Run adb as root (Needs root access).
adb root;
# Since you're running as root su is not required
adb shell mount -o rw,remount /;
If this fails, you could try the below:
# List the Devices.
adb devices;
# Run adb as root
adb root;
adb remount;
adb shell su -c "mount -o rw,remount /";
To find which user you are:
$ adb shell whoami
I could not get the mount command to work without specifying the dev block to mount as /system
#cat /proc/mounts returns ( only the system line here )
/dev/stl12 /system rfs ro,relatime,vfat,log_off,check=no,gid/uid/rwx,iocharset=utf8 0 0
so my working command is
mount -o rw,remount -t rfs /dev/stl12 /system
Otherwise... if
getenforce
returns
Enforcing
Then maybe you should call
setenforce 0
mount -o rw,remount /system
setenforce 1
The following may help (study the impacts of disable-verity first):
adb root
adb disable-verity
adb reboot
I had the same problem and could not mount system as read/write. It would return
Usage: mount [-r] [-w] [-o options] [-t type]
device directory
Or
operation not permitted. Access denied
Now this works on all rooted devices.
DO THE FOLLOWING IN TERMINAL EMULATOR OR IN ADB SHELL
$ su
#mount - o rw,remount -t yaffs2 /system
Yaffs2 is the type of system partition. Replace it by the type of your system partition as obtained from executing the following
#cat /proc/mounts
Then check where /system is appearing from the lengthy result
Extract of mine was like
mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0/dev/block/platform/msm_sdcc.3/by-num/p10 /system ext4 ro,relatime,data=ordered 0 0
/dev/block/platform/msm_sdcc.3/by-num/p11 /cache ext4 rw,nosuid,nodev,relatime,data=ordered 0 0
So my system is ext4. And my command was
$ su
#mount -o rw,remount -t ext4 /system
Done.
Get "adbd insecure" from google play store, it helps give write access to custom roms that have it secured my the manufacturers.
In addition to all the other answers you received, I want to explain the unknown option -- o error: Your command was
$ adb shell 'su -c mount -o rw,remount /system'
which calls su through adb. You properly quoted the whole su command in order to pass it as one argument to adb shell. However, su -c <cmd> also needs you to quote the command with arguments it shall pass to the shell's -c option. (YMMV depending on su variants.) Therefore, you might want to try
$ adb shell 'su -c "mount -o rw,remount /system"'
(and potentially add the actual device listed in the output of mount | grep system before the /system arg – see the other answers.)
mount -o rw,remount $(mount | grep /dev/root | awk '{print $3}')
this does the job for me, and should work for any android version.

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.

bash passing arguments invalid (adb)

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.

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

Categories

Resources