Home > Linux > linux simple router

linux simple router

June 16th, 2010

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



IF_NET=$(ifconfig $IF_NET | awk ‘/inet/ {print $2}’ | grep -o ‘[0-9]*\.[0-9]*\.[0-9]*’)

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
echo “Enter this information in connected machines:”
echo “IP address : $IP_LOC.2-254”
echo “Netmask :”
echo “Gateway : $IP_LOC.1”
echo “DNS : $(echo -n $(awk ‘/^nameserver/ {print $2}’ /etc/resolv.conf))”
echo “”
echo “Press ENTER to continue.”

#monitor connection
iftop -i $IF_LOC

echo “Error setting up network”

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 [/sourcecode] and give execute rights to the script: [sourcecode lang="bash"] chmod +x router.sh [/sourcecode] now just run it: [sourcecode lang="bash"] sudo ./router.sh [/sourcecode] Script will show configuration of new created network [sourcecode lang="bash"] Enter this information in connected machines: IP address : Netmask : Gateway : DNS : Press ENTER to continue. [/sourcecode] and start monitor its traffic, to quit routing hit q or CTRL+C It is simplest way to start sharing internet I know off

Categories: Linux Tags: , ,
Comments are closed.