Use Network-Manager to connect to a WiFi Access Point on the command-line

Solution 1:

You can do this directly using nmcli, even if the connection isn't configured in Network Manager yet:

nmcli dev wifi con "myssid" password "myssidpassword"

If you don't want to have the wireless password stored in your history in recent versions (maybe newer than mentioned below) you can add --ask to have nmcli prompt you for missing information it needs.

nmcli --ask device wifi connnect "myssid"

This facility was added in Network Manager 0.9.6, so this only works in Ubuntu 12.10 or later.

You can also get a list of available access points with:

nmcli dev wifi list

Solution 2:

You can use cnetworkmanager for this.

Edit:

After some investigation it becomes clear that it is impossible to run two "user configuration providers" (an application listening at a certain dbus address) at the same time, and cnetworkmanager needs to act as such a "configuration provider" (at least until the connection is set up).

Now, what you can do is add the information for the connection to gconf under the /system/networking/connections key (you can do that from the commandline or a script), and then use dbus or a command-line tool like cnetworkmanager or nmcli to connect to it. Maybe you'll also have to add the key to the user's keyring first (to avoid password popups).

But of course that won't work on a desktop that uses another NM-client than nm-applet (e.g. KDE). You would have to figure out how to provide a config for them too...

Edit 2:

It seems like it might be possible via org.freedesktop.NetworkManagerUserSettings on the system DBus, but this is all poorly documented IMO...

You might want to read about configuration settings and the DBus interface spec.

And here is a relevant example (maybe you want to set up a user connection instead of a system connection though)

Solution 3:

You can use iwconfig (wifitools) for wep encryption, or wpa_supplicant for wpa and wep, also you use wicd-curses if you're looking for something easier, good luck =)