#!/bin/sh
####################################
# #
# host -> gateway -> INTERNET #
# #
####################################
# Ethernet card in host computer
ECARDINHOST=eth0
# Ethernet card in gateway computer (connects to host)
ECARTINGW_2HOST=eth0
# Ethernet card in gateway computer (connects to Internet)
ECARDINGW_2INTERNET=eth1
## Settings for host<->gateway network
# Netmask
NETMASK=255.255.255.0
# IP address for gateway
IP_GW=192.168.50.2
# IP address for host
IP_HOST=192.168.50.1
if [ "$#" -eq "0" ]; then
echo -e "Usage:\n\t$0 [--gateway|--host]\n"
exit 0
elif [ "$#" -eq "1" ]; then
case "$1" in
"--gateway")
#checking internet connection
ping -W 5 -c 1 ya.ru
if [ "$?" -ne "0" ]; then
echo 'No Internet connection! Aborting!!!'
exit 1
fi
# enable packet forwarding
sudo -u root -s "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo ifconfig $ECARTINGW_2HOST $IP_GW netmask $NETMASK
sudo iptables -t nat -A POSTROUTING -o $ECARDINGW_2INTERNET -j MASQUERADE
echo 'Add this lines to /etc/resolv.conf in host computer (not in gateway!):'
echo
cat /etc/resolv.conf
echo
exit 0
;;
"--host")
sudo ifconfig $ECARDINHOST $IP_HOST netmask $NETMASK
sudo route add default gw $IP_GW
echo 'Add lines from gateway resolv.com to host resolv.conf'
echo 'Do not forget say "sudo route del default gw 192.168.50.2"!'
exit 0
;;
esac
fi