0

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
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
cjg123
  • 473
  • 7
  • 23
  • Not using check_output is fine, as long as I am returned the output as text. – cjg123 Dec 18 '18 at 20:57
  • How on earth is this a duplicate of a question that makes no mention of python or subprocess? Please remove the duplicate tag. – cjg123 Dec 19 '18 at 00:26
  • Your double quotes were not properly delimited when it goes inside `'adb shell'`. You can use - `re.sub(r'\.','',''.join(re.findall(r'\..*\.',subprocess.check_output('adb shell service call iphonesubinfo 1', shell=True).decode())))` . This will return imei number. – Rilwan Dec 19 '18 at 10:17
  • @Rilwan This is close...It is not returning the actual IMEI of the device though...An 8 and a 9 is missing when using your method (8 is in the middle and the 9 is towards the end....so not endpoints). – cjg123 Dec 20 '18 at 16:03
  • can you give the output of `'adb shell service call iphonesubinfo 1'` ?. I had verified in my phone before pasting it here though. – Rilwan Dec 20 '18 at 16:29
  • I figured it out, thanks to your hints! will post solution – cjg123 Dec 20 '18 at 21:16
  • Guess I can't answer it formally. But to anyone interested: 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 – cjg123 Dec 20 '18 at 21:17

0 Answers0