I was trying to run Mobile originated call perl script on Emulator but getting error while running the script:
Below is the script:
Mo_call.pl
#!/usr/bin/perl -w
use strict;
use New_MO.pm;
for(my $i=0; $i<=4;$i++)
{
New_Mo::call_Originate();
}
New_MO.pm
package New_MO;
sub call_Originate
{
system("adb -s $device_id shell service call phone 763726728");
sleep 10;
system("adb -s $device_id shell input keyevent 4");
system("adb -s $device_id shell input keyevent 3");
}
1;
I am new for this things so if possible then please let me know where I am doing mistake.
Thanks
You should try using
use New_MO;
instead of
use New_MO.pm;
If this doesn't help, would you please share the error message you're getting?
Related
I try launch adb commands in python without custom modules.
try:
process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, shell=True)
process.stdin.write("adb shell uninstall com.q.q".encode("utf8"))
process.stdin.write("adb shell install C:\\...\\qwerty.apk".encode("utf8"))
but this not working. Code finish without results
cannot test with your exact commands but that works fine:
import subprocess
process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, shell=True)
o,e = process.communicate(b"dir\n")
print(o)
(I get the contents of my directory)
so for your example, you're missing the line terminators when sending commands. The commands aren't issued to the cmd program, the pipe is broken before that.
That would work better:
import subprocess
process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write(b"adb shell uninstall com.q.q\n")
process.stdin.write(b"adb shell install C:\\...\\qwerty.apk\n")
o,e = process.communicate()
but this is a very strange way to run commands. Just use check_call, with args split properly:
subprocess.check_call(["adb","shell","uninstall","com.q.q"])
subprocess.check_call(["adb","shell","install",r"C:\...\qwerty.apk"])
I am trying to connect two devices to my pc and run some commands on them using python and adb.
when I run the command from the command prompt, it goes through fine, but when i put those in the python script, they give me errors.
this is causing the errors all the time:
from subprocess import check_output, CalledProcessError
try:
adb_ouput = check_output(["adb","devices","-l","|", "grep", "\"model\""])
print adb_ouput
except CalledProcessError as e:
print e
The error message I get is this:
Usage: adb devices [-l]
Command '['adb', 'devices', '-l', '|', 'grep', '"model"']' returned non-zero exit status 1
When I try the same code without the grep command, it works
adb_ouput = check_output(["adb","devices","-l"])
It gives me the right output.
When I try the same in windows command prompt, It works fine (I am replacing grep with FINDSTR because i'm using it in windows, and I have tried doing the same in the python script as well, with 'shell = True' and also without.)
eg:
adb devices -l | FINDSTR "model"
This gives me an ouptut without any problems.
The output I get is
123ab6ef device product:xxxxxxxxx model:xxxxxxxxx device:xxxxxxxxx
bd00051a4 device product:yyyyyyyyyy model:yyyyyyyyyy device:yyyyyyyyy
I am trying to understand where I am going wrong here, but can't figure it out.
So far I have checked the docs: https://docs.python.org/3/library/subprocess.html
https://docs.python.org/3/library/subprocess.html#subprocess.CalledProcessError
These just give me the error codes.
I have also looked at these answers:
Python, adb and shell execution query
I took some error checking from here and added to my code.
Python subprocess.check_output(args) fails, while args executed via Windows command line work OK
python check_output fails with exit status 1 but Popen works for same command
I think i am close but just can't put my finger on it.
Any help would be appreciated.
First
adb_ouput = check_output(["adb","devices","-l","|", "grep", "\"model\""])
certainly requires shell=True, but even with that it's not equivalent to
adb devices -l | FINDSTR "model"
When using check_output, you're passing "model" as grep argument literally but you should pass just model. "model" is not in your output (with quotes) so grep fails to find it, and returns exitcode 1 which is not really an error for grep but makes check_output trigger an exception because it expects 0.
So I would to this as a quickfix:
adb_ouput = check_output(["adb","devices","-l","|", "grep", "model"],shell=True)
And as a longfix I'd perform the grep command directly with python.
adb_output = check_output(["adb","devices","-l"])
for l in adb_output.splitlines():
if "model" in l:
print(l)
I am trying to execute adb shell commands in python using subprocess.Popen
Example: Need to execute 'command' in adb shell. While executing manually, I open the command window and execute as below and it works.
>adb shell
#<command>
In Python I am using as below but the process is stuck and doesn't give output
subprocess.Popen('adb shell <command>)
Tried executing manually in command window, same result as python code,stuck and doesn't give output
>adb shell <command>
I am trying to execute a binary file in background(using binary file name followed by &) in the command.
Found a way to do it using communicate() method in subprocess module
procId = subprocess.Popen('adb shell', stdin = subprocess.PIPE)
procId.communicate('command1\ncommand2\nexit\n')
use pexpect (https://pexpect.readthedocs.io/en/stable/)
adb="/Users/lishaokai/Library/Android/sdk/platform-tools/adb"
import pexpect
import sys, os
child = pexpect.spawn(adb + " shell")
child.logfile_send = sys.stdout
while True:
index = child.expect(["$","#",pexpect.TIMEOUT])
print index
child.sendline("ls /storage/emulated/0/")
index = child.expect(["huoshan","google",pexpect.TIMEOUT])
print index, child.before, child.after
break
Ankur Kabra, try the code below:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
command = 'adb devices'
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print 'standard output: %s \n error output: %s \n',(stdout,stderr)
and you will see the error output.
Usually it will tell you:
/bin/sh: adb: command not found
which means, shell can not excute adb command.
so, adding adb to your PATH or writing the full path of adb will solve the problem.
May help.
I am writing a ruby script in which I want to execute the android logcat -v time command in a child process and after some time kill the process (when the the parent is done doing xyz).
For example I have:
childpid = Process.fork {
adb -s <device-serial> logcat -c
adb -s <device-serial> logcat -v time>/path-to-file/forkLog.log
}
sleep(30)
#parent do thing else here ...
Process.kill("SIGHUP", childpid) #kill the child process
According to what I've read the adb logcat code is executed in another child sup-process, so when I try to do a Process.kill the childpid stops but its sub-process does not.
how do I kill the logcat -v time>forklog.log process from the parent process??
Typically, when you're using fork to call a bash script, you'll want to use Kernel::exec() instead of Kernel::system() or Kernel::`
Process.fork{exec('adb -s <device-serial> logcat -c && adb -s <device-serial> logcat -v time>/path-to-file/forkLog.log'}
The difference is that exec will replace the Ruby process with the shell script, while the other two will create a subprocess.
I want to convert the AT-command 'AT$WCDMA?\r' to a String[]. Then I want to send it to the modem with
invokeOemRilRequestStrings(java.lang.String[] strings, Message response)
I tried:
String[] wcdma = {"A","T","$","W","C","D","M","A","?","\r"};
or
String wcdma2[] = new String[1];
wcdma2[0] = "AT$WCDMA?\r";
but both are not working.
When I am using the linux bash it works fine with the following commands:
$ adb shell
# su
# stop ril-daemon
# echo -e 'AT$WCDMA?\r' > /dev/smd0
Has anybody an idea, what I am doing wrong?
Cheers
Felix