V jednom skriptu jsem potreboval zjistit vsechny dostupne WiFi karty v pocitaci s Ubuntu 14.04. lspci se ukazalo nepouzitelne, musel jsem pracovat primo s /sys.
Lspci na mem notebooku vratilo nasledujici vypis:
martin@martin:~$ lspci 00:00.0 Host bridge: Intel Corporation 2nd Generation Core Processor Family DRAM Controller (rev 09) 00:01.0 PCI bridge: Intel Corporation Xeon E3-1200/2nd Generation Core Processor Family PCI Express Root Port (rev 09) 00:02.0 VGA compatible controller: Intel Corporation 2nd Generation Core Processor Family Integrated Graphics Controller (rev 09) 00:16.0 Communication controller: Intel Corporation 6 Series/C200 Series Chipset Family MEI Controller #1 (rev 04) 00:1a.0 USB controller: Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #2 (rev 05) 00:1b.0 Audio device: Intel Corporation 6 Series/C200 Series Chipset Family High Definition Audio Controller (rev 05) 00:1c.0 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 1 (rev b5) 00:1c.1 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 2 (rev b5) 00:1c.5 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 6 (rev b5) 00:1d.0 USB controller: Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #1 (rev 05) 00:1f.0 ISA bridge: Intel Corporation HM65 Express Chipset Family LPC Controller (rev 05) 00:1f.2 SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family 6 port SATA AHCI Controller (rev 05) 00:1f.3 SMBus: Intel Corporation 6 Series/C200 Series Chipset Family SMBus Controller (rev 05) 01:00.0 VGA compatible controller: NVIDIA Corporation GF119M [GeForce GT 520M] (rev ff) 03:00.0 Network controller: Qualcomm Atheros AR9285 Wireless Network Adapter (PCI-Express) (rev 01) 04:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 06) martin@martin:~$
Jak je videt, notebook ma dve sitove karty. Logicky je videt, ze AR9285 Wireless je WiFi. Ale na pritomnost slova wireless a v druhem radku ethernet se neda spolehat.
V Linuxu maji vsechny sitove karty „svuj adresar“ ve virtualnim souborovem systemu /sys/class/net/.
martin@martin:/$ ifconfig | grep "Link encap" eth0 Link encap:Ethernet HWadr .... lo Link encap:Místní smyčka wlan0 Link encap:Ethernet HWadr .... martin@martin:/$ martin@martin:/$ ls /sys/class/net/ eth0 lo wlan0 martin@martin:/$
Jak ale poznat bezdrat? Jednoduse. Pokud vlezeme do jednotlivych podadresaru a nenajdeme adresar wireless, neni to wifi:
martin@martin:/$ ls /sys/class/net/eth0 | grep wireless martin@martin:/$ ls /sys/class/net/wlan0 | grep wireless wireless martin@martin:/$ ls /sys/class/net/lo | grep wireless martin@martin:/$
Zbyva to jen obalit do skriptu:
#!/bin/bash for f in /sys/class/net/* do if [[ -d $f/wireless && $f != "/sys/class/net/mon."* ]]; then basename $f fi done
For cyklem se projdou vsechny polozky v adresari /sys/class/net. U kazde polozky se prida /wireless a testuje se, jestli je to adresar (-d) a zaroven jestli neexistuje monitor zarizeni.
Mohlo by se totiz stat, ze existuje sitovka wlan0 a k ni mon.wlan0. Mon.wlan0 nas vubec nezajima. Proto porovname retezec obsahujici aktualni cestu s vyrazem obsahujicim mon. Hvezdicka musi byt az za uvozovkami!
Nakonec pomoci basename zahodime priponu a cestu.
A takhle vypada spusteni skriptu:
martin@martin:~$ ls /sys/class/net/ eth0 lo wlan0 martin@martin:~$ /tmp/wifiDevice.sh wlan0 martin@martin:~$
Uvedeny zpusob je jenom jednou z mnoha cest, jak dosahnout vysledku 🙂