How to input Programatically in cmd - android

I am writing this code to bat to remove android pattern lock ..
adb shell
su
rm /data/system/gesture.key
In this case I have to write 'su' after 'adb shell', and 'rm /data/system/gesture.key' after su.
I have to input automatically 'su' and 'rm /data/system/gesture.key'.
I achieved to su by this command
echo 'su' | adb shell
But I can't add last command after 'su'. Please help me. Thanks in advance !

Related

Batch script for adb commands [duplicate]

This question already has answers here:
How to use su command over adb shell?
(7 answers)
Running ADB commands through powershell
(1 answer)
Closed 6 years ago.
i am writing batch script for executing adb commands but while executing "SU" commands only the first commands runs "adb shell" and stops
as the script is typing all the commands very fast and not waiting for previous command to run completely so that next command can be entered into the shell window that is "su" example of code is below
:lckscreen
adb shell
su
rm /data/system/gesture.key
rm /data/system/*.key
exit
exit
pause
goto menu4
i also tried
adb shell & su
but still same Result
shell#Samsung_s4:/ $
please help
Please try
adb shell "su -c 'rm /data/system/gesture.key'"
adb shell "su -c 'rm /data/system/*.key'"
and see the question in How to use su command over adb shell?
You will have to create a separate .txt file containing all the shell commands, in example:
su
mount -o remount,rw /system
mv /system/build.prop /sdcard
Make sure to have put an "enter" at the bottom to execute the last command. Now, in the bat file, where you'd normally put adb shell etc. Put adb shell > nameofyourtxt.txt.
If the only command you want to execute is "su", you can do adb shell su.

Making an ADB batch [ADB shell stopping issue]

This is my idea of what I want to do, simply execute the following commands but instead right after the line "adb shell" it stops, no other command can be triggered, whatever it is.
adb connect 192.168.1.101:5555
adb shell
su
rm /data/system/locksettings.db
rm /data/system/locksettings.db-wal
rm /data/system/locksettings.db-shm
PAUSE
reboot
adb shell command alone with out any extra parameters starts the shell in the interactive mode. Meaning it just sits there waiting for user input indefinitely. Your script never gets past that line.
What you really want is:
adb connect 192.168.1.101:5555
adb shell su 0 rm /data/system/locksettings.db
adb shell su 0 rm /data/system/locksettings.db-wal
adb shell su 0 rm /data/system/locksettings.db-shm
PAUSE
adb reboot

Running ADB commands through powershell

so I'm trying to run some ADB commands through a powershell script.
This is a simple example of what I am trying to do:
adb shell "
echo "in adb shell"
su root
echo "you are now root."
ls
cd /data/data
echo "in /data/data"
ls
"
I saw in a previous post to add the "" next to shell and at the bottom but that still didn't work for me. I can start the shell and the first ls works. but it just prints the rest of the commands out instead of doing them. The output I am getting looks like:
PS C:\Scripts> & .\test.ps1
: not found/sh:
in adb shell
su root
echo you are now root.
ls
cd /data/data
echo in /data/data
MSM8960_lpm.rc
acct
cache
.
.
.
ueventd.qcom.rc
ueventd.rc
vendor PS C:\Scripts>
Any help would be greatly appreciated. Thanks in advance!
Note: the path to ADB is within the powershell $env:path so the adb commands are working
I was able to get around this by doing:
adb shell "su -c '[cmd]; [cmd]' "

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

Launch a script as root through ADB

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 !
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"
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.
This works for me:
adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"
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
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
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 :)

Categories

Resources