使用WIFI网卡 wpa_supplicant

在上篇文章中,编译了应用程序iw,它使得我们的开发板可以通过usb wifi网卡连接到无线热点,为了方便实验,我们采用了手机设置了无线热点。
对手机的热点有4中安全方式:

WEP
WPA
WPA2
使用iw工具,只能连接到前两种安全模式。对于WPA、WPA2这两种安全模式,iw工具就无能为力了。
需要使用wpa_supplicant,wpa_supplicant可以用于上述4中"认证/加密"
下载源码:网址为wireless.kernel.org/en/users/Documentation/wpa_supplicant
点击 wpa_supplicant's home page,看到最新版本是wpa_supplicant-2.9.tar.gz。

但是这里我们选用wpa_supplicant-2.0.tar.gz

a. wpa_supplicant : 可用于上述4种"认证/加密"
a.1 先编译它的依赖libopenssl
tar xzf openssl-1.0.1d.tar.gz
cd openssl-1.0.1d/
./config shared no-asm --prefix=$PWD/tmp
修改Makefile:
CC= arm-linux-gcc
AR= arm-linux-ar $(ARFLAGS) r
RANLIB= arm-linux-ranlib
NM= arm-linux-nm
MAKEDEPPROG= arm-linux-gcc

make
make install
安装:
把编译出来的头文件应该放入:
/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include

把编译出来的库文件应该放入:
/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib

把编译出来的库文件应该放入NFS文件系统的lib目录:
/work/nfs_root/fs_mini_mdev_new/lib


a.2 要有一个配置文件.config
tar xzf wpa_supplicant-2.0.tar.gz
cd wpa_supplicant-2.0/wpa_supplicant/
cp defconfig .config
修改.config加上一行: CONFIG_LIBNL32=y
修改Makefile:
CC=arm-linux-gcc

make
make DESTDIR=$PWD/tmp install

使用:
mkdir -p /var/run/wpa_supplicant
① OPEN
wpa_supplicant -B -c/etc/plaintext.conf -iwlan0
wpa_cli -iwlan0 status // 查看状态
ifconfig wlan0 192.168.1.55
ping 192.168.1.1

配置文件内容为:
ctrl_interface=/var/run/wpa_supplicant
network={
  ssid="dswei"
  key_mgmt=NONE
}

② WEP/WEP
wpa_supplicant -B -c/etc/wep.conf -iwlan0
wpa_cli -iwlan0 status // 查看状态
ifconfig wlan0 192.168.1.55
ping 192.168.1.1

配置文件:
ctrl_interface=/var/run/wpa_supplicant
network={
  ssid="dswei"
  key_mgmt=NONE
  wep_key0="baiwenwang123"
  wep_tx_keyidx=0  //使用第0个密码
}

③WPA(TKIP)
wpa_supplicant -B -c/etc/wpa-psk-tkip.conf -iwlan0
wpa_cli -iwlan0 status // 查看状态
ifconfig wlan0 192.168.1.55
ping 192.168.1.1

配置文件
ctrl_interface=/var/run/wpa_supplicant # 一个目录,用于wpa_supplicant和wpa_cli的socket通信
network={
  ssid="dswei"
  proto=WPA # proto: list of accepted protocols, 可取WPA,RSN
        # If not set, this defaults to: WPA RSN
  key_mgmt=WPA-PSK # 认证方式
              # If not set, this defaults to: WPA-PSK WPA-EAP
  pairwise=TKIP # If not set, this defaults to: CCMP TKIP
  group=TKIP # If not set, this defaults to: CCMP TKIP WEP104 WEP40
  psk="baiwenwang123"
}

④ WPA2(AES)
wpa_supplicant -B -c/etc/wpa_wpa2.conf -iwlan0
wpa_cli -iwlan0 status // 查看状态
ifconfig wlan0 192.168.1.55
ping 192.168.1.1

配置文件:
ctrl_interface=/var/run/wpa_supplicant
network={
  ssid="dswei"
  psk="baiwenwang123"
}

另:
wpa_cli可工作于"命令模式"和"交互模式"

命令模式:wpa_cli -iwlan0 scan

交互模式:

wpa_cli
status
scan

⑤ 配置文件里设置多个network:
ctrl_interface=/var/run/wpa_supplicant
network={
  ssid="dswei"
  psk="baiwenwang123"
}
network={
  ssid="Programer"
  psk="baiwenwang"
}

它会根据顺序先连接dswei,如果连接不上,再去连接Programer

⑥ 访问外网:

例如:ping news.qq.com

ping: bad address 'news.qq.com'
需要将new.qq.com转换成ip,这里就涉及到了一个DNS,域名服务器

修改/etc/resolv.conf添加DNS:
nameserver 192.168.1.1

此时,再ping news.qq.com

PING news.qq.com(182.254.1.167):56 data bytes
ping: sendto: Network is unreachbale

有了外网ip了,但是无法到达。还需要干啥呢?
还需要加路由,设置网关。

设置网关:
route add default gw 192.168.1.1

注意:以上几种连接方式,在连接上了AP之后,我们还需要手工设置网卡的ip,能否让网卡自动获取ip呢?可以的。
下一篇博客讲解的dhcp就可以实现此功能。

posted @ 2019-09-29 22:17  一代枭雄  阅读(1751)  评论(0编辑  收藏  举报