linux simple router
June 16th, 2010
Comments off
Had you ever need of simple routing, like sharing your connection to other computer?
I had :) so here is a script which turns my laptop into a router, simple configuration, one command, to run it save the script as router.sh
#!/bin/bash
#configuration
IF_LOC=eth0
IP_LOC=192.168.67
IF_NET=wlan0
#script
IF_NET=$(ifconfig $IF_NET | awk '/inet/ {print $2}' | grep -o '[0-9]*\.[0-9]*\.[0-9]*')
#connect
iptables-save > /dev/shm/old_routes
ifconfig $IF_LOC inet $IP_LOC.1
ip route add $IP_LOC.0/24 dev $IF_LOC src $IP_LOC.1
iptables -t nat -A POSTROUTING -s $IF_NET.0/24 -j MASQUERADE
if ip route | grep $IP_LOC.1 >/dev/null
then
#message
clear
echo "Enter this information in connected machines:"
echo "IP address : $IP_LOC.2-254"
echo "Netmask : 255.255.255.0"
echo "Gateway : $IP_LOC.1"
echo "DNS : $(echo -n $(awk '/^nameserver/ {print $2}' /etc/resolv.conf))"
echo ""
echo "Press ENTER to continue."
read
#monitor connection
iftop -i $IF_LOC
else
echo "Error setting up network"
fi
#disconnect
ip route del $IP_LOC.0/24 dev $IF_LOC
ifconfig $IF_LOC down
iptables-restore < /dev/shm/old_routes
rm /dev/shm/old_routes
#end
and give execute rights to the script:
chmod +x router.sh
now just run it:
sudo ./router.sh
Script will show configuration of new created network
Enter this information in connected machines: IP address : 192.168.67.2-254 Netmask : 255.255.255.0 Gateway : 192.168.67.1 DNS : 217.172.224.160 89.228.6.21 Press ENTER to continue.
and start monitor its traffic, to quit routing hit q or CTRL+C
It is simplest way to start sharing internet I know off


