某些场景下, 需要使用不同的MAC地址与外界通信,查询了部分资料, 现记录如下:
1.生成MAC地址命令如下:
dd if=/dev/urandom bs=1 count=32 2>/dev/null | md5sum | cut -b 0-12 | sed 's/\(..\)/\1:/g; s/.$//'
2.openwrt通过MACVLAN使用不同的MAC地址与WAN口设备通信:
(添加到/etc/rc.local文件中, 以便开机自动运行)
# set up virtual mac addresses as aliases on the main WAN i/f eth0.2
ip link add link eth0.2 eth2 type macvlan
ifconfig eth2 hw ether `dd if=/dev/urandom bs=1 count=32 2>/dev/null | md5sum | cut -b 0-12 | sed 's/\(..\)/\1:/g; s/.$//'
` ip link add link eth0.2 eth3 type macvlanifconfig eth3 hw ether `
dd if=/dev/urandom bs=1 count=32 2>/dev/null | md5sum | cut -b 0-12 | sed 's/\(..\)/\1:/g; s/.$//'
` ip link add link eth0.2 eth4 type macvlanifconfig eth4 hw ether `
dd if=/dev/urandom bs=1 count=32 2>/dev/null | md5sum | cut -b 0-12 | sed 's/\(..\)/\1:/g; s/.$//'
` ip link add link eth0.2 eth5 type macvlanifconfig eth5 hw ether `
dd if=/dev/urandom bs=1 count=32 2>/dev/null | md5sum | cut -b 0-12 | sed 's/\(..\)/\1:/g; s/.$//'
`ifup eth2
ifup eth3
ifup eth4
ifup eth5
# default route
route add default gw 192.168.188.1 dev eth0.2
在/etc/config/network中加入我们上面添加的interface:
config 'interface' 'wan2'
option 'ifname' 'eth2'
option 'proto' 'dhcp'
option 'defaultroute' '0'
option 'peerdns' '0'
option 'gateway' '0.0.0.0
在/etc/config/firewall中加入上面添加的interface
config zone
option name wan1
option network 'wan1'
option input REJECT
option output ACCEPT
option forward REJECT
option masq 1
option mtu_fix 1
# forwards from 1st WAN i/f to SP2010 Web01
config redirect
option src wan1
option src_dport 3389
option proto tcp
option dest_ip 192.168.188.94
添加alias接口,以便openwrt的wan能够直接访问modem, 然后使用iptables规则, 将访问请求改写src ip为alias接口的IP
config 'alias'
option 'interface' 'wan'
option 'proto' 'static'
option 'ipaddr' '192.168.0.2'
option 'netmask' '255.255.255.0'
iptables -t nat -I postrouting_rule -s 192.168.188.0/24 -d 192.168.1.1 -j SNAT --to 192.168.1.99
iptables -I zone_lan_forward -s 192.168.1.0/24 -d 192.168.1.99 -j ACCEPT
或者
MODEMIP=192.168.0.1
MODEM_NET=`echo $MODEMIP | cut -d "." -f 1-3`
ROUTER_WAN_PORT_IP=192.168.0.2
WAN_PORT=eth0.2
ifconfig $WAN_PORT $ROUTER_WAN_PORT_IP netmask 255.255.255.0 broadcast $MODEM_NET.255
iptables -A forwarding_rule -d $MODEMIP -j ACCEPT
iptables -t nat -A postrouting_rule -d $MODEMIP -o $WAN_PORT -j MASQUERADE
参考资料:
http://osxdaily.com/2010/11/10/random-mac-address-generator/
https://shuggill.wordpress.com/2012/08/22/configuring-multiple-public-dhcp-ip-addresses-on-a-linksys-wrt54g-with-openwrt/
-全文完-