Re: Configure connection via command line [SOLVED]



Well, i have just solved my problem.
What I was trying to do was to connect to a wireless network using network manager without typing the wep/wpa key.
I did this using one bash script and a few python commands
Thanks Jirka for the answers :)

The first thing is to get the uuid of the connection you want to connect to

$ python

import sys
import os
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import gnomekeyring as gk

def list_uuids():
    bus = dbus.SystemBus()
    for c in get_connections():
        proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', c)
        iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
        settings = iface.GetSettings()
        conn = settings['connection']
        print "%s - %s (%s)" % (conn['uuid'], conn['id'], conn['type'])

(found on http://www.mail-archive.com/networkmanager-list gnome org/msg11871.html)
Note: every time you delete the configuration for a connection on network manager, a new uuid is assigned to it

This function lists all the connections uuid that network manager manages.
The next step is to create an entry on gnome-keyring that can be done with the python command


gk.item_create_sync('login', gk.ITEM_NETWORK_PASSWORD, 'Network secret for <your network name>/802-11-wireless-security/psk', {'connection-uuid': 'f63e5eae-2090-4639-a894-38f10006ccc9', 'setting-key':'psk', 'setting-name': '802-11-wireless-security'}, '<network key>', True)

'login' is the keyring name
gk.ITEM_NETWORK_PASSWORD is the type of the item being added
'Network secret for <your network name>/802-11-wireless-security/psk' is any string used to identify. I used the model seen on seahorse application.
{'connection-uuid': 'f63e5eae-2090-4639-a894-38f10006ccc9', 'setting-key':'psk', 'setting-name': '802-11-wireless-security'} are the attributes. As far as i noted, network manager uses connection-uuid to know if that connection already has a password stored.
<network key> is wep, wpa key to your network
'True' updates any other entry already existent

(based on http://blogs.codecommunity.org/mindbending/bending-gnome-keyring-with-python-part-2/)


After that, you can choose the connection on network manager and you wont be asked any key.
Or you can use this script to tell network manager which network it should connect (found on http://ubuntuforums.org/showthread.php?t=800330)

#!/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

Best regards,
Arlen Nascimento


On Thu, Jul 15, 2010 at 12:40 PM, Jirka Klimes <jklimes redhat com> wrote:
On Thursday 15 of July 2010 11:10:15 Arlen Nascimento wrote:
> Actually, i dont know if i made myself clear, let me try again.
> As the network will be available, it will appear on nm-applet, so i dont
> need to create the connection itself.
> I already have a bash script that controls nm-applet, e.g., connects to the
> network i say it to. The thing is, the final user will not have the network
> key, what i want is to make network manager (any instance of it) read the
> password from a file, so it wont be necessary to type it.
>

I just wanted to provide broader look on the theme as I didn't know what your
actual issue is.
Nevertheless, as I've said, the connection *should* store the password, so you
shouldn't need to type it again.
In case of using system connections, the plugins store the passwords in their
configuration files. User connections use gnome keyring to store passwords.
Check that the password is present in 'Wireless security' tab (Edit
connections ...). When connection is activated, you should see in NM logs
(/var/log/message, /var/log/daemon.log, etc.) something like this:

<info> Activation (wlan0/wireless): connection '<connection name>' has
security, and secrets exist.  No new secrets needed.

when secrets are correctly stored.
or
 <info> Activation (wlan0/wireless): access point '<connection name>' has
security, but secrets are required.

when secrets are not available.

So, could you be more specific and state what error or behaviour you see?
In case of user connection, check that you have gnome-keyring properly
installed?
And describe your environment:
- distro
- NM version
- do you use system or user connection?

Jirka



--
Arlen Nascimento



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]