Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: VPN auto-connect

  1. #1
    Join Date
    Jan 2005
    Beans
    24

    Question VPN auto-connect

    Gidday all,

    I connect to my work network from my laptop using network-manager's pptp VPN plugin. It works great.

    One small hassle I've been trying to solve for YEARS now is to find a way to make this connection automatically establish whenever an internet connection is working.

    If the angels were really singing it'd also be possible to try a "sudo mount -a" automatically when the VPN comes up, to mount the LAN drives.

    Even if it would just give it a try at boot time it'd be GREAT!

    Anyone solved this one? It seems to me that there might be a way of creating a couple of command line in System|Preference|Session to do the business...

    Cheers,

    Tony

  2. #2
    Join Date
    Nov 2009
    Location
    Moscow, RF
    Beans
    84
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: I *wish* I could make my VPN auto-connect!

    Well... if it is still useful I have figured a way to do this in Karmic.
    But it is qite a pain.
    1) Use seahorse to delete your default keyring password (null password.)
    2) make folowing script on your HD. Credit goes here http://forum.ubuntu.ru/index.php?top...8218#msg608218
    and here
    http://forum.ubuntu-fr.org/viewtopic...70479#p3170479
    Code:
    #!/bin/bash
    
    
        ############
        # SETTINGS #
        ############
    
    get_connections_paths()
    {
        dbus-send --system --print-reply --dest="$1" "/org/freedesktop/NetworkManagerSettings" "org.freedesktop.NetworkManagerSettings.ListConnections" \
        | grep "object path" | cut -d '"' -f2
    }
    
    get_connection_settings()
    {
        dbus-send --system --print-reply --dest="$1" "$2" org.freedesktop.NetworkManagerSettings.Connection.GetSettings
    }
    
    get_connection_string_setting()
    {
        echo "$1" | grep -A 1 \""$2"\" | grep variant | cut -d '"' -f2
    }
    
    get_connection_id()
    {
        get_connection_string_setting "$1" "id"
    }
    
    get_connection_type()
    {
        get_connection_string_setting "$1" "type"
    }
    
    get_device_type_by_connection_type()
    {
        echo "$1" | grep -q "ethernet" && echo 1 && return
        echo "$1" | grep -q "wireless" && echo 2 && return
        echo 0
    }
    
    find_connection_path()
    {
        for connection_path in `get_connections_paths "$1"`
        do
            connection_settings=`get_connection_settings "$1" "$connection_path"`
            connection_settings_id=`get_connection_id "$connection_settings"`
            [ "$connection_settings_id" = "$2" ] && echo "$1" "$connection_path"
        done
    }
    
    find_connection_path_everywhere()
    {
        find_connection_path "org.freedesktop.NetworkManagerSystemSettings" "$1"
        find_connection_path "org.freedesktop.NetworkManagerUserSettings" "$1"
    }
    
    print_connections_ids()
    {
        for connection_path in `get_connections_paths "$1"`
        do
            connection_settings=`get_connection_settings "$1" "$connection_path"`
            connection_settings_id=`get_connection_id "$connection_settings"`
            echo "$connection_settings_id"
        done
    }
    
    print_connections_ids_everywhere()
    {
        print_connections_ids "org.freedesktop.NetworkManagerSystemSettings"
        print_connections_ids "org.freedesktop.NetworkManagerUserSettings"
    }
    
    
        ###########
        # DEVICES #
        ###########
    
    get_devices_paths()
    {
        dbus-send --system --print-reply --dest="org.freedesktop.NetworkManager" "/org/freedesktop/NetworkManager" "org.freedesktop.NetworkManager.GetDevices" \
        | grep "object path" | cut -d '"' -f2
    }
    
    get_device_property()
    {
        dbus-send --system --print-reply --dest="org.freedesktop.NetworkManager" "$1" "org.freedesktop.DBus.Properties.Get" string:"org.freedesktop.NetworkManager.Device" string:"$2" \
        | grep variant | awk '{print $3}'
    }
    
    get_device_type()
    {
        get_device_property "$1" "DeviceType"
    }
    
    get_device_path_by_device_type()
    {
        device_path_by_device_type="/"
        for device_path in `get_devices_paths`
        do
            device_type=`get_device_type "$device_path"`
            [ "$device_type" = "$1" ] && device_path_by_device_type="$device_path"
        done
        echo "$device_path_by_device_type"
    }
    
    
        #######################
        # ACTIVES CONNECTIONS #
        #######################
    
    get_actives_connections_paths()
    {
        dbus-send --system --print-reply --dest="org.freedesktop.NetworkManager" "/org/freedesktop/NetworkManager" "org.freedesktop.DBus.Properties.Get" string:"org.freedesktop.NetworkManager" string:"ActiveConnections" \
        | grep "object path" | cut -d '"' -f2
    }
    
    get_last_active_connection_path()
    {
        get_actives_connections_paths | tail -n 1
    }
    
    get_parent_connection_path_by_device_type()
    {
        parent_connection_path="/"
        [ "$1" = 0 ] && parent_connection_path=`get_last_active_connection_path`
        echo "$parent_connection_path"
    }
    
    get_active_connection_property()
    {
        dbus-send --system --print-reply --dest="org.freedesktop.NetworkManager" "$1" "org.freedesktop.DBus.Properties.Get" string:"org.freedesktop.NetworkManager.Connection.Active" string:"$2" \
        | grep variant | awk -F '"' '{print $2}'
    }
    
    get_active_connection_service()
    {
        get_active_connection_property "$1" "ServiceName"
    }
    
    get_active_connection_path()
    {
        get_active_connection_property "$1" "Connection"
    }
    
    get_active_connection_path_by_connection_path()
    {
        for active_connection_path in `get_actives_connections_paths`
        do
            service=`get_active_connection_service $active_connection_path`
            path=`get_active_connection_path $active_connection_path`
            [ "$service" = "$1" ] && [ "$path" = "$2" ] && echo "$active_connection_path"
        done
    }
    
    print_actives_connections_ids()
    {
        for active_connection_path in `get_actives_connections_paths`
        do
            service=`get_active_connection_service $active_connection_path`
            path=`get_active_connection_path $active_connection_path`
            connection_settings=`get_connection_settings "$service" "$path"`
            connection_settings_id=`get_connection_id "$connection_settings"`
            echo "$connection_settings_id"
        done
    }
    
    
        ##############
        # START/STOP #
        ##############
    
    start_connection()
    {
        my_connection_complete_path=`find_connection_path_everywhere "$1"`
        my_connection_settings=`get_connection_settings $my_connection_complete_path`
        my_connection_type=`get_connection_type "$my_connection_settings"`
        my_connection_device_type=`get_device_type_by_connection_type "$my_connection_type"`
        
        my_connection_service=`echo $my_connection_complete_path | awk '{print $1}'`
        my_connection_path=`echo $my_connection_complete_path | awk '{print $2}'`
        my_connection_device_path=`get_device_path_by_device_type "$my_connection_device_type"`
        my_parent_connection_path=`get_parent_connection_path_by_device_type "$my_connection_device_type"`
        
        echo "connection_service=$my_connection_service"
        echo "connection_path=$my_connection_path"
        echo "connection_device_path=$my_connection_device_path"
        echo "parent_connection_path=$my_parent_connection_path"
        
        dbus-send --system --print-reply --dest="org.freedesktop.NetworkManager" /org/freedesktop/NetworkManager "org.freedesktop.NetworkManager.ActivateConnection" string:"$my_connection_service" objpath:"$my_connection_path" objpath:"$my_connection_device_path" objpath:"$my_parent_connection_path"
    }
    
    stop_connection()
    {
        my_connection_complete_path=`find_connection_path_everywhere "$1"`
        my_active_connection_path=`get_active_connection_path_by_connection_path $my_connection_complete_path`
        
        echo "active_connection_path=$my_active_connection_path"
        
        dbus-send --system --print-reply --dest="org.freedesktop.NetworkManager" /org/freedesktop/NetworkManager "org.freedesktop.NetworkManager.DeactivateConnection" objpath:"$my_active_connection_path"
    }
    
    
        ########
        # MAIN #
        ########
    
    invalid_arguments()
    {
        echo "Usage: `basename "$0"` connexion_name start|stop"
        echo "---Available Connections:"
        print_connections_ids_everywhere
        echo "---Active Connections:"
        print_actives_connections_ids
        exit 0
    }
    
    [ "$#" != 2 ] && invalid_arguments
    
    case "$2" in
        "start")
            start_connection "$1"
            ;;
        "stop")
            stop_connection "$1"
            ;;
        *)
            invalid_arguments
            ;;
    esac
    name this script nmcli

    After that, make it executable as a command
    Code:
    sudo cp nmcli /usr/local/bin/
    sudo chmod ugo+x /usr/local/bin/nmcli
    3) create a script in your home folder
    Code:
    #!/bin/bash
    sleep 25
    nmcli VPNNAME start
    (do not forget to make it executable by using chmod 777)
    where VPNNAME is the name of your connection
    sleep time is needed for all interfaces to self-configure before vpn starts. For example, I get VPN error if sleep is less than 15.
    4) Add this script to system->settings->sessions preferences->startup programs.
    5)Enjoy it on reboot^^
    6) BTW You can add "sudo mount -a" in the end of script at step 3. (Not forgetting to add some more sleep time before command).

    NOTE: This works on my system with autologin. Without autologin, some steps may need to be changed.
    Last edited by Cabalbl4; March 24th, 2010 at 09:01 AM. Reason: Forgot to add a necessary step in guide

  3. #3
    Join Date
    Nov 2009
    Location
    Moscow, RF
    Beans
    84
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: I *wish* I could make my VPN auto-connect!

    Yay!!! I have figured a way to autoconnect it!!!

    My dear ISP sometimes resets the connection, and it is not nice when I am not at home.

    But now I can check the connection for availability and, if needed, start it.
    To do this, just follow my previous post until step 3, and, when at step 3, use the following script instead of given one (or, You may use them both, as the previous one is executed only once):
    Code:
    #!/bin/bash
    
    for (( ; ; )); do
    
    # creating ifinite loop
    
    tested=$(nmcli|grep -c VPNNAME)
    # VPNNAME here is the name of desired vpn connection to monitor. The results can be:
    # 0 - this connection does not exist
    # 1 - connection exists, but not active
    # 2 - connection exists, and is active
    case $tested in
    "0")
    echo "something is wrong, cannot find desired connection in avaliable list"
    ;;
    "1")
    echo "VPN avaliable, but not connected" 
    nmcli VPNNAME start
    ;;
    "2")
    echo "VPN seems to work" 
    ;;
    esac
    # enter desired time between checks here.
    sleep 30 
    done
    This script auto-checks VPN availability using nmcli from previous post, in a constant period of time.

    do not forget to make it executable and add it to autorun

    Works fine for me ^^

    PS. However, this script requires a constant connection to lan, and it can only monitor the VPN connection status. It cannot do much good if the lan interface is down.
    Last edited by Cabalbl4; April 5th, 2010 at 07:49 PM.

  4. #4
    Join Date
    Jan 2008
    Beans
    16

    Re: I *wish* I could make my VPN auto-connect!

    Fantastic! Just what I was looking for. I had tried using "pon" and "poff" with pptp-linux, but I could get a stable connection to the VPN, whilst the NetworkManager connection was very stable. Now I can schedule when the VPN is up or down. Don't forget to set the DISPLAY variable if you want to run from cron.

  5. #5
    Join Date
    Jul 2008
    Beans
    6

    Re: I *wish* I could make my VPN auto-connect!

    wow... awesome! Just what I was looking for soooo much. Now let's see if a Noob like me can get it to work...

    thanks a lot!

  6. #6
    Join Date
    Apr 2006
    Beans
    78

    Re: I *wish* I could make my VPN auto-connect!

    Am struggling to get this to work when I try and manually start my connection from the command line I get the following:

    paul@IBM-Frontend:~$ nmcli VPNUK start
    bash: /usr/local/bin/nmcli: /bin/bash^M: bad interpreter: No such file or directory

    Any help much appreciated.

  7. #7
    Join Date
    Jan 2008
    Beans
    16

    Re: I *wish* I could make my VPN auto-connect!

    Quote Originally Posted by ptipton View Post
    Am struggling to get this to work when I try and manually start my connection from the command line I get the following:

    paul@IBM-Frontend:~$ nmcli VPNUK start
    bash: /usr/local/bin/nmcli: /bin/bash^M: bad interpreter: No such file or directory

    Any help much appreciated.
    Looks like you're editing the file in Windows. You have carriage return character in there (^M). You need to remove all the ^M characters from your script.

  8. #8
    Join Date
    Apr 2006
    Beans
    78

    Re: I *wish* I could make my VPN auto-connect!

    Quote Originally Posted by red_lego_man View Post
    Looks like you're editing the file in Windows. You have carriage return character in there (^M). You need to remove all the ^M characters from your script.
    Worked a treat. Thanks!!

  9. #9
    Join Date
    Mar 2007
    Beans
    35

    Re: I *wish* I could make my VPN auto-connect!

    Thanks! I had to mod it to get it working in Ubuntu 10.10 Maverick Meerkat.

    Seems to wor for me now.

    Code:
    #!/bin/bash
    VPNNAME=VPN1
    # for ((;;)); do
    while true; do
    
    # creating ifinite loop
    
    tested=$(nmcli con status|grep -c $VPNNAME)
    
    date
    case $tested in
    "0")
    echo "not connected, försöker ansluta"
    nmcli con up id "VPN1"
    ;;
    "1")
    echo "VPN seems to work" 
    #nmcli $VPNNAME start
    ;;
    "2") # newer get here
    echo "VPN seems to work" 
    ;;
    esac
    # enter desired time between checks here.
    sleep 30
    
    done

  10. #10
    Join Date
    Jun 2008
    Beans
    503

    Re: I *wish* I could make my VPN auto-connect!

    Excellent stuff! Very good.

    The 2nd reconnection script doesnt work in Meerkat but the original one does.
    Last edited by sonicb00m; October 3rd, 2010 at 04:09 PM.

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •