Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388966
  • 博文数量: 120
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 18:15
文章分类

全部博文(120)

文章存档

2016年(13)

2015年(41)

2014年(66)

我的朋友

分类: LINUX

2015-12-10 16:43:32

wifi模块设置为AP模式,通过USB接口连接CPU,此时CPU通过网口连接外网,手机通过无线连接wifi提供的ap上网。
1、根据实际的情况,设置wifi模块为ap模式
2、使用桥接,配置网卡,脚本配置如下:

#!/bin/sh
################################################################################
#
#                router           Internet, dhcp-server
#                 -|
#                 -| eth0         IFu=eth0, dhcp-client
#          -----------|------------
#          |       | NAT     |
#          |       |       -|
#          |    |--+--|      |  IFd=br0, IPd=192.168.0.1, dhcp-server
#          |    |   -|bridge  |
#          -------|-----|---------
#               |eth1 |wlan0      IFd1=eth1, IFd2=wlan0 
#              /       \
#            pc0       pc1        dhcp-client
#
################################################################################
# Uplink port. This port link to Internet
IFu=eth0
# downlink port. This port link pc
IFd=br0
IFd0=eth1
IFd1=wlan0


#设置网桥和dhcp服务器的ip段
IPd=192.168.0.5
IPd_mask=255.255.255.0
# dhcp client address pool
IPd_start=192.168.0.6
IPd_end=192.168.0.10


################################################################################
# local variable
IPu=
DNS=
################################################################################
# $1 - deamon name
daemon_stop ()
{
local ix=0
local MAXTIMES=20
local DEAMON=${1}
local MSG="busybox ps | grep -r ${DEAMON}"


if [ ss"$(eval ${MSG})" == ss"" ]; then
return 0
fi


echo -n "$(start-stop-daemon -K -n ${DEAMON}) wait"


while [ ${ix} -lt ${MAXTIMES} ]; do
if [ ss"$(eval ${MSG})" != ss"" ]; then
sleep 1s
ix=$((${ix}+1))
else
echo "ok"
return 0
fi
echo -n "."
done


echo "fail"
return 1
}
################################################################################
# config bridge
ifconfig ${IFd0} 0.0.0.0 
ifconfig ${IFd1} 0.0.0.0
ifconfig ${IFd} down
brctl delbr ${IFd}
brctl addbr ${IFd}
brctl addif ${IFd} ${IFd0} 
brctl addif ${IFd} ${IFd1}
ifconfig ${IFd} ${IPd} up

################################################################################
# get internet ip from uplink port
daemon_stop udhcpc
udhcpc -i ${IFu} #为外接网卡自动获取ip地址,获取到的ip一定要是能正常上网的,有时候公司会对ip进行限制
TMP=$(ifconfig ${IFu} | grep "inet addr:")
IPu=$(echo ${TMP:20} | sed -e 's/Bcast.*$/ /')
echo "Uplink port ip: ${IPu}"

################################################################################
# get internet dns from uplink port获取dns
TMP=$(cat /etc/resolv.conf | grep "nameserver" | sed -e 's/nameserver/ /g')
DNS=$(echo ${TMP})
echo "DNS: ${DNS}"

################################################################################
# config downlink port dhcp server配置dhcp配置文件
DHCPD_CFG=/etc/udhcpd.conf
{
echo start ${IPd_start}
echo end   ${IPd_end}
echo interface ${IFd}
echo;
echo option dns ${DNS}
echo option lease 864000
echo option router ${IPd}
echo option subnet ${IPd_mask}
} > ${DHCPD_CFG}
daemon_stop udhcpd
udhcpd ${DHCPD_CFG} #启动dhcp服务器
echo "start udhcpd at interface ${IFd}"

################################################################################
# config nat配置nat
echo "1" >/proc/sys/net/ipv4/ip_forward
iptables -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
#使用SNAT规则,将dhcp的client地址转换成上行外接网卡地址,同时指定输出网卡,这样就能上网了
#此处也可以不使用网桥,直接将wlan转成eth0的地址也可

iptables -t nat -A POSTROUTING -s ${IPd}/${IPd_mask} -o ${IFu} -j SNAT --to-source ${IPu}
echo "wait for connect"


阅读(3875) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~