Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [CONTRIBUTO] Ridondanza Router

    E' una piccola cavolata di script , ma cmq lo vogliore rendere pubblico casomai a qualcuno interessi e non gl iva di perdere tempo

    La situazione è un firewall che è appoggiato a 2 router , nel caso il link di uno di essi andasse giù , lui switcha sull'altro !!!

    codice:
    #!/bin/bash
    DEFAULT_GW=192.168.0.254 
    ALT_GW=192.168.0.253
    VALNULL=0
    PKT_RCV=`ping -q -c 5 -n 216.239.59.104 | awk '/packets transmitted/{print $4}'`
    DF_GW=`route | awk '/default/{print $2}'`
    
    if [ "$PKT_RCV" = "$VALNULL" ]; then
    echo " `date` || CRITICAL --- > Pacchetti ricevuti $PKT_RCV / Defualt Gw  $DEFAULT_GW " >> /usr/local/ha-gw/tmp/ha-gw.log
            route del default 
            if [ $DF_GW = $DEFAULT_GW ];  then
    # ERA GIa' il DEFAULT GW
                route add default gw $ALT_GW
            else
                route add default gw $DEFAULT_GW
            fi
    else
    #  OK NON SUCCEDE NULLA
        echo " `date` || OK !!!! --- > Pacchetti ricevuti $PKT_RCV / Defualt Gw  $DEFAULT_GW " >> /usr/local/ha-gw/tmp/ha-gw.log
        exit 0
    fi 
    
    exit 0
    I have tried all ... but the preferred remains SLACKWARE !
    RHCE (Linux Red Hat Certified Engineer)
    CNAC (Cisco Networking Academy Certified)
    "Non auro, sed ferro, recuperanda est patria"

  2. #2

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    2,894
    Molto bello...

    Thanks

  4. #4
    questo potrebbe essere solo uno spunto ... ma che ne dite di renderlo molto più consistente ... dai dai , aggiunte idee e parte di codice , farlo anche in c o in perl !!
    I have tried all ... but the preferred remains SLACKWARE !
    RHCE (Linux Red Hat Certified Engineer)
    CNAC (Cisco Networking Academy Certified)
    "Non auro, sed ferro, recuperanda est patria"

  5. #5
    Scusate se torno alla carica , ma ho perfezionato lo script facendolo totalemnte in perl e se lanciato con la & diventa un processo liberando la shell e demonizzarsi (seeeee me piacerebbe )

    codice:
    #!/usr/bin/perl -w > /dev/tty4
    use Net::Ping;
    
    $file = "demone.log";    ## nome del File Log Specificare la path 
    $t = 0;                  ## contatore
    $sec_delay=3;            ## delay prima del check gateway
    @hosts = ('192.168.99.40', '212.239.81.25'); ## hosts da controllare aggiungere quanti ne volete
    unlink $file;
    $GW_1="192.168.99.243";  ## Primo GW (default)
    $GW_2="192.168.99.242";  ## Gateway Alternativo
    
    
    
    ######## Funzione per il controllo del GW attualmente utilizzato
    sub change_gateway
        {
         $route_tmp = `route`;
         system(`route del default`);
         @array = split (/ /,$route_tmp);
         $default_GW = "$array[134]";
         if ($default_GW eq $GW_1)
            {
             system(`route add default gw 192.168.99.242`);        
            }
         if ($default_GW eq $GW_2)
            {
             system(`route add default gw 192.168.99.243`);        
            }
        else 
            {
             system(`route add default gw 192.168.99.242`);        
            }
         return $default_GW;
        }
    
    ######## Funzione PING 
    sub ping
        {
            $n_host = $#hosts;
            $ris = 0;
            for ($i = 0; $i <= $n_host;$i++)
                {
                    $p = Net::Ping->new("icmp");
                    $flag_ping=$p->ping("$hosts[$i]");
                    if ("$flag_ping" eq 0){$ris = $ris + 1;};
                    if ("$flag_ping" eq 1){$ris = 0;};
                    $p->close();
                }
         if ($ris == 0){$ris=1;}
         else {$ris=0;}
         return $ris;
        }
    
    
    ##### Linee di debug
    #print def_gateway();
    #printf ping();
    
    
    ################ CORE PROCESS ######################
    unlink $file;
    while (1)
    {
        $date = localtime;   ## Prendo la data del giorno
        sleep 1;             ## Delay di un secondo
        $t=$t+1;
        if ($t % $sec_delay == 0)  ## Razionalizzazione del tempo
        {
    
            if (ping() == 1){       ## HOST IS UP NESSUN CAMBIO
                 $res_ping="OK is UP";
                 open (FILE, ">>$file") || die ("non posso aprire il file");
                 print FILE "$t | $date |host di riferimento=@hosts -> $res_ping Nessun Cambio \n";
                 close (FILE);
                };
            if (ping() == 0){       ## HOST IS DOWN CAMBIO GW !!
                 open (FILE, ">>$file") || die ("non posso aprire il file");
                 $res_ping="CRITICAL is DOWN";
                 change_gateway();
                 print FILE "$t | $date |host di riferimento=@hosts -> $res_ping Cambio GW \n";
                 close (FILE);
                };
    
         };
    };
    exit 0
    I have tried all ... but the preferred remains SLACKWARE !
    RHCE (Linux Red Hat Certified Engineer)
    CNAC (Cisco Networking Academy Certified)
    "Non auro, sed ferro, recuperanda est patria"

  6. #6
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    2,894
    Mitico...

  7. #7
    versione 1.2 Apparentemente stabile ...



    #!/usr/bin/perl -w
    use Net::Ping;

    $file = "/var/log/demone.log"; ## nome del File Log Specificare la path
    $t = 0; ## contatore
    $sec_delay=60; ## delay prima del check gateway
    @hosts = ('192.168.99.40', '212.239.81.25'); ## hosts da controllare aggiungere quanti ne volete
    #unlink $file;
    $GW_1= "192.168.99.243"; ## Primo GW (default)
    $GW_2= "192.168.99.242" ; ## Gateway Alternativo





    ######## Funzione per il controllo del GW attualmente utilizzato
    sub change_gateway
    {
    $route_tmp = `route`;
    @array = split (/ /,$route_tmp);
    $default_GW = "$array[134]";
    sleep 1;
    system(`/etc/rc.d/rc.inet1 stop`);
    sleep 1;
    system(`/etc/rc.d/rc.inet1 `);
    sleep 1;
    system(`route delete default`);
    sleep 1;
    if ($default_GW eq $GW_1)
    {
    system(`route add default gw $GW_2`);
    printf " ENTRO 1 \n";
    }
    if ($default_GW eq $GW_2)
    {
    system(`route add default gw $GW_1`);
    printf " ENTRO 2 \n";
    }
    }

    ######## Funzione PING
    sub ping
    {
    $n_host = $#hosts;
    $ris = 0;
    for ($i = 0; $i <= $n_host;$i++)
    {
    $p = Net::Ping->new("icmp");
    $flag_ping=$p->ping("$hosts[$i]");
    if ("$flag_ping" eq 0){$ris = $ris + 1;};
    if ("$flag_ping" eq 1){$ris = 0;};
    $p->close();
    }
    if ($ris == 0){$ris=1;}
    else {$ris=0;}
    return $ris;
    }

    ################ CORE PROCESS ######################
    while (1)
    {
    $date = localtime; ## Prendo la data del giorno
    sleep 1; ## Delay di un secondo
    $t=$t+1;
    if ($t % $sec_delay == 0) ## Razionalizzazione del tempo
    {

    if (ping() == 1)
    { ## HOST IS UP NESSUN CAMBIO
    $res_ping="OK is UP";
    open (FILE, ">>$file") || die ("non posso aprire il file");
    print FILE "$date | host di riferimento=@hosts -> $res_ping Nessun Cambio \n";
    close (FILE);
    };
    if (ping() == 0)
    { ## HOST IS DOWN CAMBIO GW !!
    open (FILE, ">>$file") || die ("non posso aprire il file");
    $res_ping="CRITICAL is DOWN";
    change_gateway();
    print FILE "$date | host di riferimento=@hosts -> $res_ping Cambio GW \n";
    close (FILE);
    };

    };
    };
    exit 0
    I have tried all ... but the preferred remains SLACKWARE !
    RHCE (Linux Red Hat Certified Engineer)
    CNAC (Cisco Networking Academy Certified)
    "Non auro, sed ferro, recuperanda est patria"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.