miércoles, 18 de enero de 2012

Script para 2 enlaces de internet sobre la misma pc

Esto debe ir en la siguiente ruta:

/etc/NetworkManager/dispatcher.d

Ahora después de este escripts puedes mandar las seciones de http y https por un proveedor y lo demás por otro. En mi caso todas las descargas las tengo por un enlace y el acceso a mi pc por otro, espero les sirva

#!/bin/sh
# Script to dispatch NetworkManager events
#
# Runs ifupdown scripts when NetworkManager fiddles with interfaces.


CompareDG()
{
a="`ip route show| grep default | cut -d " " -f 5`"
if [ "$a" = "$1" ]; then
echo "$1 no puede tener Default gw" >> $LOGFILE
exit 1
fi
}

CompareIP()
{
exif="`ip route show | grep default | awk '{print $5;}'`"
intif=$1
echo "dentro de compare exif $exif">> $LOGFILE
#comparamos las ips entre eth0 y eth1
for i in `seq 1 3`; do
execif_ext=`ifconfig $exif | grep -Ev inet6 | grep inet | awk '{print $2}' | cut -d: -f2 | cut -d. -f$i`
execif_int=`ifconfig $intif | grep -Ev inet6 | grep inet | awk '{print $2}' | cut -d: -f2 | cut -d. -f$i`
echo "extif $exif == intif $intif" >> $LOGFILE
if [ "$execif_ext" = "$execif_int" ]; then
if [ $i -eq 3 ]; then
echo "$exif tiene configurado el mismo segmento $intif" >> $LOGFILE
exit 1
fi
continue
fi
done
}

IPT()
{
if [ "$1" = "clean" ]; then
$IPT -t mangle -D OUTPUT -p tcp --dport 80 -j MARK --set-mark 4
$IPT -t mangle -D OUTPUT -p tcp --dport 443 -j MARK --set-mark 4
$IPT -t nat -D POSTROUTING -o $INTIF -j SNAT --to-source $MY_INTIP
else
$IPT -t mangle -A OUTPUT -p tcp --dport 80 -j MARK --set-mark 4
$IPT -t mangle -A OUTPUT -p tcp --dport 443 -j MARK --set-mark 4
$IPT -t nat -A POSTROUTING -o $INTIF -j SNAT --to-source $MY_INTIP
fi
}

if [ -z "$1" ]; then
echo "$0: called with no interface" 1>&2
exit 1;
fi
IFACE="$1"
STATUS="$2"
EXTIF="eth0"
INTIF="eth1"
LOGFILE="/tmp/logfile_nm"
RT_TABLE="totalplay"
DG_TABLE="192.168.100.1"
MY_INTIP="`ifconfig $INTIF | grep -Ev inet6 | grep inet | awk '{print $2}' | cut -d: -f 2`"
IPT=iptables
echo Capturamos la siguiente informacion $1 $2 >> $LOGFILE

if [ $IFACE != "$INTIF" ]; then
echo La interface $IFACE no concuerda con el uso del Script >> $LOGFILE
exit 1
fi

echo Hacemos la operacion requerida $IFACE >> $LOGFILE
#Verificamos que el default route no este por eth1
CompareDG "$INTIF"
#Verificamos que eth0 que es la externa no tenga el mismo segmento que eth1
CompareIP "$INTIF"
#Creamos tabla de ruteo
echo "Flusheand tabla $RT_TABLE" >> $LOGFILE
ip route flush table $RT_TABLE
ip rule del fwmark 4 table $RT_TABLE
IPT "clean"
echo "Limpiando reglas de fw" >> $LOGFILE
ip route show table main | grep -Ev default | while read ROUTE; do ip route add table $RT_TABLE $ROUTE; done
ip route add default via $DG_TABLE dev $INTIF table $RT_TABLE
echo "Creamos reglas de firewall asociadas" >> $LOGFILE
IPT
ip rule add fwmark 4 table $RT_TABLE
#ip route flush cache
echo 1 > /proc/sys/net/ipv4/ip_forward
for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 0 > $f; done
ip route flush cache