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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
martin@martin:~$ <strong>lspci</strong> 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) <strong>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)</strong> 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/.
1 2 3 4 5 6 7 8 |
martin@martin:/$ <strong>ifconfig | grep "Link encap"</strong> <strong>eth0</strong> Link encap:Ethernet HWadr .... <strong>lo</strong> Link encap:Místní smyčka <strong>wlan0</strong> Link encap:Ethernet HWadr .... martin@martin:/$ martin@martin:/$ <strong>ls /sys/class/net/</strong> <strong>eth0 lo wlan0</strong> martin@martin:/$ |
Jak ale poznat bezdrat? Jednoduse. Pokud vlezeme do jednotlivych podadresaru a nenajdeme adresar wireless, neni to wifi:
1 2 3 4 5 |
martin@martin:/$ <strong>ls /sys/class/net/eth0 | grep wireless</strong> martin@martin:/$ <strong>ls /sys/class/net/wlan0 | grep wireless</strong> <strong>wireless</strong> martin@martin:/$ <strong>ls /sys/class/net/lo | grep wireless</strong> martin@martin:/$ |
Zbyva to jen obalit do skriptu:
1 2 3 4 5 6 7 |
#!/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:
1 2 3 4 5 |
martin@martin:~$ <strong>ls /sys/class/net/</strong> <strong>eth0 lo wlan0</strong> martin@martin:~$ <strong>/tmp/wifiDevice.sh </strong> <strong>wlan0</strong> martin@martin:~$ |
Uvedeny zpusob je jenom jednou z mnoha cest, jak dosahnout vysledku 🙂