• # SCRIPT COMPLET

    Posté par . En réponse au message Le plus petit. Évalué à 1.

    Pour ceux que ça intéresserait, voici le script complet.

    Je sais qu'il pourrait être nettoyé, optimisé et qu'on pourrait même y ajouter certaines fonctions. Mais pour l'instant c'est le mieux que je puisse faire !

    Le script à pour but de connecter automatiquement un serveur OpenVPN au démarrage d'une session Linux à condition de de l'avoir ajouté à gnome-session-properties, ou d'avoir créé un lanceur .desktop dans /etc/xdg/autostart/, ou encore de l'avoir placé dans /etc/NetworkManager/Dispatcher.d/ (dans ce dernier cas aucune déconnexion sera possible, et les notifications ne s'afficheront pas).

    Voici le code, originalement créé pour la communauté Freedom-Ip qui offre un service OpenVPN gratuit.

    Mais attention ! Ce script cherche les valeurs freedomip retournées par le NerworkManager d'Ubuntu, pensez à le modifier en conséquence ;)

    Fonctions du script:

    • Cherche l'interface réseau active (limité à wlan0 & eth0)
    • Vérifie que la connexion internet soit bien opérationnelle via ping, sinon stop l'opération
    • Récupère l'occupation des serveurs et détermine le moins occupé via wget / http (à adapter en fonction de votre fournisseur!)
    • Vérifie qu'aucune connexion VPN (ici à Freedom-IP) ne soit active. Si c'est le cas, déconnecte puis reconnecte au serveur le moins occupé
    • Sinon connecte tout simplement le serveur le moins occupé, à condition qu'une interface réseau (wlan0 ou eth0) et qu'aucun VPN Freedom-IP ne soient actifs
    • Retours terminal étape par étape
    • Retours de notifications graphiques via libnotify : Pas d'internet, VPN déjà connecté, Serveur le moins occupé, Reconnexion, Connexion, Erreur de connexion

    Script:

    #!/bin/bash
    #
    # FREEDOM-IP AUTO CONNECT SCRIPT
    #
    # DÉPENDANCES: nmcli ping wget openvpn network-manager-openvpn network-manager-openvpn-gnome openssl libnotify
    ETH_DEV="eth0"
    WLN_DEV="wlan0"
    # ANALYSE INTERFACE RÉSEAU ACTIVE
    echo
    echo ----- ANALYSE INTERFACE RÉSEAU
    NET_STAT=$(nmcli con status | egrep "${ETH_DEV}|${WLN_DEV}")
    echo
    echo $NET_STAT est la connexion active
    # VÉRIFICATION D'ÉTABLISSEMENT COMPLET DE LA CONNECTIVITÉ INTERNET
    echo
    echo ----- VÉRIFICATION INTERNET...
    PING=$(ping -c 3 vpn2.freedom-ip.com | grep received | cut -d ',' -f2 | cut -d ' ' -f2)
    echo
    echo $PING sur 3 paquets reçus depuis vpn2.freedom-ip.com
    if [ -z $PING ]; then
     echo
     echo Pas de connexion Internet fonctionnelle 
     notify-send 'Freedom-IP:' 'Pas de connexion Internet fonctionnelle' -i dialog-error
     exit
    fi
    # OCCUPATION DES SERVEURS
    echo
    echo ----- OCCUPATION DES SERVEURS
    wget --no-check-certificate -O /tmp/fip_occupation https://freedom-ip.com/statistiques.php
    SERV_LOAD=$(grep personnes /tmp/fip_occupation | cut -d '<' -f2 | cut -d '"' -f2)
    echo
    echo $SERV_LOAD
    SERV_MIN_LOAD=$(awk '{ for (i=1; i<=NF; i++) { if (i%5==1) { nb=$i } if (i%5==0) { print nb" "$i } } }' < <(grep personnes /tmp/fip_occupation | cut -d '<' -f2 | cut -d '"' -f2 | sort -n) | cut -d ' ' -f2 | cut -d '
    ' -f1)
    echo
    echo $SERV_MIN_LOAD est le moins occupé, sélection pour connexion
    notify-send "Occupation des serveurs" "$SERV_MIN_LOAD est le moins occupé, sélection pour connexion"
    # VÉRIFICATION D'UNE CONNEXION AU VPN DÉJÀ EXISTANTE
    echo
    echo ----- VERIFICATION
    VPN_STAT=$(nmcli con status | grep freedomip | cut -d\ -f 1)
    if [ -z "${VPN_STAT}" ]; then
    echo
    echo Pas de connexion VPN active
    notify-send 'Freedom-IP:' 'Établissement de la connexion sécurisée...' -i dialog-warning ; else
    echo
    echo Déjà connecté à $VPN_STAT - Redémarrage avec $SERV_MIN_LOAD
    notify-send "Déjà connecté à $VPN_STAT" "Redémarrage avec $SERV_MIN_LOAD" -i dialog-warning
    echo
    echo ----- DÉCONNEXION DE $VPN_STAT
    nmcli con down id "${VPN_STAT}"
    echo
    echo ----- RECONNEXION À $SERV_MIN_LOAD
    echo
    RCON=$(nmcli con up id $SERV_MIN_LOAD'_freedomip' | grep "Connexion activée")
    echo $RCON
     if [ "$RCON" = "Connexion activée" ]; then
     sleep 3
     notify-send "Connexion sécurisée établie au server $SERV_MIN_LOAD, surfez tranquile :-)" -i dialog-information ; else
     echo
     echo Impossible de se connecter à $SERV_MIN_LOAD 
     notify-send 'Freedom-IP:' "Impossible de se connecter à $SERV_MIN_LOAD" -i dialog-error
     fi
    echo
    echo ----- FIN DE SCRIPT
    exit
    fi
    # CONNECTION AU VPN SI INTERNET CONNECTÉ
    if [ $PING -eq 3 ]; then
     echo
     echo ----- CONNEXION À $SERV_MIN_LOAD
     echo
     if [ ! -z "${NET_STAT}" -a -z "${VPN_STAT}" ]; then
     CON=$(nmcli con up id $SERV_MIN_LOAD'_freedomip' | grep "Connexion activée")
     echo $CON
     fi
    fi
     if [ "$CON" = "Connexion activée" ]; then
     sleep 3
     notify-send "Connexion sécurisée établie au server $SERV_MIN_LOAD, surfez tranquile :-)" -i dialog-information ; else
     echo
     echo Impossible de se connecter à $SERV_MIN_LOAD 
     notify-send 'Freedom-IP:' "Impossible de se connecter à $SERV_MIN_LOAD" -i dialog-error
     fi
    echo
    echo ----- FIN DE SCRIPT
    exit
    
    

    Encore un grand merci à vous tous pour votre aide précieuse ;)