I need to determine my device's MAC (hardware) address, ie that for the wifi. I'm using Termux on an Android device, although maybe an answer will apply more generally.
I'm using python, but any bash would be okay too.
On some 2017 devices, the following used to work:
import subprocess
result = subprocess.run(['ip','link'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
rc,so, se = result.returncode, result.stdout, result.stderr
assert not rc
ipl = re.split('\n[^ ]+ ', so, re.MULTILINE)
wlanl = [L for L in ipl if L.startswith('wlan0')]
assert wlanl
HWadd = re.findall('link/ether (.*?) ',wlanl[0])[0].lower()
however, this is failing strangely (I cannot get re.split to work as I expect) on a new 2019 device.
Instead, I now have a nearly equally ungainly alternative that works:
import subprocess
result = subprocess.run(['ip','link'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
rc,so, se = result.returncode, result.stdout, result.stderr
assert not rc
ipl = re.findall('\n[0-9]+: wlan0:.*?\n +link/ether ([^\n]*?) .*?\n', so, re.MULTILINE+ re.DOTALL)
assert ipl
HWadd = ipl[0].lower()
Is there a more reliable way to extract this information from the OS somewhere?
By the way, using termux-wifi-connectioninfo
gives:
"mac_address": "02:00:00:00:00:00"