Ubuntu – Connecting to wireless networks from command line

command linewirelesswpa-supplicant

I need to write a shell script which connects to one of the two available wi-fi connections. One is a un secure connection and the other is secure connection.
My question has 2 parts-

  1. How to connect to the un-secure (un-encrypted and no password required) connection from command line (or by executing a shell script) when I'm connected to the secure connection?

    I followed the steps in http://www.ubuntugeek.com/how-to-troubleshoot-wireless-network-connection-in-ubuntu.html for in-secure connection. I put all the commands in a script and executed it (I made sure that interface name and essid are correct)

    sudo dhclient -r wlan0
    sudo ifconfig wlan0 up
    sudo iwconfig wlan0 essid "UAPublic"
    sudo iwconfig wlan0 mode Managed
    sudo dhclient wlan0
    

    But nothing happens: I'm not disconnected from the current network and connected to the new one.

  2. When I want to connect to the secure wi-fi network, I understand from https://askubuntu.com/a/138476/70665 that I need to use wpa_supplicant.
    But I enter a lot of details in the interface when I connect via UI

    • security : wpa and wpa2 enterprise
    • Authentication : PEAP
    • CA certificate : Equifax…
    • PEAP version : automatic
    • inner authentication : MSCHAPv2
    • username :
    • password :

How to use wpa_supplicant to mention all these details in the command line? The conf file

network={
        ssid="ssid_name"
        psk="password"
}

doesn't work for me.

Best Answer

I'm assuming you're using Ubuntu Desktop as you didn't specify otherwise.

You can create a wireless network configuration that will be managed by Network Manager by creating files in /etc/NetworkManager/system-connections. Look at existing files to see how the format looks like.

Once the connection is created, you can use the nmcli command to manage NetworkManager from the command line, doing things such as enabling, disabling and querying connections.

Incidentally, the System Testing tool (checkbox) has a script that does exactly this: creates a connection and enables it, with parameters that you supply on the command line.

For instance, this creates a connection to the open "duck" network:

sudo /usr/share/checkbox/scripts/create_connection duck

This will create a connection to a network using WPA2 security, with "wings" password:

sudo /usr/share/checkbox/scripts/create_connection -S wpa -K wings duck

The script is written in Python so it should be easy for you to look at and adapt to your needs.

The script's help says this:

Usage: create_connection [options] SSID

Options:
  -h, --help            show this help message and exit
  -S SECURITY, --security=SECURITY
                        The type of security to be used by the connection.
                        One of wpa and wep. No security will be used if
                        nothing is specified.
  -K KEY, --key=KEY     The encryption key required by the router.
  -U UUID, --uuid=UUID  The uuid to assign to the connection for use by
                        NetworkManager. One will be generated if not
                        specified here.
  -R RETRIES, --retries=RETRIES
                        The number of times to attempt bringing up the
                        connection until it is confirmed as active.
  -I INTERVAL, --interval=INTERVAL
                        The time to wait between attempts to detect the
                        registration of the connection.