Enter adb shell from python - android

I'm trying to execute command to adb shell from python
I'm using Windows OS
On CMD :
C:\Users\deounix>adb shell
shell#android:/ $ su root
su root
root#android:/ # reboot
What I'm doing is :
import os
os.popen("adb shell & su root & reboot")
But it isn't work
How can I do it?

import os
os.system("adb shell su -c reboot")

import os
os.system("c:/platform-tools/adb devices")
To run any command like adb devices on python execute the above command
This will work definitely. You just need to confirm the path where you have got the adb shell

Related

adb command in cmd file (windows)

I have command and she working fine in cmd in manual mode:
adb shell
su
echo 'AT+EGMR=1,7,"558378919678440"' > /dev/radio/pttycmd1
How do I run this command in a .bat file?
I tried such options, but they do not work:
adb shell su -с echo 'AT+EGMR=1,7,"558378919678440"' > /dev/radio/pttycmd1
adb shell "su -с 'echo 'AT+EGMR=1,7,"558378919678440"' > /dev/radio/pttycmd1'"
adb shell su "-с echo 'AT+EGMR=1,7,"558378919678440"' > /dev/radio/pttycmd1"
Please help.
Using bash you can run this command
adb shell su root sh -c $'"echo \'AT+EGMR=1,7,\\"558378919678440\\"\' > /dev/radio/pttycmd1"'
So, if you are running it from a batch file you may need to invoke bash first

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

How to type adb shell commands in one line?

I want to type multiple adb shell commands in one line, for example, i want to type adb shell and su and cd sys together. i tried to connect them with && and & bzw.adb shell && su && cd sys, but it seems like not work with adb commands, but works with windows commands. does anybody knows what is the problem?
Sovled by myself adb shell "su -c 'cd sys'"

ADB Screenshot: not found?

My phone runs rooted 2.3.6 Gingerbread. I write these lines to take a screenshot:
adb shell screencap -p /sdcard/screen.png
It says permission denied! So I add su, like this:
adb shell "su -c 'screencap -p /sdcard/screen.png'"
Now when I run it, it says "screenshot: not found!"
You could accomplish this with MonkeyRunner instead of ADB by creating a screenshot.py file with the following content:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection()
result = device.takeSnapshot()
result.writeToFile('<some path>/screenshot.png','png')
and run it with the command (on Windows)
<android sdk path>\tools\monkeyrunner.bat <some path>\screenshot.py

Running multiple adb commands with python Popen or os.system

One problem with ADB is that you need multiple commands to get things done.
For example:
adb shell
su
cp /data/local/x /data/local/y
exit
adb pull /data/local/y
Can this be done using python popen and os-system? Tried the example below without success..
print 'Starting emulator...'
subprocess.Popen(['emulator', '-avd', 'testavd'])
os.system('adb wait-for-device')
os.system('Perform whatever adb commands you need')
Any pointers?
You can simply do:
adb shell su -c cp /data/local/x /data/local/y
adb pull /data/local/y
or, if you want to run more than one command (only Linux & OSX):
adb shell <<EOF
ls
date
cat /proc/version
exit
EOF

Categories

Resources