1,打开网卡,不知道为啥,我这个把wifi插上ifconfig查看就自动有了,如果没有的话可以使用下面命令查看所有网卡
ifconfig -a
# ifconfig -a
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
wlan0 Link encap:Ethernet HWaddr 0C:C6:55:1E:27:FF
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
#
下面这个wlan0就是我的wifi网卡
我们可以使用下面命令打开网卡
ifconfig wlan0 up
//根据配置文件/etc/wpa_supplicant.conf来初始化wlan0 //如果wpa_supplicant.conf文件为空,则不会自动连接,需要连接一次网络才行
wpa_supplicant -Dnl80211 -i wlan0 -c /etc/wpa_supplicant.conf -B
2,现在就可以通过命令搜索wifi网络了
wpa_cli -i wlan0 scan //扫描附近热点
wpa_cli -i wlan0 scan_result //打印热点信息
iwlist wlan0 scanning //使用iwlist工具扫描附近的wifi热点
# wpa_cli -i wlan0 scan_result
bssid / frequency / signal level / flags / ssid
20:76:93:4c:0c:86 2467 -71 [WPA2-PSK-CCMP][ESS] wifi-YF
20:76:93:46:69:22 2472 -86 [WPA2-PSK-CCMP][ESS] wifi_xs
ca:50:e9:28:e8:e6 2412 -90 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] ChinaNet-CWiI
98:f4:28:4c:74:53 2452 -92 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] ChinaNet-zfEV
3,设置wifi参数
wpa_passphrase SSID PWD > xxx.conf
4,连接wifi
wpa_supplicant -i wlan0 -c ./xxx.conf & iwconfig wlan0
# wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf & iwconfig wlan0
wlan0 IEEE 802.11 ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=12 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:on
# Successfully initialized wpa_supplicant
wlan0: Trying to associate with 20:76:93:4c:0c:86 (SSID='wifi-YF' freq=2467 MHz)
wlan0: Associated with 20:76:93:4c:0c:86
wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
wlan0: WPA: Key negotiation completed with 20:76:93:4c:0c:86 [PTK=CCMP GTK=CCMP]
wlan0: CTRL-EVENT-CONNECTED - Connection to 20:76:93:4c:0c:86 completed [id=0 id_str=]
成功连接wifi
5,使用ifconfig查看网络
# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:520 (520.0 B) TX bytes:520 (520.0 B)
wlan0 Link encap:Ethernet HWaddr 0C:C6:55:1E:27:FF
inet addr:169.254.147.92 Bcast:169.254.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11 errors:0 dropped:6 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1094 (1.0 KiB) TX bytes:1842 (1.7 KiB)
#
看,到这里已经获取到ip地址了
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss
# ping 192.168.123.1
PING 192.168.123.1 (192.168.123.1): 56 data bytes
64 bytes from 192.168.123.1: seq=2 ttl=64 time=184.081 ms
64 bytes from 192.168.123.1: seq=5 ttl=64 time=54.066 ms
64 bytes from 192.168.123.1: seq=6 ttl=64 time=196.282 ms
64 bytes from 192.168.123.1: seq=8 ttl=64 time=28.138 ms
也可以ping通路由器了。