hostapd

hostapd用于开启wifi ap,执行后会创建/var/run/hostpad文件夹,hostapd_cli是用户操作界面。

1
2
3
$ hostapd /etc/hostapd.conf &
$ hostapd_cli -i wlan0 enable
$ hostapd_cli -i wlan0 disable

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
#
# Start the Hotspot....
#

HOSTAPD_CONF=/etc/hostapd.conf
HOSTAPD_CONF_DEFAULT=/rom${HOSTAPD_CONF}

case "$1" in
start)
printf "Starting Hotspot: "

# start hostpad if /var/run/hostapd not exsit
if ! [ -e /var/run/hostapd ]; then
hostapd $HOSTAPD_CONF > /dev/null &
timeout=0
while ! [ -e /var/run/hostapd ]; do
usleep 100000
let timeout++
if [ $timeout -gt 10 ]; then
echo "FAIL"
exit 1
fi
done
fi

hostapd_cli -i wlan0 enable > /dev/null
[ $? = 0 ] && echo "OK" || echo "FAIL"
;;
stop)
printf "Stopping Hotspot: "
hostapd_cli -i wlan0 disable > /dev/null
[ $? = 0 ] && echo "OK" || echo "FAIL"
;;
ssid)
if [ $# == 2 ]; then
printf "Setting Hotspot SSID: "
{
sed -i "s/^ssid=.*/ssid=$2/" $HOSTAPD_CONF &&
hostapd_cli -i wlan0 set ssid "$2" > /dev/null &&
echo "OK"
} || { # error catch
echo "FAIL"
exit 1
}
"$0" stop
killall hostapd
"$0" start
else
SSID=`hostapd_cli -i wlan0 status | sed -n 's/^ssid\[0\]=\(.*\)/\1/p'`
[ -n "${SSID}" ] && echo $SSID || exit 1
fi
;;
passwd)
if [ $# == 2 ]; then
printf "Setting Hotspot PASSWD: "
{
sed -i "s/^wpa_passphrase=.*/wpa_passphrase=$2/" $HOSTAPD_CONF &&
hostapd_cli -i wlan0 set wpa_passphrase "$2" > /dev/null &&
echo "OK"
} || { # error catch
echo "FAIL"
exit 1
}
"$0" restart
else
exit 1
fi
;;
reset)
printf "Resetting Setting Configuration: "
cp $HOSTAPD_CONF_DEFAULT $HOSTAPD_CONF
[ $? = 0 ] && echo "OK" || (echo "FAIL"; exit 1)
"$0" stop
killall hostapd
"$0" start
;;
state)
STATE=`hostapd_cli -i wlan0 status | sed -n 's/^state=\(.*\)/\1/p'`
[ -n "${STATE}" ] && echo $STATE || exit 1
;;
restart|reload)
"$0" stop
"$0" start
;;
*)
echo "Usage: $0 {start|stop|ssid|passwd|state|reset|restart}"
exit 1
esac

exit $?

/dev/random随机数不工作导致hostapd的密码错误的问题

现象:开启WEP加密后,密码错误:

1
2
3
random: Cannot read from /dev/random: Resource temporarily unavailable
random: Only 0/20 bytes of strong random data available from /dev/random
random: Not enough entropy pool available for secure operations

里面的0/20,多连几次可能会增加(如变成7/20/14/20),所以可能多连接几次就能连接成功了。

临时解决办法,用/dev/urandom替代:

1
2
$ mv /dev/random /dev/random.orig
$ ln -s /dev/urandom /dev/random