home.


Tagged: wifi


Linux: Connect to open and closed networks from the command line with a single command.

You can easily connect to an open network though

iwconfig wlan0 essid "THE NETWORK NAME"

Closed networks work with wpa_supplicant. You normally send a configuration file with that but you can simulate the file with a named pipe:

wpa_supplicant -iwlan0 -c <(echo -e 'network={\n ssid="THE NETWORK NAME" \n psk="THE PASSWORD" \n}') -B

wlan0 is your network interface name. -B means go into the background. The \ns in the configuration file is needed sadly and so we pass -e to echo to interpret them.

iwconfig should show your new connection but prehaps only after a second.

After this dhclient wlan0 will get you a dhcp address and you’ll be ready to go. You may want to kill any prior instances of dhclient.

unix wifi

List the open wifi networks from the linux command line

If you want to use the command line and iwlist scan to list all the open wifi networks you can:

a=`iwlist wlan0 scan | grep 'ESSID\|key:' | sed 's/.*ESSID:"\(.*\)".*/\1/g' | sed 's/.*key:\(.*\).*/key:\1/g' | sed 's/\n//'`

This first lists all the networks and grabs the network name and whether there’s a key on this network.

This gives us a list of network’s key state and the network name.

Let’s now join the network key state and the network name into one line with sed 'N;... and swap the variables around to show the network name first.

echo "$a" | sed 'N;s/\n/,/' | awk -F ',' '{print $2" ("$1")"}'

Put these two lines in a bash script and run that to see all the networks and whether they’re open networks or not.

wifi

Unix: Connect to a hidden WIFI network with Debian from the command line

For some reason, putting a hidden WIFI network in /etc/network/interfaces doesn’t work – at least not if you do it the same way you do visible networks, perhaps.

However, you can do it without if-up and /etc/network/interfaces and get it working.

For example, with a WPA2 network, you can initially connect to a network via:

iwconfig wlan0 essid YOUR_HIDDEN_NETWORK key s:YOUR_PASSWORD

I encountered the below error, although it seems you can just ignore it.

Error for wireless request "Set Encode" (8B2A) :
    SET failed on device wlan0 ; Invalid argument.

From this point, iwconfig should show you’re connected to the ESSID, at least. Then run this to setup DHCP:

dhclient wlan0

And again, an error appears,RTNETLINK answers: File exists, which I found I can ignore, as it seems to be saying you’re overwriting your previous DHCP settings.

If you run, iwconfig you should see an ESSID and Access-Point etc and ifconfig should show you your IP.

And you should–hopefully–now be connected to the internet.

unix wifi

List names of wifi networks on the command line

This small script – to be run as the superuser or someone with permissions to run iwlist – will give you a list of the networks available.

iwlist wlan0 scan | grep 'ESSID' | sed 's/.*ESSID:"\(.*\)".*/\1/g'

Put it in a executable file in a bin directory.

Sometimes, you’ll get the message

wlan0     Interface doesn't support scanning : Device or resource busy

And this normally goes away eventually, retrying the command.

unix wifi

Page 1 of 1