Thursday, November 24, 2011

Ubuntu ICS

Source is from this link http://ubuntu-works.blogspot.com/2010/06/internet-connection-sharing-in-ubuntu.html

#!/bin/bash

# (c) Mahesh R. S. (themaheshrs _at_ gmail.com)
# Created on June 20, 2010.
# distributed under GPL or some such liberal open-source licence.

# Purpose: This simple script enables Internet Connection Sharing
# on my computer.
#
# I have multiple computers at home. Only one of them can connect
# to the Internet over a 3G USB modem. Typically, that computer is
# either an old ACER laptop -OR-
# an old Lenovo laptop.
# All the other computers connect to the Internet through either
# of these two laptops. So, this script is typically run on either
# of those notebooks.
#
# The idea here is:
# 1. Enable IP-Forwarding, so that the laptop that is connected to
# the Internet can pass packets around between its interfaces
# 2. Enable Network-Address-Translatin (NAT) on the laptop that is
# connected to the Internet so that the other computers can enjoy
# the Internet
# 3. Enable a local interface through which the other computers connect
# to the Internet

# This script assumes that your notebook connects to the Internet through
# ppp0 and that your internal computers connect to eth0. The internal
# network is on the 192.168.100/24 subnet. (TBD: I should be able to make
# those values "configurable" by having the user pass them as arguments.)

if [ `id -u` -ne 0 ]
then
echo ":( PLEASE RUN THIS SCRIPT WITH ROOT PERMISSIONS."
echo "e.g. \$ sudo $0 "
exit -1
fi

if [ $# -ne 1 ]
then
echo "Please specify whether to start or stop ICS."
echo "e.g. \$ sudo $0 "
exit -1
fi


ics_stop()
{
# disable IPv4 Forwarding
echo 0 > /proc/sys/net/ipv4/ip_forward

# the following three rules are as defined in:
# https://help.ubuntu.com/10.04/serverguide/C/firewall.html
# note that we are deleting the rules here, hence the "-D"
iptables -t nat -D POSTROUTING -s 192.168.100.0/24 -o ppp0 -j MASQUERADE
iptables -D FORWARD -s 192.168.100.0/24 -o ppp0 -j ACCEPT
iptables -D FORWARD -d 192.168.100.0/24 -m state --state ESTABLISHED,RELATED -i ppp0 -j ACCEPT

# unconfigure eth0 to connect to the internal (NAT-ted) network
ip address del 192.168.100.1/24 dev eth0
}

ics_start()
{
# enable IPv4 Forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

# the following three rules are as defined in:
# https://help.ubuntu.com/10.04/serverguide/C/firewall.html
# note that we are adding the rules here, hence the "-A"
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ppp0 -j MASQUERADE
iptables -A FORWARD -s 192.168.100.0/24 -o ppp0 -j ACCEPT
iptables -A FORWARD -d 192.168.100.0/24 -m state --state ESTABLISHED,RELATED -i ppp0 -j ACCEPT

# configure eth0 to connect to the internal (NAT-ted) network
ip address add 192.168.100.1/24 dev eth0
}

case $1 in
start)
ics_start
echo "ICS started."
;;
stop)
ics_stop
echo "ICS stopped."
;;
 

1 comment:

  1. The last line should be a closing statement for case condition in bash. So enter this.

    ESAC

    ReplyDelete