I am getting error while executing the below command:
$ sudo adb shell ping `cat /data/my_address.pst`
Where my_adress.pst file contains the ip address.
But the same command works fine when executed from the shell.
$ sudo adb shell
$ ping `cat /data/my_address.pst`
How can i pass executable commands like this? please throw some light on this.
Thanks.
Escape the ` so it is not interpreted by the shell.
adb shell ping \`cat /data/my_address.pst\`
Your cat /data/my_address.pst command gets executed by local shell so you need to escape the backticks or single quote the whole command. Also you do not need sudo and use of $() is preferable over backticks:
adb shell 'ping $(cat /data/my_address.pst)'
Related
I am trying to access adb shell of an android device and pass multiple commands such as
adb root
adb shell
cd /data/local/tmp
export PATH =/data/local/tmp:$PATH
export PATH =/data/local/tmp/testcases:$PATH
sh ./runltp -p -l 1213reportipc.log -d /data/local/tmp/LTP13/tmp -f ipc1q
exit
While I am able to do this manually using adb shell, I am unable to do this via a Python script since as soon script execution encounters 'adb shell' command it spawns a new internal shell and rest of the commands are not executed.
Can anyone please help me with this.
Use a text editor such as sublime or something, write your full adb script using subprocess to send commands and run it while your laptop is connected to your phone and USB debugging is switched on.
import subprocess
subprocess.call("adb shell <your command here>", shell = True)
subprocess.call("adb shell tap 172 3241", shell = True) -- example
etc, just put your commands after the 'adb shell' and run the python script. This should type all your commands into the adb shell. You should even be able to enter commands such as
subprocess.call("adb kill-server", shell=True)
or subprocess.call("adb root", shell=True)
Hi I need to run things in the form of adb shell <command>
When I test everything out inside adb shell, it works because I was able to set some aliases in .bashrc.
However, when I do adb shell <command>, nothing works because .bashrc is not used when you run adb shell <command>, because it's in the non-interactive mode.
How can I work around this? Can I adb push some files to the filesystem so that the alias will be there when adb shell is run?
If your android device is rooted you can add your aliases for adb shell into the /system/etc/mkshrc file.
One way to do this is to issue several shell commands in a single ADB command. You can put them in a string, and separate them with semicolons, thus:
adb shell "alias foo bar ; export MY_VARIABLE=/path/to/somewhere ; my_executable "
The " are crucial here, make sure they are paired up correctly. You could run your .bashrc this way, thus:
adb shell "source /path/to/.bashrc ; my_executable"
You can write a bash script that sets the aliases and then executes your shell:
#!/usr/bin/bash
. $HOME/.bashrc
adb shell $#
Does anyone know how to run commands from adb shell and remain in shell session?
What I`m trying to achieve is to set aliases in adb shell.
I have tried the following without success
adb shell <<< "ls"
After executing this command indeed remain in shell, but cannot receive output of any further command.
I have also tried the following:
adb shell <<EOF
ls
EOF
with the same result.
When you run:
adb shell ls
You are running this command currently outside of ADB.
First, you need to enter ADB:
adb shell
Once you enter ADB shell, you can continue to see output and input more commands.
ls
help
To exit ADB, simply type "exit" or hit "Ctrl + C"
expect solution
This will run the command, and leave you in an ADB shell automatically.
adb-cmd
#!/usr/bin/env expect
spawn adb shell
expect "#"
send [ concat [ join $argv " " ] ]
send "\r"
interact
Usage:
adb-cmd 'cd /data/data; ls'
Tested in Ubuntu 16.04 host, Android O guest.
There was a similar question answered in the comments here.
In short, run the following from your terminal:
stty raw -echo ; ( echo "ls" && cat ) | adb shell ; stty sane
Note: without the stty magic, the command is piped to adb and tab complete etc. is not recognized.
//you can use a nodejs script as below:
//let input='shell script here'
let input='cd /data/local/tmp\nchmod +x a.out\n./a.out\n'
p=run("adb.exe",args:'shell',cb:rs=>lg(rs)})
p.stdout.on("data",rs=>{
lg(rs)
setInterval(_=>process.exit(),2000);
})
p.stdin.write(input)
//+++++++++++++++++++++++++
function lg(...args){
console.log(...args);
}
function run(cmd,args,cb){
if(args) cmd= cmd+" "+args;
return require('child_process').exec(cmd,(er,stdout,stderr)=>{
if(er) return cb(stdout||stderr,false);
stdout=stdout==''?true:stdout;
cb(stdout,true)
});
}
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
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 :)