• # script pour openvpn dual VPN + SSH

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

    Je reposte ici histoire de ne pas recréer un enième thread (d'où me vient l'idée de ce code :D)

    Donc voici un bout de code dont je ne comprends pas bien le problème.
    Ce script est censé quitter openvpn et autossh s'ils sont lancé, ou les lancer s'ils sont quitté. Le script est conçu pour pouvoir changer de serveur VPN en fonction de si le serveur VPN1 est présent ou non sur le LAN. Il est aussi conçu pour ne pas fermer les autres éventuels tunnels SSH

    #!/bin/bash
    # -> WTFPL - infos script : https://www.0rion.netlib.re/forum4/viewtopic.php?f=84&t=646
    # -> code by voxdemonix <-
    # -> V1.2 (2017年07月25日) <-
    #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'
    if [ ! "$SUDO_USER" ]; then
    echo "!!! i need root !!!"
    exit 0
    fi
     # NETWORK OPTIONS
    INTERFACE="tun"
    IpVpnLocale="192.168.1.69" # (VPN1) l'adresse IP locale de votre serveur hébergeant owncloud
    MacVpnLocal="69:69:69:69:69:69" #l'adresse mac du serveur hébergeant owncloud (tapez ifconfig dans un terminal sur votre server pour la voir)
     # VPN OPTIONS
    fichierOvpnLAN="/etc/openvpn/userVPN1.ovpn" # (VPN1) le nom de votre fichier de conf .ovnp en LAN
    fichierOvpnNotLAN="/etc/openvpn/userVPN2.ovpn" # (VPN2) le nom de votre fichier de conf .ovnp 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)
    LANG="fr"
     # SSH OPTIONS
    AdresseServerOnion="blablablablablabla.onion" # (VPN2)
    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
    case $LANG in
     fr) VpnDisabled="Connexion VPN fermé"
     VpnConnecting="Connexion VPN en cours"
     VpnEnabled="VPN Connecté !";;
     en | usa | *) VpnDisabled="VPN Connexion Disabled"
     VpnConnecting="VPN Connexion"
     VpnEnabled="VPN Connected !";;
    esac
     # boucle qui permet d'attendre que le réseau soit connecté
    stop="0"
    while [ $stop -lt 1 ]
    do
    sleep 20 # 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
     if ps -aux | grep "[o]penvpn" | grep "$fichierOvpnLAN"; then
     # LAN
     for pid in `ps -ef | grep "[o]penvpn" | grep "$fichierOvpnLAN" | awk '{print 2ドル}'` ; do kill $pid ; done
     hostnameVPN=$(cat "$fichierOvpnLAN" | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     notify-send "$VpnDisabled ($hostnameVPN)"
     # if you use SSH in LAN, uncomment following line
     # si vous utilisez SSH en LAN, décommentez les lignes suivantes
     for pid in `ps -ef | grep "[a]utossh" | grep "$IpVpnLocale" | awk '{print 2ドル}'` ; do kill $pid ; done
     notify-send "SSH Tunneling STOP ($IpVpnLocale)"
     fi
     if ps -aux | grep "[o]penvpn" | grep "$fichierOvpnNotLAN"; then
     # WAN
     for pid in `ps -ef | grep "[o]penvpn" | grep "$fichierOvpnNotLAN" | awk '{print 2ドル}'` ; do kill $pid ; done
     hostnameVPN=$(cat "$fichierOvpnNotLAN" | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     notify-send "$VpnDisabled ($hostnameVPN)"
     for pid in `ps -ef | grep "[a]utossh" | grep "$AdresseServerOnion" | awk '{print 2ドル}'` ; do kill $pid ; done
     notify-send "SSH Tunneling STOP ($AdresseServerOnion)"
     fi
    else
     ping $IpVpnLocale -c 2 >> /dev/null 2>&1
     macRecover=$(arp -n | grep -i -o $MacVpnLocal)
     if [ "$macRecover" == "$MacVpnLocal" ]; then
     # echo "local/LAN"
     # si vous voulez aussi faire un tunnel ssh en LAN décommentez les deux lignes suivantes
     # for pid in `ps -ef | grep "[a]utossh" | grep "$IpVpnLocale" | awk '{print 2ドル}'` ; do kill $pid ; done
     # su $UserLocalForSshTunneling -c "autossh -M 0 -q -N -o 'ServerAliveInterval 60' -o 'ServerAliveCountMax 3' -L $portEntree:127.0.0.1:$portSortie $UserRemoteForSshTunneling@$IpVpnLocale -f"
     openvpn --daemon $openVpn1Param --config $fichierOvpnLAN
     hostnameVPN=$(cat "$fichierOvpnLAN" | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     notify-send "$VpnConnecting ($hostnameVPN)"
     else
     # echo "tor/wan"
     # kill oprevious ssh tunneling
     for pid in `ps -ef | grep "[a]utossh" | grep "$AdresseServerOnion" | 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@$AdresseServerOnion -f"
     openvpn --daemon $openVpn2Param --config $fichierOvpnNotLAN
     hostnameVPN=$(cat "$fichierOvpnNotLAN" | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
     notify-send "$VpnConnecting ($hostnameVPN)"
     fi
     # 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
     notify-send "$VpnEnabled ($hostnameVPN)" 
     stop=10
     exit
     fi
     sleep 20
     stop=$((stop+1))
     done
    fi

    Il y a une erreur quelque part avant la ligne "if [ "$macRecover" == "$MacVpnLocal" ];" car cette dernière fonctionne dans mes scripts actuels hors maintenant elle renvoie "./test.sh: 86: [: 69:69:69:69:69:69: unexpected operator" (l'adresse mac est censurée par mes soins, dans le script elle est bonne).
    Je soupçonne les deux if qui gèrent la coupure d'openvpn et d'autossh d'être à l'origine du soucis.

    PS: bash est vraiment horrible :D

    Donation Bitcoin : 1N8QGrhJGWdZNQNSspm3rSGjtXaXv9Ngat