[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

hotplug + ifupdown 实现 usb 电缆 modem 的网关






深圳天威视讯的有些电视上网


有一个 Motorola 的 USB 接口的电缆 modem,使用 2.6.x 系列的 usbnet 模块作 为驱动。

几台机器利用交换机连接成一个小的局域网,利用电缆 modem 所在机器作为网关 上网。 当usb线接上电脑时,自动从 ISP 获取 IP,给内网的网卡绑定 192.168.0.1 的内 部IP,在内网网卡上启动 dhcpd 服务,启动 iptables防火墙。
在拔掉 usb 线时,关闭服务。

OS:Debian 3.1 sarge
Kernel:2.6.8-2-686

网卡配置文件:
/etc/network/interfaces

### etherconf DEBCONF AREA. DO NOT EDIT THIS AREA OR INSERT TEXT BEFORE IT.
auto lo eth0
allow-hotplug eth1

iface lo inet loopback

iface eth0 inet static
address 192.168.0.10
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1

iface eth1 inet dhcp
hostname debian
up /etc/init.d/homenet start
down /etc/init.d/homenet stop

mapping hotplug
script grep
map eth1

### END OF DEBCONF AREA. PLACE YOUR EDITS BELOW; THEY WILL BE PRESERVED.

/etc/init.d/homenet 服务脚本文件:
#!/bin/sh

if [ "$(id -u)" != "0" ]
then
echo "You must be root to start, stop or restart gpm."
echo
exit 1
fi

homenet_start() {
echo "binding gateway IP 192.168.0.1 on eth0"
ifconfig eth0:0 192.168.0.1
echo "add NAT masquerade"
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
echo "start dhcpd on eth0"
/etc/init.d/dhcp start
return 0
}

homenet_stop() {
echo "clear NAT masquerade"
iptables -t nat -D POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
echo "rm iptables modules"
rmmod ipt_MASQUERADE iptable_nat ip_conntrack ip_tables
rmmod -v af_packet
echo "close gateway IP"
ifconfig eth0:0 down

echo "stop dhcpd"
/etc/init.d/dhcp stop
}

case "$1" in
start)
homenet_start || exit 1
;;
stop)
homenet_stop || exit 1
;;
restart)
homenet_stop && sleep 3
homenet_start || exit 1
;;
*)
echo "Usage: /etc/init.d/homenet {start|stop|restart}"
exit 1
esac

exit 0

DHCPD 服务的配置文件:

/etc/dhcpd.conf
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.2 192.168.0.9;
option domain-name "atzlinux.com";
option domain-name-servers 211.148.192.135,211.136.83.81,202.96.134.133;
option routers 192.168.0.1;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.0.255;
default-lease-time 600;
max-lease-time 7200;
}

--

肖盛文(Faris Xiao)
Email:atzlinux@163.com




Reply to: