基于Linux wpa_supplicant wpa_cli 工具调试WiFi sta 网络连接

     在网上看了不少这方面的文章,可惜总是照着用行不通,自己玩了一下,写出来,我下面是基于Linux 非Android平台操作的。

必要的工具:wpa_supplicant  wpa_cli udhcpc

wpa_supplicant  -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf  -dd  -B
wpa_cli -iwlan0 

scan 能看到有回复;
scan_results 能看到搜到的AP热点, 之后可以配置一个没有密码的热点 AP_SSID

remove_network all 注意:如果前面没有设置连接过,该命令会返回FAIL,因为没有网络配置存在,继续往下执行即可
add_network
set_network 0 ssid "AP_SSID"
set_network 0 key_mgmt NONE
select_network 0 
这时候启动udhcpc -i wlan0 -s /etc/udhcpc.script -b 自动获取IP
ifconfig 命令查看ip以及用route 命令查看网关等正常,就用ping 即可测试网络是否正常

补充:

当连接加密热点时,按照后面如下执行即可:
add_network
set_network 0 ssid "AP_SSID2"
set_network 0 key_mgmt WPA-PSK  这个要根据实际情况来选
set_network 0 psk "admin1234"
select_network 0
这时候启动udhcpc -i wlan0 -s /etc/udhcpc.script -b 自动获取IP
ifconfig 命令查看ip以及用route 命令查看网关等正常,就用ping 即可测试网络是否正常

udhcpc.script 脚本如下

# udhcpc script edited by Tim Riker 

[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1

RESOLV_CONF="/etc/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

case "$1" in
	deconfig)
		/sbin/ifconfig $interface 0.0.0.0
		;;

	renew|bound)
		/sbin/ifconfig $interface $ip $BROADCAST $NETMASK

		if [ -n "$router" ] ; then
			echo "deleting routers"
			while route del default gw 0.0.0.0 dev $interface ; do
				:
			done

			for i in $router ; do
				route add default gw $i dev $interface 
			done
		fi

		echo -n > $RESOLV_CONF
		[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
		echo nameserver 114.114.114.114 >> $RESOLV_CONF
		for i in $dns ; do
			echo adding dns $i
			echo nameserver $i >> $RESOLV_CONF
		done
		;;	
esac

exit 0

 

你可能感兴趣的:(linux)