Tuesday, March 3, 2015

Round Robin Internet Failover Setup



Ahoy! It's been a while and been to busy. Here is another of my personal projects and I hope this could help others too. I have updated the instructions below so that it would be easier to follow. I hope :)

In this setup I used gateway switching on the WAN side if a fail on the current router occurred.
Below is a sample graphic on how this setup was made…


 





From the Router side:
- the DHCP was disabled
- set their IP for example in setting like 192.168.0.1/24 for the first router, 192.168.0.2/24 for the second and so on.

For the Server:
- Set it’s WAN IP like 192.168.0.10/24 or  whatever  IP you want as long as the Routers and your server are on the same subnet
- Set the LAN side like 192.168.1/24 or what ever IP you like as long as it is not the same subnet as the WAN side
- setup the Server for the LAN side as DHCP, BIND(optional), SQUID(optional).(kindly see the link)
- the IPTABLES for MASQUERADING for LAN for internet sharing. (kindly see the link)
- I used Ubuntu linux as my server in this setup.

Script1 the route table. You can save this as rtable.sh:
#!/bin/bash
# This is my route add script.
# created by verZion 2/26/2015
# place this in /etc/rc.local for this to run at startup

#clear the current route table
/sbin/route del -net 0.0.0.0
/sbin/route del -net 0.0.0.0
/sbin/route del -net 0.0.0.0

#deploy the route table. Note that the last executed is the primary used gateway
/sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.0.3
/sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.0.2
/sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.0.1
#end of script

- make rtable.sh executable
# chmod a+x rtable.sh
 
- To test Script1 execute it manually and to see if the last executed route is in the beginning of the output list.
# ./rtable.sh 
# netstat -nr            (to check the route table)
Example output on the deployed route table...
$ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.0.2     0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.0.3     0.0.0.0         UG        0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0


- If there are no problems encountered on the rtable.sh script put it in /etc/rc.local file. Just put it above exit 0 line 
ex: 
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.


 /home/verzion/rtable.sh  


 exit 0

#####
Script2 the failover:
- Below is the script I created for the round robin internet failover. Save it as filename netfailover.sh
#!/bin/bash
# This is my internet failover script.
# created by verZion 2/26/2015
# Placed this in cron and set it to run every 5 mins to check the connection.
# Why 5mins? It's because it will download a 2MB file to determine the
# consistency of the connection.
# If you wish for a lighter checking uncomment the PING line below and set it to cron to check
# every 1minute. Note cron smallest interval is 1 minute, no seconds.
# updated 3/6/2015 - found some bugs on the loop statement

#failsw Legend
# 0 = do the check.
# 1 = failure encountered. Email the admins.
# 2 = critical error stop the email until the admin do something to fix the problem


#initialize some variables
loop1=1
dum01=0
failsw=0
gwh=3
#uncomment dURL if you are going to use download as check tool
#dURL="http://squid.acmeconsulting.it/download/squid-2.7.STABLE8-bin.zip"

#get the current route table and put it in dump01 file
/bin/netstat -nr > /tmp/dump01

#Set the maximum gateways in the network
maxcon=3

#Start testing and changing the connection on failure
while [  $loop1 -le $maxcon ] && [ $failsw -ne 2 ]; do

        gw=$(awk '{print $2};' /tmp/dump01 | head -$gwh | tail -1 )
         echo Checking connection on $gw... >> /var/log/syslog
       #download the file
        #wget --limit-rate=30k --tries=1 --connect-timeout=10 --read-timeout=5  --no-check-certificate --output-document=/tmp/dummy $dURL &> /dev/null

        #if you wish to use ping instead of download uncomment the line below
        #and comment the wget line above
        ping zdnet.com -c3 &> /dev/null

        #check the return value where non 0 means we have a problem
        if [[ "$?" -ne 0 ]]; then
            let loop1=$loop1+1
            failsw=1

            if [[ $loop1 -le $maxcon ]]; then  #if less than or equal change the route table
                 echo network problem detected!!! Changing gateway...
                 echo clearing the route configuration...
                 let loop2=$maxcon-1
                 while [  $loop2 -ne 0 ]; do
                        echo delete route $loop2
                        /sbin/route del -net 0.0.0.0
                        let loop2=$loop2-1
                 done

                 echo then shuffling the route table...        
                 loop2=3
                 let dum01=$maxcon+2
                 let loop4=$loop1+2
                 while [ $loop2 -lt $dum01 ] || [ $loop2 -eq $dum01 ]; do
                        if [[ $loop2 -ne $loop4 ]]; then
                            gw=$(awk '{print $2};' /tmp/dump01 | head -$loop2 | tail -1 )
                            echo loading gateway $gw
                            /sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw $gw
                       fi
                       let loop2=$loop2+1
                 done
                 #the last route is the primary gateway
                 let dum01=$loop1+2
                 gw=$(awk '{print $2};' /tmp/dump01 | head -$dum01 | tail -1 )
                 echo then loading default gateway $gw
                 /sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw $gw
           fi
           let gwh=$gwh+1
        else
            echo no network problem detected. Exiting!
            break     #exit loop
        fi
done

#check if all gateways failed
if  [[ $loop1 -gt $maxcon ]]; then
    echo Warning all gateways failed!!!
    sed -i '20s/failsw=0/failsw=2/' /home/verzion/netfailover.sh   #This means to stop the checking until failsw is set to 0
    #email the admin about it
    /usr/bin/mail -s "Server reporing: MAJOR Problem on the internet connection!!!" verzion@internal.com <<< "All gateways are down! :( After fixing the problem reset the failsw to 0 to enable the autocheck again."
fi
if [[ $failsw -eq 1 ]]; then
    #email the admin about the current gateway changes
    echo Warning 1 of the internet gateway has failed. Currently gateway $gw is loaded.
    /usr/bin/mail -s "Server reporing: Internet failure detected!" verzion@internal.com <<< "The internet gateway was changed to $gw"
fi
if [[ $failsw -eq 2 ]]; then
    echo Major network downtime detected. Reset the failsw variable to 0 after fixing the problem
fi

#end of script

- set the permission on the script files so it will become executable.
# chmod a+x netfailover.sh

- you may test it manually by executing it like example below
# ./netfailover.sh

- If no problems occurred on the manual execution now edit /etc/crontab and add this below the lines.
* * * * *   root    sleep 10; /home/verzion/netfailover.sh >> /var/log/syslog

- No need to restart cron as it will see the changes after the save.

- to see the log files execute the command below…
# tail -f /var/log/syslog

- Below is a sample log output from syslog…
Mar  2 12:17:01 VZServer CRON[32463]: (root) CMD (   sleep 10; /home/verzion/netfailover.sh >> /var/log/syslog)
Checking connection on 192.168.0.1...
no network problem detected. Exiting!


Summary on the Setup
- Note on the script there is a line that mails the admin as notification. I have setup an internal mail and the setup for the internal mail is not covered here.

- Also on the Script2 the BLUE colored text is the one that you can modify according to your needs and the VIOLET colored is the one you need to reset if a total failure was met in the script.

- Below are some details on the colored BLUE on Script2 (netfailover.sh)

#dURL="http://squid.acmeconsulting.it/download/squid-2.7.STABLE8-bin.zip"
If you are going to use the download test just remove the remark(#) and put the remark on ping line. In short use only either of the two.

#Set the maximum gateways in the network
maxcon=3
You set the number of routers here and the lowest is 2. Please do not use this script for only 1 router. It would be senseless.

ping zdnet.com -c3
Here you may use any URL you wish. You can ping facebook.com if you want.
on the -c3 option of the ping command this says how many times the ping should run where it is set to 3. You may set it higher or lower according to your needs.

/home/verzion/netfailover.sh
This states where the netfailover.sh script was placed on your computer. Do not forget to change this as the path is needed to be correct.

"Server reporing: MAJOR Problem on the internet connection!!!" verzion@internal.com
All under the mailing line this sends mail to the internal mail I set up on my server. The notes for the mail server setup is not found on this documentation. Kindly search for it as there are tons of working tutorials out there.
If you do not wish to use this as of now you may remark(#) the mail command line.


Have fun coding!!!  

Tuesday, May 13, 2014

Using wget to update no-ip account

This was created because the WAN IP is connected to is dynamic and the ISP has NAT to it. To further explain it it was like this...


MYPC(192.168.0.x) --> router(10.x.x.x) --> ISP(120.x.x.x)

As you can see if the DUC app the no-ip provided checks our IP it will see the 120.x.x.x and not the 10.x.x.x so I created a script to extract the IP from the router. After getting the IP from the router the trick to send the acquired IP to update our account is...


wget.exe -Ooutput -E --http-user="your@mail.com" --http-password=p4ssw0rd "http://dynupdate.no-ip.com/nic/update?hostname=yourDNS.no-ip.org&myip=1.2.3.4"

This will update the IP of the account DNS and will put the result to a file named "output"
I colored the command Blue where it should not be changed and Green to change it according to your needs.

To see the documentation on how to update your DNS in no-ip.com kindly see http://www.noip.com/integrate/

Wednesday, October 23, 2013

Revoke/Unrevoke a client certificate in OpenVPN

Source: http://blog.abhijeetr.com/2012/06/revokeunrevoke-client-certificate-in.html

REVOKING

To revoke the access of a client, the first method will be to use the Client Revocation List. For that, go to easy_rsa directory & execute (where cname is the one which you want to disable)


# source ./vars
# ./revoke-full cname



Then move the file crl.pem created in keys folder to the /etc/openvpn/ folder. Finally, edit the server.conf & add the following line.

crl-verify crl.pem

The above file is append-only file & re-read every time a client connects to the server so there is no need to restart the server next time you overwrite the file.


 UNREVOKING

Now coming on the un-revoking part, I tried asking the above question on serverfault & came to know that un-revoking of certificate should generally not be done.

But, even then if you want to do that I will quote the guy:
in your CA folder, there should be an index.txt, with certificate IDs in it. The ones starting with "V" are valid, and ones with "R" are revoked. You can edit that file, and fix the first char to "V", and delete the third column (the revocation date). If you have more then one certificate, you should see the pattern (sequential number comes in the third column now, etc).
Then you just need to regenerate the CRL again, and it should work.

To regenerate the CRL file again, execute the following two commands in the easy_rsa directory:

source ./vars
openssl ca -gencrl -out "crl.pem" -config "$KEY_CONFIG"'



If the revoke command fails read this bug report...
https://bugs.launchpad.net/ubuntu/+source/openvpn/+bug/231199

Thursday, September 19, 2013

Update for my "IPTABLES for transparent Squid"

Ahoy! On my old post on TRANSPARENT SQUID there is an existing problem where https or port 443 can get through because SQUID does not cache SSL. Try visitng https://www.google.com from my old configuration and see that it can pass thru.

Here on my post I added this config from my previous post to block certain IP range or non-internet users group. Place the rule below the "DNAT port 80 request comming from LAN systems to squid 3128 ($SQUID_PORT) aka transparent proxy".

# Sample of IP range to be blocked
$ipt -t nat -A PREROUTING -i $LAN -m iprange --src-range 192.168.1.200-192.168.1.250 -p tcp --dport 443 -j DNAT --to 192.168.1.1:3128
$ipt -A FORWARD -p tcp -i $LAN -m iprange --src-range 192.168.1.200-192.168.1.250 --dport 443 -j REJECT

After this all https request from the IP range declared above will be rejected.

Note that If a certain application needs to connect to an SSL connection but the user is in the blocked list you will need to manually hand code the proxy IP to the client PC application and add to the SQUID rules the url that needs to be given access. This way the server will be the one giving SSL connection thru proxy and we can retain the transparent proxy configuration.

Friday, April 12, 2013

Reset NIC in Win7

I posted this because sometimes the NIC/LAN card does not work out of the box. The problem that I encountered is that Win7 says that there are no Internet connection on the network, though all are fine.
After a restart (disable and enable) of the NIC the Internet starts working.

I would like to do this automatically so I created a batch file and I name it "lanreset.bat". Below are the contents...

@echo off
wmic path win32_networkadapter where NetConnectionID="LAN" call disable
wmic path win32_networkadapter where NetConnectionID="LAN" call enable


Just replace the blue colored text with the name of the NIC.
Put it in the Task Scheduler where it would run in on startup so that it would execute.

Saturday, December 29, 2012

autoDial script for Windows

This is my personal script or batchfile because my connection is not that reliable or should I say the connection sometimes stops working. So I created this script from various sites to complete it.
Sorry I got lazy what sites are the sources but I just searched how to dial using command prompt.

This script runs in msdos shell and uses rasdial to control the dial up and there is an existing dial up account named Sun before creating this script. See the script I made below...

@echo off
:goagain

ping www.google.com | find "TTL"

if ERRORLEVEL 1 goto reconnect
if ERRORLEVEL 0 goto lineok

:reconnect
echo Disconnected
rasdial Sun /disconnect
rasdial Sun

:lineok
echo line ok
echo Waiting for 50 seconds before checking connection again...
PING 1.1.1.1 -n 50 -w 1000 >NUL
goto goagain


This script filename is autodial.bat

As the script goes the @echo off line hides the commands,
the  ping www.google.com | find "TTL" is to check if the connection gets a ping reply,

the rasdial [connection name] connects the internet
and adding /disconnect disconnects the internet.

the PING 1.1.1.1 -n 50 -w 1000 >NUL is the delay command where it will wait for 50 seconds before going to the next line.


Also this script goes on an endless loop so to stop the script just either close the command prompt window or press ctrl + c to cancel the script.


Hope this small script may help others in any way. Happy Holidays!!!