I send adb shell dumpsys wifi to get current available wifi ap BSSID(MAC address).The result as follows:
Latest scan results:
BSSID Frequency RSSI Age SSID Flags
7c:7d:3d:c3:4c:e0 2422 -40 6.716 HUAWEI-YJDAD5 [WPA2-PSK-CCMP][ESS]
d4:ee:07:26:24:18 2432 -50 6.716 HiWiFi_Refine [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]
24:09:95:55:54:20 2442 -52 6.716 HUAWEI-5420 [WPA2-PSK-CCMP][WPS][ESS]
70:72:3c:97:52:b8 2437 -53 6.716 HUAWEI-H6FCXT [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]
0c:d6:bd:3d:f6:14 2417 -52 6.716 HUAWEI-DUS8FG [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]
f0:b4:29:20:21:1b 2442 -54 6.716 Xiaomi_211A11 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]
80:38:bc:05:ed:a1 2412 -58 6.716 Huawei-Employee [WPA2-EAP-CCMP][ESS]
e0:19:1d:cc:7c:a4 2412 -57 6.715 HUAWEI-B83GL6 [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS][ESS]
18:c5:8a:17:4b:a1 2412 -60 6.715 Huawei-Employee [WPA2-EAP-CCMP][ESS]
So how to use findstr Regular Expression on Windows to collect BSSID from the result? I tried adb shell dumpsys wifi | findstr /r "[0-9a-f]{2}(:[0-9a-f]{2}){5}" but get nothing
Regular expression support by findstr is limited and does not support all features of regular expression implementation in Perl interpreter, or the regular expression class in Boost library, or what JavaScript RegExp object supports. They all support regular expressions in Perl syntax, but their implementations and features are different. Run in a command prompt window findstr /? to get displayed help of this console application and which regular expressions are supported by findstr.
But findstr is designed to output the line containing the found string and not just the found string. It would not make much sense for a non regular expression search in a file to output just the found string as it would be equal the search string, perhaps with the exception of case in case of using option /I and a search string containing letters.
Therefore I suggest to use command FOR to get just the MAC address written into a text file.
#echo off
rem Delete a perhaps already existing output file.
if exist MacAddress.txt del MacAddress.txt
rem Run command to get WiFi data, skip the first line of output, and
rem write to output file just the first data column with the MAC addresses.
for /F "skip=1" %%I in ('adb.exe shell dumpsys wifi') do echo %%I>>MacAddress.txt
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
for /?
if /?
See also Microsoft article Using command redirection operators for an explanation of >>.
Related
Using adb shell to run commands on an android device, I get different results when running ls with or without a wildcard ( globbing, i.e * ).
When running ls without a wildcard, the last path is displayed properly. When running ls with a wildcard, the path is displayed with an : in the end of it for some reason. The actual file does not have a : in its path.
My issue is specifically with the last file: /data/data/com.kauf.wrapmyFaceFunphotoeditor/files/DV-com.com.kauf.wrapmyFaceFunphotoeditor-2020-05-17-17-44-30-DEBUG.txt:
it has an : in the end which isn't supposed to be there
Why does using a wildcard in ls add characters to the result path?
Edit, environment details: Windows 10 / Android 7, the code is running on sh. I've ran adb shell to get to this command prompt, and doing it in one line (i.e adb shell su -c ls ...) returns similar results, same for adb shell command ...; also clarified the question.
As described in Why you shouldn't parse the output of ls, ls's behavior is not always well-defined. It's generally safer to use NULs (if you don't have any control or knowledge of filenames) or newlines (if you have reason to be certain that filenames can't contain them) to directly delimit a list of values emitted by the shell. Consider, then:
# output is separated by NULs, which cannot possibly exist in filenames
printf '%s\0' /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
...or...
# output is separated by newlines; beware of a file named DV-evil<newline>something-else
printf '%s\n' /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
Note that if you're passing this through extra unescaping layers, it may be necessary to double up your backslashes -- if you see literal 0s or ns separating filenames in your output, that's evidence of same.
Note also that if no matching files exist, a glob will expand to itself, so you can get an output that contains only the literal string /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*; in bash this can be suppressed with shopt -s nullglob, but with /bin/sh (particularly the minimal busybox versions more likely to be available on Android) this may not be available. One way to work around this is with code similar to the following:
# set list of files into $1, $2, etc
set -- /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
# exit immediately if $1 does not exist
if [ "$#" -le 1 ] && [ ! -e "$1" ]; then
exit
fi
# otherwise, print the list in our desired format
printf '%s\0' "$#"
I wonder if there is any way to apply a regular expression on to tag names while filtering adb logcat logs. I can use grep pipe but it applies on the whole text rather only tag names. I want to apply regex in tag names only. For example,
$ adb logcat -v tag | grep MyData*
produces,
D/MyActivity: calling MyDataManager
D/MyDataManager: data1 inserted
D/MyDataWorker: data1 inserted
But my expected output is,
D/MyDataManager: data1 inserted
D/MyDataWorker: data1 inserted
How to do that?
There is no inbuilt support for grep with regexp on tag from adb logcat . But Below worked for me to match all logs which has tag starts with Activ.
adb shell logcat | grep -P "^\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+Activ"
-P for perl like regexp support.
Make pattern with \s whitespace and \S non whitespace chars until you reach the tag.
For Example in below log, it starts with non whitespace '07-08' which will match with first \S+ in pattern. similarly for rest of the pattern.
07-08 17:13:00.042 1677 1708 D ActivityManager: cleanUpApplicationRecord -- 475
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'm writing a shell script for android and i need to get the last created directory, i would usually use
ls -t | head -1 but ls -t gives me the error "ls: Unknown option '-t'"
is there another shell command that can order the files by time-stamp or another way to do that in android? the busy box is more limited
It looks like stat is available in BusyBox, which means that you could do something like this:
stat -c '%Y %n' */ | sort -nr | cut -d' ' -f2-
This passes the names of all directories (paths ending in a slash) to stat, which prints the last modification time (seconds since the UNIX epoch) and the filename. These are sorted in reverse numerical order and then the time field is stripped from each line.
This assumes that your directory names don't contain newlines, otherwise the sorting would be messed up.
I am seeking a way to measure launch times of general apps in Android, and happen to know that
adb logcat -b events | grep am_activity_launch_time
might be one answer.
But I failed to find out what duration the command above shows (i.e., from what event to what event). Any help?
You can use this command
cmd0=add logcat -c
to clear the logs and then execute the below command
cmd1 = "grep am_activity_launch_time Example.txt | grep com.example.example| cut -d ',' -f4-4 > Example.csv"
The values are then saved in a csv file.
Then you can the python commands
os.system(cmd0);
os.system(cmd2);
Then use matplotlib to plot the graph from the generated csv files.
axes=plt.gca()
axes.set_xlim([1,20])
axes.set_ylim([0,4000])
plt.title("Performance Analysis ")
plt.ylabel('Time in milliseconds')
plt.xlabel('Number of itirations (DEVICE:Samsung Galaxy Note 4)')
data1=np.genfromtxt("Example.csv)"
plt.plot(data1,label="Example",linewidth=2.0,color='m')
plt.legend(bbox_to_anchor=(1,1),loc=1,borderaxespad=0.)
plt.show()
Once this is done execute this file in the python cmd.
adb logcat -b all | egrep -n --color 'Displayed *'
12461:11-26 22:43:05.316 1110 1167 I ActivityTaskManager: Displayed mobi.goldendict.android.free/mobi.goldendict.android.GoldenDict: +1s494ms
You could measure app launches time in android by this.