---
This keeps coming in handy every so often, it's a simple script to turn a Linux laptop into a router that manages traffic between it's wired and wireless interfaces. Today I ended up using it to get networking to a machine in an environment where there were no available ports on the switch to plug into; previously it's been used to provide access to a LAN which had no access to the WAN on its own, but there was a local wireless network which did.
Run this after establishing a wireless connection and connecting to another machine or switch, it will setup the private Ethernet network, iptables rules, forwarding, and the dhcp server.
#!/bin/bash
echo "router build for public interface ath0, private interface eth0"
echo "--------------------------------------------------------------"
echo "setting up the wire"
ifconfig eth0 192.168.0.1 netmask 255.255.255.0
echo "building nat for 192.168.0.X network"
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o ath0 -j MASQUERADE
echo "setting routes between ethernet and wireless"
iptables -A FORWARD -s 192.168.0.0/24 -o ath0 -j ACCEPT
iptables -A FORWARD -d 192.168.0.0/24 -m state --state ESTABLISHED,RELATED -i ath0 -j ACCEPT
echo "saving"
sh -c "iptables-save > /etc/iptables.rules"
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "success!"
echo "---------------------------------------"
echo "setting up dhcp for eth0"
echo "option domain-name-servers 4.2.2.2;" > /etc/dhcpd.conf
echo "default-lease-time 60;" >> /etc/dhcpd.conf
echo "max-lease-time 72;" >> /etc/dhcpd.conf
echo "ddns-update-style none;" >> /etc/dhcpd.conf
echo "authoritative;" >> /etc/dhcpd.conf
echo "log-facility local7;" >> /etc/dhcpd.conf
echo "subnet 192.168.0.0 netmask 255.255.255.0 {" >> /etc/dhcpd.conf
echo " range 192.168.0.100 192.168.0.254;" >> /etc/dhcpd.conf
echo " option routers 192.168.0.1;" >> /etc/dhcpd.conf
echo " option domain-name-servers 4.2.2.2;" >> /etc/dhcpd.conf
echo "}" >> /etc/dhcpd.conf
dhcpd
echo "dhcpd server running for eth0 network"
echo "router build for public interface ath0, private interface eth0"
echo "--------------------------------------------------------------"
echo "setting up the wire"
ifconfig eth0 192.168.0.1 netmask 255.255.255.0
echo "building nat for 192.168.0.X network"
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o ath0 -j MASQUERADE
echo "setting routes between ethernet and wireless"
iptables -A FORWARD -s 192.168.0.0/24 -o ath0 -j ACCEPT
iptables -A FORWARD -d 192.168.0.0/24 -m state --state ESTABLISHED,RELATED -i ath0 -j ACCEPT
echo "saving"
sh -c "iptables-save > /etc/iptables.rules"
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "success!"
echo "---------------------------------------"
echo "setting up dhcp for eth0"
echo "option domain-name-servers 4.2.2.2;" > /etc/dhcpd.conf
echo "default-lease-time 60;" >> /etc/dhcpd.conf
echo "max-lease-time 72;" >> /etc/dhcpd.conf
echo "ddns-update-style none;" >> /etc/dhcpd.conf
echo "authoritative;" >> /etc/dhcpd.conf
echo "log-facility local7;" >> /etc/dhcpd.conf
echo "subnet 192.168.0.0 netmask 255.255.255.0 {" >> /etc/dhcpd.conf
echo " range 192.168.0.100 192.168.0.254;" >> /etc/dhcpd.conf
echo " option routers 192.168.0.1;" >> /etc/dhcpd.conf
echo " option domain-name-servers 4.2.2.2;" >> /etc/dhcpd.conf
echo "}" >> /etc/dhcpd.conf
dhcpd
echo "dhcpd server running for eth0 network"
So far this has been tested to setup a wireless bridge on Ubuntu 9.10 as well as multiple Gentoo installs.
2 comments: