Buat routing tabel nice dan update secara berkala
Script ini dibuat untuk melakukan pemisahan routing antara trafik Indonesia Internet Exchange (NICE/IIX) dan Internasional (IX). Hal ini dengan asumsi router linux belum mempunyai BGP Peer untuk menerima daftar dinamik routing tabel nice dengan protokol BPG. Tabel nice bisa diperoleh dari situs mikrotik indonesia. Update dilakukan secara berkala atau periodik dengan cronjob.
Script yang diperlukan ada dua:
- make-nice.sh
Script ini akan unduh tabel nice, kemudian jalankan script ipr-nice.sh baik untuk menghapus routing tabel nice lama maupun buat yang baru. - ipr-nice.sh
Script ini dijalankan oleh make-nice.sh untuk buat atau hapus routing tabel nice.
Script make-nice.sh
#!/bin/bash # File: /usr/local/sbin/make-nice.sh # # Deskripsi: Membuat tabel nice, hapus routing tabel nice lama dan buat yang baru # # Created by Arief Yudhawarman (2009) # Email: awarmanff at yahoo.com # # # PARAMETER # WGET=/usr/bin/wget SED=/bin/sed ECHO=/usr/bin/echo CAT=/bin/cat LN=/bin/ln # nice url NICEURL=/usr/local/sbin/nice.url # log download nice, gagal atau sukses ditulis di sini NICE_LOG=/usr/local/sbin/nice.log # tabel nice lama NICE_OLD=/usr/local/sbin/nice-old.txt # tabel nice.txt digunakan oleh script ipr-nice.sh NICE=/usr/local/sbin/nice.txt # buat tabel nice baru NICE_NEW=/usr/local/sbin/nice-new.txt # # UNDUH TABEL NICE # # download tabel nice dari mikrotik indonesia # jika download gagal, tulis log dan keluar dengan exit code 0 $WGET -O /usr/local/sbin/nice.rsc -i $NICEURL > /dev/null 2>&1; RETVAL=$?; [ $RETVAL -gt 0 ] && $ECHO "Download nice gagal." > $NICE_LOG && exit 0 $ECHO "Download nice sukses." > $NICE_LOG # # BAKUP TABEL NICE # # bakup file nice.txt ke nice_old.txt [ -e $NICE_NEW ] && /bin/cat $NICE_NEW > $NICE_OLD # flush tabel nice $CAT /dev/null > $NICE_NEW # parse network nice dan simpan ke tabel nice $SED -n '/^add list/s/add list=nice address="\([0-9.\/]*\).*"/\1/p' /usr/local/sbin/nice.rsc >> $NICE_NEW # # HAPUS ROUTING TABEL NICE LAMA # if [ -e $NICE_OLD ] then # symlink nice-old.txt ke nice.txt $LN -sf $NICE_OLD $NICE # hapus routing tabel nice lama /usr/local/sbin/ipr-nice.sh del fi # symlink nice-new.txt ke nice.txt $LN -sf $NICE_NEW $NICE # # BUAT ROUTING TABEL NICE BARU # /usr/local/sbin/ipr-nice.sh add
Penjelasan:
- Baris 35: Unduh tabel nice dari situs mikrotik indonesia. Jika gagal maka tulis log dan script langsung berhenti dengan exit code 0.
- Baris 36: Tulis log jika download nice sukses.
- Baris 42: Jika telah unduh tabel nice sebelumnya maka bakup tabelnya ke nice-old.txt.
- Baris 45: Buat file kosong tabel nice baru.
- Baris 48: Parse network nice dan simpan ke file tabel nice baru.
- Baris 53-59: Jika ada tabel nice lama maka hapus routingnya.
- Baris 66: Buat routing tabel nice baru.
Script ipr-nice.sh
#!/bin/bash
# File: /usr/local/sbin/ipr-nice.sh
#
# Deskripsi: Buat/Hapus routing tabel nice
#
# Created by Arief Yudhawarman (2009)
# Email: awarmanff at yahoo.com
# Parameter
GATEWAY=10.10.10.1
IP=/sbin/ip
GREP=/bin/grep
CAT=/bin/cat
ECHO=/bin/echo
# tabel nice
NICE=/usr/local/sbin/nice.txt
# tabel network lokal
LOCAL=/usr/local/sbin/localnet.txt
# Argumen
# $1: add | del
if [ $# -lt 1 ]
then
$ECHO "Usage: $0 [ add|del ]"
exit 0
fi
case "$1" in
add|del)
# NICE
$GREP -v '^1.2.3.4' $NICE |
while read i
do
$IP ro $1 $i via $GATEWAY
done
# Network lokal
$CAT $LOCAL |
while read i
do
$IP ro $1 $i via $GATEWAY
done
;;
*) $ECHO "Usage: $0 [ add|del ]"
;;
esac
Penjelasan:
- Baris 11: IP gateway ke nice.
- Baris 26: Script dijalankan dengan memasukkan argumen add (buat routing tabel nice) atau del (hapus routing tabel nice).
- Baris 34-38: Baca tabel nice.txt dan buat routing tabel nice baru.
- Baris 41-45: Baca tabel localnet.txt dan tambahkan ke routing tabel nice baru.
Contoh tabel nice.txt:
1.2.3.4 182.0.0.0/12 114.120.0.0/13 120.168.0.0/13 114.56.0.0/14 125.166.0.0/15 125.162.0.0/16 125.163.0.0/16 125.160.0.0/16 125.161.0.0/16 125.164.0.0/16 125.165.0.0/16 120.163.0.0/16 120.162.0.0/16 120.161.0.0/16 120.160.0.0/16 124.81.0.0/16 222.124.0.0/16
Contoh tabel localnet.txt:
10.10.2.0/24 192.168.1.0/24 192.168.2.0/24
Agar bisa update secara berkala maka script make-nice.sh dijalankan via cronjob:
# m h dom mon dow command 0 5 * * * /usr/local/sbin/make-nice.sh
Last update: 2010-05-29 18:15:00 +07:00
Advertisement
ashadebi
May 29, 2010 at 12:28 pm
Itu nanti file nice.txt sama localnet.txt sudah generate sendiri atau harus diisi sendiri Bos?
Mbah Darmo
January 7, 2011 at 7:19 am