1

I'm currently working on a Raspberry Pi 3, trying to do some IoT stuff. I want it to be able to sniff on a usb wireless adapter, and send its data either via default wireless adapter or ethernet. I thought I could use udev, modifying /etc/udev/rules.d/70-persistent-net.rules. It seemed to me that I need to get the MAC address of the default wireless adapter. I got the MAC address using lshw:

root@raspberrypi:/etc/udev# lshw -class network   
*-usb:1             
   description: Wireless interface
   product: 802.11 n WLAN
   vendor: Ralink
   physical id: 4
   bus info: usb@1:1.4
   logical name: wlan1
   version: 1.01
   serial: 1.0
   capabilities: usb-2.00 ethernet physical wireless
   configuration: broadcast=yes driver=rt2800 usbdriverversion=4.4.13-v7+ firmware=0.29 link=no maxpower=450mA multicast=yes speed=480Mbit/s wireless=IEEE 802.11bgn   
*-network:0
   description: Wireless interface
   physical id: 2
   logical name: wlan0
   serial: b8:27:eb:e9:67:22
   capabilities: ethernet physical wireless
   configuration: broadcast=yes driver=brcmfmac driverversion=7.45.41.26 firmware=01-df77e4a7 ip=192.168.0.102 multicast=yes wireless=IEEE 802.11bgn   
*-network:1
   description: Ethernet interface
   physical id: 3
   logical name: eth0
   serial: b8:27:eb:bc:32:77
   size: 100Mbit/s
   capacity: 100Mbit/s
   capabilities: ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd autonegotiation
   configuration: autonegotiation=on broadcast=yes driver=smsc95xx driverversion=22-Aug-2005 duplex=full firmware=smsc95xx USB 2.0 Ethernet ip=10.42.0.11 link=yes multicast=yes port=MII speed=100Mbit/s

From this command, I can see the MAC address of my default wireless interface. Simply I'd put it in the rules and I'd be able to set the interface directly to wlan0. What I want is something more general: I don't want to do these setup in every device I set. I'll write a script and it'll get the wireless adapter's address for me.

root@raspberrypi:/etc/udev# sudo lshw -class network | grep b8:27:eb | sed 's/^.*serial: //' 
b8:27:eb:e9:67:22 
b8:27:eb:bc:32:77

I used lshw, grep'd the MAC address that belonged to Raspberry Pi Foundation (b8:27:eb:xx:xx:xx), then get something like "serial: , and used sed to simply leave the MAC.
Then I figured that the ethernet adapter had also MAC address with same header.

My question is, how can I get only the RPi wireless adapter's MAC address as an output of a command?

2
  • Wireless interfaces are not always named wlan[0..n] for instance I saw wlp[0..n]s0 – Emmanuel Aug 18 '16 at 13:14
  • Raspberries I worked with had wireless interfaces starting with wlan. I guess it has something to do with kernel namespaces but I couldn't see the namespaces it has. It's not what I'm interested in, though. – Hüseyin Bilge Yağcı Aug 18 '16 at 13:31
1

It will print the first wireless MAC (note that I'm not sure that these addresses are called "MAC addresses" in a wireless context)

lshw -class network  | awk '/usb:/ {ok=0} /network:/ {ok=1} /serial:/ {serial=$2} /capabilities: .*wireless/ && ok {print serial; exit}'
4
  • Executed with superuser, didn't get any result; now I'm trying to figure out why I didn't get any. Thanks for you effort! – Hüseyin Bilge Yağcı Aug 18 '16 at 13:37
  • @HüseyinBilgeYağcı I removed the "^" character in the pattern – Emmanuel Aug 18 '16 at 13:44
  • Wow, it actually worked, but in a weird way. When I sudo'd it, it gives me $2 of then usb adapter ( which is 1.0). It gives different results based on whether it's super-user'd or not. Since I'm a newbie in awk, I'm going to read some documentation and try to solve this situation. Nonetheless, your answer gives me what I asked. Thanks a lot! – Hüseyin Bilge Yağcı Aug 18 '16 at 14:31
  • @HüseyinBilgeYağcı I just corrected the bug, I added a flag to match which block usb or network is read – Emmanuel Aug 18 '16 at 14:44
0

If you know the name of the interface, you can query the MAC address with ifconfig

ifconfig wlan0 | grep HWaddr | cut -d ' ' -f 10-

ifconfig wlan0 will print information about only that interface.

grep HWaddr will keep only the line that has the MAC address

cut -d ' ' -f 10- will split that one line on spaces and give you what's after the 10th space until the end of the line.

1
  • I don't fully understand the opening sequence of the system, and I'd like to manipulate the interface-naming script before I logged in, also if possible before I named the interface wlan0. Therefore I won't be able to grep wlan0. Even though I can use ifconfig | grep b8:27:eb, I still can't differentiate between the ethernet and wireless adapters produced by same company. Thanks for your effort and have a nice day :) – Hüseyin Bilge Yağcı Aug 18 '16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.