入手Raspberry Pi Model B (512MB) 已经一段时间了。装上官方的Raspbian,感觉跟手头上的AO522没区别。同样是基于Debian,同样是LXDE桌面。也试了下Xbian (XMBC + Raspbian),感觉非常棒,只是菜单操作有点卡。可惜手上的SD卡空间不够大,否则,还可以玩玩Android。
玩过后,正是按计划进行。首先是作为无线路由来使用。其实就跟Lubuntu共享WiFi一样,测试过程中,还改良了一下以前写的sharewifi脚本(以前的版本:http://www.foxail.org/blog/?p=490)。但是还有一个问题,执行该脚本的stop操作后,系统不会把建立的mon.wlan0接口关闭,需要重启系统,wifi才能从共享模式变成正常的接收模式。
1)sharewifi脚本,设置共享wifi,我放在/opt下,内容如下:(首先要安装hostapd和dnsmasq,执行sudo apt-get install hostapd dnsmasq
即可)
### begin the file /opt/sharewifi #########################################
#!/bin/sh
# Setup wireless AP, share the Internet from interface0 to interface1
# USAGE: sharewifi [ start | stop ] interface0 interface1
# EXAMPLE: sharewifi start wlan1 wlan0
help( )
{
cat << HELP
Setup wireless AP, share the Internet from interface0 to interface1
USAGE: sharewifi [ help | start | stop ] interface0 interface1
EXAMPLE: sharewifi start wlan1 wlan0
List clients:
cat /var/lib/misc/dnsmasq.leases
HELP
exit 0
}
start( )
{
echo Starting share wifi ......
echo Share Internet $port_in to $port_out
# Configure iptable rules
iptables -F
iptables -t nat -A POSTROUTING -s $ip_prefix.0/24 -o $port_in -j MASQUERADE
iptables -A FORWARD -s $ip_prefix.0/24 -o $port_in -j ACCEPT
iptables -A FORWARD -d $ip_prefix.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i $port_in -j ACCEPT
# Log the message of route
#iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j LOG --log-prefix "NEW_HTTP_CONN: "
# Save iptable rules
sh -c "iptables-save > /etc/iptables.rules"
# Configure hostapd
hostapd_conf=/etc/hostapd/hostapd.conf
[ -f $hostapd_conf ] && rm $hostapd_conf
echo >> $hostapd_conf interface=$port_out
echo >> $hostapd_conf driver=nl80211
echo >> $hostapd_conf ssid=foxrpi-ap
echo >> $hostapd_conf channel=1
echo >> $hostapd_conf hw_mode=g
echo >> $hostapd_conf auth_algs=1
echo >> $hostapd_conf wpa=3
echo >> $hostapd_conf wpa_passphrase=1234567890
echo >> $hostapd_conf wpa_key_mgmt=WPA-PSK
echo >> $hostapd_conf wpa_pairwise=TKIP CCMP
echo >> $hostapd_conf rsn_pairwise=CCMP
chmod 755 $hostapd_conf
# Configure /etc/dnsmasq.conf
dnsmasq_conf=/etc/dnsmasq.conf
[ -f $dnsmasq_conf ] && rm $dnsmasq_conf
echo >> $dnsmasq_conf interface=$port_out
echo >> $dnsmasq_conf bind-interfaces #这个是只监听wlan0,没有之会检测所有卡
echo >> $dnsmasq_conf except-interface=lo
echo >> $dnsmasq_conf dhcp-range=$ip_prefix.10,$ip_prefix.110,6h #设置dhcp地址范
chmod 755 $dnsmasq_conf
# Enable routing
sysctl net.ipv4.ip_forward=1
#killall named
/etc/init.d/hostapd stop
ifconfig $port_out $ip_prefix.1
hostapd -B $hostapd_conf
/etc/init.d/dnsmasq restart
echo Sucess share wifi
exit 0
}
stop( )
{
echo Stopping share wifi ......
echo Stop share Internet $port_in to $port_out
# Configure iptable rules
iptables -F
# Log the message of route
#iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j LOG --log-prefix "NEW_HTTP_CONN: "
# Save iptable rules
sh -c "iptables-save > /etc/iptables.rules"
# Configure hostapd
hostapd_conf=/etc/hostapd/hostapd.conf
[ -f $hostapd_conf ] && rm $hostapd_conf
# Configure /etc/dnsmasq.conf
dnsmasq_conf=/etc/dnsmasq.conf
[ -f $dnsmasq_conf ] && rm $dnsmasq_conf
# Enable routing
sysctl net.ipv4.ip_forward=0
#killall named
/etc/init.d/hostapd stop
/etc/init.d/dnsmasq stop
ifconfig $port_out down
ifconfig $port_out del $ip_prefix.1
ifconfig $port_out up
echo Sucess stop share wifi
exit 0
}
ip_prefix=192.168.23 #wifi路由所用网段
#port_in is the network interface which connected to Internet, and default wlan1.
port_in=wlan1
#port_out is the network interface which will be setup AP, and default wlan0.
port_out=wlan0
if [ -n "$2" ]; then
port_in=$2
if [ -n "$3" ]; then
port_out=$3
fi
fi
case "$1" in
"help" )
help ;;
"start" )
start ;;
"stop" )
stop ;;
*)
help ;;
esac
### end the file /opt/sharewifi #########################################
2)设置开机自动启动,先编写开机启动的脚本。在/etc/init.d下建立文件sharewifi,文件内容如下:
### begin the file /etc/init.d/sharewifi #########################################
#!/bin/sh
### BEGIN INIT INFO # Provides: sharewifi
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the Wireless AP server
# Description: starts the Wireless AP using start-stop-daemon
### END INIT INFO
sharewifi=/opt/sharewifi
NAME=sharewifi
DESC=sharewifi
# Include nginx defaults if available
if [ ! -f $sharewifi ]; then
echo "Can't find the file $sharewifi"
exit 1
fi
case "$1" in
start)
echo -n "Starting $DESC: "
sh $sharewifi start eth0 wlan0
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
sh $sharewifi stop eth0 wlan0
echo "$NAME."
;;
restart)
echo -n "Restarting $DESC: "
sh $sharewifi stop eth0 wlan0
sleep 1
sh $sharewifi start eth0 wlan0
echo "$NAME."
;;
*)
echo "Usage: $NAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
### end the file /etc/init.d/sharewifi #########################################
3)执行以下命令,即可设置开机启动:
sudo update-rc.d /etc/init.d/sharewifi defaults