How to connect to a WiFi from command line under Ubuntu without .conf file?

10

9

I can connect to my WiFi out of the shell by doing:

nano wireless-wpa.conf

typing:

ctrl_interface=/var/run/wpa_supplicant

network={
ssid="my_router_id" 
scan_ssid=1
key_mgmt=WPA-PSK
psk="1234567890"
}

and doing:

ifconfig eth1 down
iwconfig eth1 mode Managed
ifconfig eth1 up
killall wpa_supplicant
wpa_supplicant -B -Dwext -i eth1 -c ./wireless-wpa.conf -dd
dhclient eth1

Pretty complicated.. Is there a possibility to connect to a WiFI via shell without the need of a wireless-wpa.conf?

JohnnyFromBF

Posted 2011-09-29T05:24:02.590

Reputation: 4 298

Would having a script that wrote the .conf on the fly and established the network connection suffice? – Paul – 2011-09-29T05:58:49.100

Actually I was looking for a complete different solution, because I could write a bash script myself. – JohnnyFromBF – 2011-09-29T08:47:44.467

Answers

15

You can control a running wpa_supplicant using it's control interface, which you already specify in your .conf file. While this still needs a .conf file, you don't have to put any wireless networks in it, and don't have to change it. You can then configure it with wpa_cli.

wpa_cli may need to be told which wpa_supplicant instance and interface to configure:

wpa_cli -p /var/run/wpa_supplicant -i wlan0 command ...

For clarity, I'll use just wpa_cli here. Basically, you need to create a network, set its variables, and enable it:

# wpa_cli add_network
4                                          <--- note the network ID!
# wpa_cli set_network 4 ssid '"Your SSID"'
OK
# wpa_cli set_network 4 scan_ssid 1
OK
# wpa_cli set_network 4 key_mgmt WPA-PSK
OK
# wpa_cli set_network 4 psk '"1234567890"' <--- note the single quotes around
OK
# wpa_cli enable_network 4
OK

Ambroz Bizjak

Posted 2011-09-29T05:24:02.590

Reputation: 4 265

you might want to start with wpa_cli scan followed by wpa_cli scan_results to gather what SSID's are available – sibaz – 2015-12-15T12:56:26.063

The single quotes around the strings helped me, but what do you do when there is a single quote in the SSID? It doesn't seem to work. – JDavis – 2018-05-24T19:11:57.603

The single quotes trick helped me. How can I used a variable like psk=$3 instead? – typelogic – 2018-11-30T23:08:18.003

0

You want a cli command that manages your wpa_suplicant-config? Have you tried ifup, ifdown and ifcfg? They handle connection scripts and work for wifi too but may need some tinkering with.

micke

Posted 2011-09-29T05:24:02.590

Reputation: 3 001