23 December 2009

This morning with black coffe & putty

This morning I dont have sleep yet.. make some coffee and start editing my old config.. this config not very good but it work.. still too lazy & dumb to make some "look advance" script..  hehehehe.. I use Shorewall for load balancing 2 ADSL connection and a few BASH script for switching from load balanced connection to single connection and switch from one to another modem in single connection.. This morning not too much I've edit in this script, I just change the IP address interface and adjust some parameter, what a troubled unstable connection..   hufff and now.. I still too lazy to type too much here.. my body feel drop but still cant sleep.. here I include my script in this post.. just to share, or probably any of you can give me some advice to improve the script..



[Copy&Paste]-----------------------------------------------------------------------------------------

#!/bin/bash

if [ -z "$1" ]; then
 echo " ";
 echo "[*]Perintah untuk ganti2 koneksi di GreeNET";
 echo "Option: 1   - kalau mau pake 1 koneksi, dengan tambahan option untuk pilih modem.";
 echo "              1 - Untuk pake koneksi dari modem 1";
 echo "                   Contoh perintah: koneksi 1 1";
 echo "              2 - Untuk pake koneksi dari modem 2";
 echo "                   Contoh perintah: koneksi 1 2";
 echo " ";
 echo "        2   - Kalau mau pake 2 koneksi, Option ini gak perlu Option tambahan.";
 echo "               Contoh: koneksi 2";
 echo " ";
 exit 0
fi

if [ $1 -gt 2 ]; then
  echo " ";
  echo "SALAH BOSS..!!";
  echo "Green masih pake 2 koneksi, perhatikan baik2 Option2 di bawah. ";
  echo "Option: 1   - kalau mau pake 1 koneksi, dengan tambahan option untuk pilih modem.";
  echo "              1 - Untuk pake koneksi dari modem 1";
  echo "                   Contoh perintah: koneksi 1 1";
  echo "              2 - Untuk pake koneksi dari modem 2";
  echo "                   Contoh perintah: koneksi 1 2";
  echo " ";
  echo "        2   - Kalau mau pake 2 koneksi, Option ini gak perlu Option tambahan.";
  echo "               Contoh: koneksi 2";
  echo " ";
  exit 0
fi



# The Proccess....
IPT="/sbin/iptables"
LOAD="/sbin/modprobe"
IFC="/sbin/ifconfig"

# pake 1 koneksi
if [ "$1" -eq "1" ]; then
     # pake 1 koneksi modem A
     if [ "$2" -eq "1" ]; then
        RED=eth0
        GREEN=eth2
        RED_IP=192.168.250.2
        GREEN_NET=192.168.1.0/24
        GATEWAY=192.168.250.1

      # pake 1 koneksi modem B
     elif [ "$2" -eq "2" ]; then
        RED=eth1
        GREEN=eth2
        RED_IP=192.168.251.2
        GREEN_NET=192.168.1.0/24
        GATEWAY=192.168.251.1
     else
       echo " ";
       echo "[*] Pilihan koneksi salah, lihat kembali Option yang tersedia.";
       exit 0   
     fi
            echo "Proses..";
         /sbin/shorewall stop  > /dev/null 2>&1
       
        # ganti2 default gw
        /etc/init.d/networking restart
        $IFC eth0 down
        $IFC eth1 down
       
        $IFC $RED up
        $IFC eth2 up
        route add default gw $GATEWAY
       
        #(sebagian contekan dari si_k0c1l)
        # Inisialisasi IPTABLES
        $LOAD ip_tables
        $LOAD iptable_filter
        $LOAD iptable_nat
        #$LOAD ip_conntrack
        #$LOAD ip_conntrack_ftp
        $LOAD ip_nat_ftp
        #$LOAD ip_conntrack_irc
        $LOAD ip_nat_irc

        # Kosongkan rumus-rumus IPTABLES
        $IPT -F
        $IPT -F -t nat
        $IPT -F -t mangle
        $IPT -X

        # Rumus default
        #$IPT -P INPUT DROP
        #$IPT -P FORWARD DROP
        #$IPT -P OUTPUT ACCEPT

        # Tembok Api
        #$IPT -A INPUT  -i eth0 -p tcp --syn --dport ! 22 -j DROP
        iptables -A OUTPUT -p tcp --dport 21 -j DROP
        iptables -A OUTPUT -p tcp --dport 25 -j DROP

        # Rumus masquerading
        $IPT -t nat -A POSTROUTING -s $GREEN_NET -o $RED -j SNAT --to-source $RED_IP

        # Rumus forward, hanya dari dalam atau luar related
        $IPT -A FORWARD -i $GREEN -o $RED -j ACCEPT
        $IPT -A FORWARD -i $RED -o $GREEN -m state --state ESTABLISHED,RELATED -j ACCEPT

        # Rumus INPUT, hanya terima dari dalam atau luar yang related
        $IPT -A INPUT -i lo -j ACCEPT
        $IPT -A INPUT -i $GREEN -j ACCEPT
        $IPT -A INPUT -i $RED -m state --state ESTABLISHED,RELATED -j ACCEPT

        # transparant proxy boo...
        #$IPT -t nat -A PREROUTING -i eth1 -p tcp -s 192.168.1.0/255.255.255.0 --dport 80 -j DNAT --to 192.168.1.1:8080

        # Hidupkan forwarding
        echo "1" > /proc/sys/net/ipv4/ip_forward
       
else  #pake 2 koneksi
    $IFC eth0 up
    $IFC eth1 up
    $IFC eth2 up
   
    $IPT -F
    $IPT -F -t nat
    $IPT -F -t mangle
    $IPT -X
    echo "[*]Tunggu sebentar, lagi proses...";
    /sbin/shorewall -f start > /dev/null 2>&1

fi

echo " ";
echo "[*]Beres bos.. sekarang GreeNET pake $1 koneksi";
echo "   Coba cek pake command: iptraf -g";
echo "   sudah sesuai keinginan apa belum";
# .Tonca

#[eof]-------------------------------------------------------------------------------------------
Reblog this post [with Zemanta]