-1

I am trying to call the public android method getSSID() from a bash script? My objective is to return the currently connected SSID on the android device to a bash variable.

requirements:

  • must run as non-root user
  • must use only android baked in functions.
  • AKA requires no additional app installs
  • must run directly from a simple bash script.

Previously: I had been using the dumpsys command with grep to retrieve what I need but now it seems I need to be root for this to work, so I'm looking for a replacement for this concept.

 $ dumpsys wifi | grep -i $grepwifi

So I found this public api...

https://developer.android.com/reference/android/net/wifi/WifiInfo.html

But I am not able to figure out how to get it to work. I have tried a couple options such as listed below without any luck.

   $ am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiInfo.getSSID()
/system/bin/sh: syntax error: '(' unexpected

Or

$ am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiInfo.getSSID
Starting: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.wifi.WifiInfo.getSSID }
Error type 3
Error: Activity class {com.android.settings/com.android.settings.wifi.WifiInfo.getSSID} does not exist.

I'm assuming that I am simply not putting the url together correctly. So I hoping that someone can point me to documentation on how these commands can be structured so that once I get past the ssid chalange I can continue to explore other public api's but if there is another easy way to retrieve wifi information from the device I would be grateful for any alternatives that work.

Steve
  • 1
  • 2
  • `am start` accepts an Intent on the command line, not method names as you are trying. – Diego Torres Milano Nov 06 '16 at 23:17
  • Thanks for the response. Here is an example of on that I have been able to get to work. adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings – Steve Nov 08 '16 at 19:50
  • But it's not a method, it's a component (pkg+activity) – Diego Torres Milano Nov 08 '16 at 19:53
  • Thanks for the response. I understand the AM documentation [https://developer.android.com/reference/android/app/ActivityManager.html] here to include a list of example public methods that could be called. – Steve Nov 08 '16 at 19:56
  • Can you point to where in the `am` (lowercase, command) documentation is specified that you can invoke methods? – Diego Torres Milano Nov 08 '16 at 20:40
  • Sorry for the bad link I just posted. I"ll try to edit that so it works. Here it is again, and the "public methods" section is the 3rd from the top of the page. [https://developer.android.com/reference/android/app/ActivityManager.html Although it is entirely possible that in my search for good Activity Manager documentation I may have confused the tool with the class by the same name. I thought they were one and the same, but it's sounding like that is not the case. – Steve Nov 09 '16 at 01:04
  • Assuming you can confirm that was indeed where I made my mistake, "class" having little or nothing to do with the "tool" by the same name, then is there any hope for what I want to do? Is there an activity that prints to std-out or a variable, info such as ssid or other things like dumpsys does? Or should I ask that as a new questions? – Steve Nov 09 '16 at 19:48
  • Doe anyone else have any suggestions. So far I have discovered by browsing files on the android that I can collect the wifi interface name into a variable with getprop. I can get the IP address into a variable. I can even get the cell carrier name into a variable but I just can't figure out where the ssid is hiding. if anyone can help, throw me a clue. 'winterfacename="$(getprop wifi.interface)"' – Steve Nov 11 '16 at 06:17

1 Answers1

0

First of all, which is the device and android version that does not allow you to use dumpsys?

Anyway, I will show you a way of doing it that does not require dumpsys using AndroidViewClient/culebra.

You can use culebra to generate the script, and then slightly modify it to your needs. Open Quick Settings, then run

culebra -r -d true -t false -i false -o ssid.py

Remove what's not needed, add a regexp to match any SSID (not just the one you are seeing) and you should have something like this

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2016  Diego Torres Milano
Created on 2016-11-09 by Culebra v12.4.0
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os

from com.dtmilano.android.viewclient import ViewClient

TAG = 'CULEBRA'

_s = 5
_v = '--verbose' in sys.argv


kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
vc = ViewClient(device, serialno, **kwargs2)


vc.uiDevice.openQuickSettings()
vc.sleep(_s)

vc.dump(window='-1')
print vc.findViewWithContentDescriptionOrRaise(re.compile(u'''Connected to .*''')).getContentDescription()

Or if you prefer you can use Culebra GUI and do these through the UI.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I have seen the same behavior with regard to non-root use of dumpsys on pretty much every device I've tested on. Today just to make sure I'm not crazy I tried 3 different phones. Android 2.3.7, 4.1.2, and 6.0.1. All 3 get "permission denial" on the command unless run as root. '$ dumpsys wifi' Does this strike anyone as unusual? I was assuming that was pretty much typical. Thank you for the suggested solution, but there are several reasons why this won't work for me. python on the android being at the top of the list. none of my 3 test phones can even run a python script by default. – Steve Nov 10 '16 at 17:43
  • Is there a more direct way that you know of? – Steve Nov 10 '16 at 17:43
  • python is run on your computer, not your phone. That's perhaps the reason why `dumpsys` doesn't work for you, `adb` is needed. – Diego Torres Milano Nov 10 '16 at 17:46
  • So to be clear your script is intended to extract the wifi ssid of a phone that is connected via adb to the computer running the scrip? - No wonder we keep mis-communicating. :) my bash script is not intended to run via adb, but rather directly on the android device itself. Now it makes sense. yes dumpsys works just fine with non-root access on all devices via adb shell from my laptop. it is when it is run directly on the device from a script that it seems to require elevated permissions. - does that open any options for me now that we have that cleared up? – Steve Nov 10 '16 at 21:49
  • Then why you said `bash`? I bet you don't have `bash` on android. – Diego Torres Milano Nov 10 '16 at 22:13
  • Dear Lord. I thought this was a place to come and ask for assistance. My bad. I'm not really interested in debating the nuances that separate BASH from BASH-like impostors. It's not like you have made any bash recommendations that are choking on android BASH. So why bring it up? – Steve Nov 11 '16 at 06:12