Hi I am currently coding a random program at the moment, that effectively needs to see my MAC address.
This is what I have at the moment:
import subprocess
def get_mac():
mac_addr = subprocess.check_output(["ifconfig", "wlan0"])
Output from where i want the HWaddr extracted:
ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr 00:1b:11:1e:97:29
inet addr:10.1.1.6 Bcast:10.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::21b:11ff:fe1e:9729/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:91394 errors:0 dropped:0 overruns:0 frame:0
TX packets:58894 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:115550581 (115.5 MB) TX bytes:6097577 (6.0 MB)
So now I have the whereabouts of the MAC, but how would I extract it in that exact format? I looked around and couldn't fine anything other than URL extractions and such.
All I could find were re.search hich doesnt help me at all as I need to find a different MAC possible every time. Thanks
EDIT:
Sorry it's 5:30 and I am a bit tired. I realised I didnt even finish my question sorry. Not even the code :/ Edited now
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.