• [^] # Re: script pour openvpn dual VPN + SSH

    Posté par . En réponse au message script lancement openvpn. Évalué à 1. Dernière modification le 26 juillet 2017 à 18:13.

    cela vous convient-il mister? :P

    #!/bin/bash
    # -> WTFPL - infos script : https://www.0rion.netlib.re/forum4/viewtopic.php?f=9&t=646&p=1460#p1460
    # -> code by voxdemonix <-
    # -> V1.6 (2017年07月26日) <-
    #su monUser -c 'autossh -M 0 -q -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -L 8080:127.0.0.1:80 proxy-ssh@torOrWanadress.onion -f'
    # uncomment next line for debug
    #set -x
    if [ ! "$SUDO_USER" ]; then
    echo "!!! i need root !!!"
    exit 0
    fi
     #SCRIPTS OPTIONS
    LANG="fr" # notification service ; accept : fr, usa (default)
    notificationType="notify" # accept : echo; notify, nothing
     # NETWORK OPTIONS
    INTERFACE="tun"
    hosnameVpnLAN="192.168.1.69" # (VPN1 hostname) l'adresse IP locale de votre serveur VPN
    MacVpnLocal="69:69:69:69:69:69" #l'adresse mac du serveur VPN (tapez ifconfig dans un terminal sur votre server pour la voir)
     # VPN OPTIONS
    fichierOvpnLAN="/etc/openvpn/userVPN1.ovpn" # (VPN1) le PATH de votre fichier de conf .ovpn en LAN
    fichierOvpnNotLAN="/etc/openvpn/userVPN2.ovpn" # (VPN2) le PATH de votre fichier de conf .ovpn via SSH
    openVpn1Param="--route-nopull" # add options for openvpn connexion to server 1 (lan)
    openVpn2Param="--route-nopull" # add options for openvpn connexion to server 2 (ssh/tor/wan)
     # SSH OPTIONS
    hosnameVpnNotLAN="hostname.onion" # (VPN2 hostname)
    UserLocalForSshTunneling="daenerys" # l'user a utiliser sur votre ordinateur pour le montage du tunnel (celui qui a exporté sa clés)
    UserRemoteForSshTunneling="proxy-ssh" # l'user a utiliser côté server ( /!\ n'utilisez jamais root !)
    portEntree="1194" # le port sur le pc local, par défaut 1194
    portSortie="1194" # le port sur le serveur, par défaut 1194
    sshLanEnable=0 # 0 for disable ; 1 for enable : enable or disable the ssh tunneling in LAN
    sshNotLanEnable=1 # 0 for disable ; 1 for enable : enable or disable the ssh tunneling in Wan | Tor
     # NOTIFICATION SYSTEM
    function vpnNotification {
     # 1ドル = message ID (watch switch/case);
     # 2ドル = hostnameVPN
     message=""
     #hostnameVPN=$(cat "2ドル" | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     hostnameVPN=2ドル
     case $LANG in
     fr )
     case 1ドル in
     VpnDisabled )
     message="Connexion VPN fermée ($hostnameVPN)"
     ;;
     VpnConnecting )
     message="Connexion VPN en cours ($hostnameVPN)"
     ;;
     VpnEnabled )
     message="VPN Connecté ! ($hostnameVPN)"
     ;;
     SshStop )
     message="Tunnel SSH STOP ($hostnameVPN)"
     ;;
     SshStart )
     message="Tunnel SSH START ($hostnameVPN)"
     ;;
     esac
     ;;
     usa | *)
     case 1ドル in
     VpnDisabled )
     message="VPN Connection Close ($hostnameVPN)"
     ;;
     VpnConnecting )
     message="VPN Connection in progress ($hostnameVPN)"
     ;;
     VpnEnabled )
     message="VPN Connected ! ($hostnameVPN)"
     ;;
     SshStop )
     message="SSH Tunneling STOP ($hostnameVPN)"
     ;;
     SshStart )
     message="SSH Tunneling START ($hostnameVPN)"
     ;;
     esac
     ;;
     esac
     case $notificationType in
     echo)
     echo "$message"
     ;;
     notify)
     notify-send "$message"
     ;;
     nothing | *)
     ;;
     esac
    }
    function quitOpenVPN {
     # 1ドル = ovpn ( $fichierOvpnLAN, $fichierOvpnNotLAN)
     # 2ドル = hostnameVPN ( $hosnameVpnLAN, $hosnameVpnNotLAN)
     ovpn=1ドル
     hostVPN=2ドル
     #stop VPN
     if ps -aux | grep "[o]penvpn" | grep "$ovpn" | grep -v "grep" ; then
     # LAN
     for pid in `ps -ef | grep "[o]penvpn"| grep "$ovpn" | grep -v "grep" | awk '{print 2ドル}'` ; do kill $pid ; done
     hostnameVPN=$(cat $ovpn | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     vpnNotification "VpnDisabled" $hostnameVPN
     fi
     #stop ssh
     if ps -aux | grep "[a]utossh" | grep "$hostVPN" | grep -v "grep" ; then
     for pid in `ps -ef | grep "[a]utossh" | grep "$hostVPN" | grep -v "grep" | awk '{print 2ドル}'` ; do kill $pid ; done
     vpnNotification "SshStop" $hostVPN
     fi
     sleep 1
    }
    function startOpenVPN {
     # 1ドル = hostname VPN ($hosnameVpnLAN, $hosnameVpnNotLAN)
     # 2ドル = ovpn ($fichierOvpnLAN, $fichierOvpnNotLAN)
     # 3ドル = ssh enable (1) or not (0) ($sshLanEnable, $sshNotLanEnable)
     # 4ドル = OpenVpn Param ($openVpn1Param, $openVpn2Param)
     hostVPN=1ドル
     ovpn=2ドル
     sshChoice=3ドル
     openvpnParam=4ドル
     # SSH TUNNELING
     if [ $sshChoice == 1 ]; then
     # kill oprevious ssh tunneling
     for pid in `ps -ef | grep "[a]utossh" | grep "$hostVPN" | awk '{print 2ドル}'` ; do kill $pid ; done
     # creat new ssh tunneling
     su $UserLocalForSshTunneling -c "autossh -M 0 -q -N -o 'ServerAliveInterval 60' -o 'ServerAliveCountMax 3' -L $portEntree:127.0.0.1:$portSortie$UserRemoteForSshTunneling@$hosnameVpnNotLAN -f"
     fi
     # START OpenVPN
     openvpn --daemon $openvpnParam --config $ovpn
     # NOTIFICATIONS
     hostnameVPN=$(cat $ovpn | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     vpnNotification "VpnConnecting" $hostnameVPN
     # boucle qui permet de verifier si le VPN se connecte bien
     stop=0
     while [ $stop -lt 10 ]
     do # temps d'attente entre chaque tentative
     if ifconfig | grep -iq $INTERFACE; then
     vpnNotification "VpnEnabled" $hostnameVPN 
     stop=10
     exit
     fi
     sleep 5
     stop=$((stop+1))
     done
    }
     # boucle qui permet d'attendre que le réseau soit connecté
    stop="0"
    while [ $stop -lt 1 ]https://www.0rion.netlib.re/forum4/viewtopic.php?f=9&t=646&p=1460#p1460
    do
    sleep 3 # temps d'attente entre chaque tentative
    _IP=$(hostname -I) || true
    if [ "$_IP" ]; then
     stop=1
    fi
    done
    if ps -aux | grep "[o]penvpn"; then
     #Connexion VPN active
     # vpn server 1
     quitOpenVPN $fichierOvpnLAN $hosnameVpnLAN
     # vpn server 2
     quitOpenVPN $fichierOvpnNotLAN $hosnameVpnNotLAN
    else
     ping $hosnameVpnLAN -c 3 >> /dev/null 2>&1
     macRecover=$(arp -n | grep -i -o $MacVpnLocal)
     if [ "$macRecover" === "$MacVpnLocal" ]; then
     # echo "local/LAN/First VPN"
     startOpenVPN $hosnameVpnLAN $fichierOvpnLAN $sshLanEnable $openVpn1Param
     else
     # echo "tor/wan/second VPN"
     startOpenVPN $hosnameVpnNotLAN $fichierOvpnNotLAN $sshNotLanEnable $openVpn2Param
     fi
    fi

    source : https://www.0rion.netlib.re/forum4/viewtopic.php?f=9&t=646&p=1460#p1460

    PS: c'est chiant que la moulinette doit se situer après les fonctions
    PS2 : si tu as une idée de petit nom pour ce script (je n'arrive pas a lui trouver un nom simple, compréhensible et bilingue (usa/fr) genre "script pour automatiser choix entre 2 serveur VPN suivant sur quel réseau on est connecté" :D ) Je posterais le tuto sur LinuxFR quand j'aurai trouvé comment titré le wiki :D
    PS3 : il y a une étrangeté avec, malgré grep "[o]penvpn", je suis quand même obligé de mettre grep -v "grep" si non la commande qui quitte openvpn match le grep (ca ne provoque pas de bug mais ça provoque l'affiche d'une ligne où le script dis ne pas avoir réussi a quitter le process de grep :D )

    Donation Bitcoin : 1N8QGrhJGWdZNQNSspm3rSGjtXaXv9Ngat