If you're on linux, you may try this to get the MAC address:
iface = 'wlan0'
mac_addr = open('/sys/class/net/%s/address' % iface).read().rstrip()
For general string extraction, you may use the re
module:
import subprocess, re
RE_MAC = re.compile(r'\bHWaddr\s+(((?(2):|)[\dA-Fa-f]{2}){6})\b')
match = RE_MAC.search(subprocess.check_output(["ifconfig", "wlan0"]))
if match:
mac_addr = match.group(1)
Note that my version of ifconfig (net-tools 1.60) uses ether
rather than HWaddr
, illustrating one problem of parsing the output of such programs.