In my Appium Python script, this is an example of how I typically do ADB calls that I need the output of:
target_device = str(subprocess.check_output(["adb", "-s", device1, "shell", "getprop | grep ro.product.model"]))
This works fine, since the syntax is pretty simple. However, I have come across the following adb command, that will provide IMEI of the device, and that is pretty cool. Here it is:
adb shell service call iphonesubinfo 1 | awk -F"'" 'NR>1 { gsub(/\./,"",$2); imei=imei $2 } END {print imei}'
However, when I try and run that adb command from inside my Python Appium script, as you can imagine, all of the quotes can get very confusing, and in the end I am unable to run it successfully. However, it does work just fine when run from a command prompt (I've tested it).
So my question is, how can I run that IMEI command using subprocess? I have tried several manipulations, but nothing has worked!!! Thank you
e.g
target_device_imei = str(subprocess.check_output(["adb", "-s", device1, "shell", "service call iphonesubinfo 1 | awk -F"'" 'NR>1 { gsub(/\./,"",$2); imei=imei $2 } END {print imei}']))
Figured out solution:
import subprocess, re
device_imei = str(subprocess.check_output(["adb","shell", "service call iphonesubinfo 1"]))
device_imei = re.findall(r"'(.*?)(?<!\\)'", device_imei)
device_imei = "".join(device_imei)
device_imei = device_imei.replace('.','')
print device_imei