1

I'm currently programming two scripts, one to connect to a wifi network, the other one to disconnect.

The connect script works well, however when I'm do

sudo ifconfig wlan0 down
sudo ifconfig wlan0 up

The interface automatically reconnect to the network even if there is nothing about wlan0 in /etc/network/interfaces.

However if I reboot, wlan0 is not connected.

Here is my script for those you are interested in

#!/bin/bash

SSID="ssid"
PASSWORD="pass"

CONF_FILE="/etc/wpa_supplicant/$SSID.conf"

if [ -f "$CONF_FILE" ]; then
    echo "wpa configuration file already exists"
else
    echo "wpa configuration file doesn't exists"
    #Generating configuration file
    sudo wpa_passphrase $SSID $PASSWORD > $CONF_FILE
    #Delete the line containing the password
    sudo sed -i '/$PASSWORD/d' $CONF_FILE
fi

#Connecting to network
sudo wpa_supplicant -c $CONF_FILE -i wlan0 &

I think it's because wpa_supplicant is still running in background, does anybody knows how to kill the wpa_supplicant running process without knowing its pid?

Edit : I confirm it comes from the wpa-supplicant running in background. The only way I found to solve it is to call a

sudo killall wpa_supplicant 

However this solution doesn't satisfy me, imagine I have another instance of wpa_supplicant running on another interface I will loose all my network connections. Pretty sure there is a better way to kill the process associated to this specific interface. I'll dig around. Hope someone already had this situation.

Arkaik
  • 852
  • 2
  • 19
  • 39

2 Answers2

1

Forget ifconfig, it will start wpa_supplicant too.
Instead use ip link set wifi0 up or ip link set wifi0 down to up/down interface, and wpa_supplicant/wpa_cli to connect to network.
Example calling supplicant:
wpa_supplicant -i wifi0 -D nl80211 -c /etc/wpa_supplicant/my_wpa_supplicant.conf You can set your networks in .conf file directly, or add them dynamically. Generate psk by wpa_passphrase, putting output inside your .conf file.

network={
    ssid="TOPGUN"
    #psk="qwerty123"
    psk=f910451a8a67c0e061d1a296955ad604b4fe6fb0e442b8a997f350f434e07f00
}

network={
    ssid="TOPGUN2"
    #psk="qwerty123"
    psk=538a2337031c5308082285cee3e433c62aaf49260cbb35768eeb3e6baa2d5191
}

Now you can change those networks from script by using wpa_cli, like this.

wpa_cli -iwifi0 select_network 0
wpa_cli -iwifi0 enable_network 0

and

wpa_cli -iwifi0 select_network 1
wpa_cli -iwifi0 enable_network 1

Where 0 is number of the first network in your .conf file, 1 is the second network.
Or
Adding networks dynamically every time in your script, that way you get a bit more control on which number networks has. Just remember that those are not persistent, should be run for every restart of the system.

wpa_cli -iwifi0 add_network <-- this command returns number of network which is used in following commands, you should remember returned value

wpa_cli -iwifi0 set_network 0 key_mgmt WPA-PSK
wpa_cli -iwifi0 set_network 0 psk '"qwerty123"'
wpa_cli -iwifi0 set_network 0 ssid '"TOPGUN"'
wpa_cli -iwifi0 select_network 0
wpa_cli -iwifi0 enable_network 0
metamorphling
  • 369
  • 3
  • 9
  • Hi, I'm trying to use wpa_cli but I always get the error `Failed to connect to non-global ctrl_ifname: wlan0 error: No such file or directory` also I launched wpa_supplicant with `-i wlan0` but wpa_cli seems to not recognize this interface as it always gives me the same error – Arkaik Jul 21 '17 at 08:52
  • That message means there is no running wpa_supplicant. Check it with `ps -aux | grep wpa`. If it's not running confirm with `ifconfig` if `wlan0` really upped? If it's okay, see if `/etc/wpa_supplicant/my_wpa_supplicant.conf` file exists. If it's okay, try to omit driver part `-D nl80211` – metamorphling Jul 21 '17 at 09:05
  • However there is a running instance of wpa_supplicant `root 1799 0.0 0.8 6692 3816 ? Ss 09:18 0:00 wpa_supplicant -B -c /etc/wpa_supplicant/SSID.conf -i wlan0`. The `/etc/wpa_supplicant/wpa_supplicant.conf` file also exists. Also I'm never running with the -D option. – Arkaik Jul 21 '17 at 09:15
  • Try to bring down all the network daemons: NM, connman. There is a chance they might interfere – metamorphling Jul 21 '17 at 09:26
  • I though there was no more interference problems since strech ? The problem is that I use NM for my eth0 interface :/ – Arkaik Jul 21 '17 at 09:30
0

I recommend you to use wpa_supplicant frontend program wpa_cli to control wpa_supplicant.

  1. first enable wpa_supplicant control socket by add ctrl_interface=/var/run/wpa_supplicant in wpa conf file, the exactly path and name can be changed according your prefer.
  2. use wpa_cli -p /var/run/wpa_supplicant terminate to teminate the wpa_supplicant.

Please refer help of wpa_cli for more information.

wen
  • 114
  • 1
  • 3
  • Hi, I already have a line `ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev` in `/etc/wpa_supplicant/wpa_supplicant.conf`. However I don't have any `/var/run/wpa_supplicant` even if there is a running instance of wpa_supplicant – Arkaik Jul 21 '17 at 09:20
  • Remove string 'DIR' please. the correct config should be: 'ctrl_interface=/var/run/wpa_supplicant' – wen Jul 21 '17 at 09:55
  • Did it, but still no `/var/run/wpa_supplicant` – Arkaik Jul 21 '17 at 11:12