6

I have multiple wifi network ssid's saved in my etc/wpa_supplicant/wpa_supplicant.conf like shown below, can we delete a specific network from this wpa_supplicant.conf

Ex: in the below networks can a delete the network myssid1 through a shell script which i can then execute through node.js server

 network={
            ssid="myssid1"
            scan_ssid=0
            proto=WPA
            key_mgmt=WPA-PSK
            psk=5f55a9b869e9ab6d03839cae23c7243accc0ac0a12079d358328bf73ad2e0ebe
    }
    network={
           ssid="myssid2"
           scan_ssid=0
           proto=WPA
           key_mgmt=WPA-PSK
           psk=d89660510d06bbf7691f5296daae36872d697a88876c53db7de91aa85df4f68b
    }
    network={
           ssid="myssid3"
           scan_ssid=0
           proto=WPA
           key_mgmt=WPA-PSK
           psk=d635b33481a13b28a67e8964f58343cb19bc8c85c67cc56ee9bfe0c302914a5f
    }
Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45

4 Answers4

16

using wpa_cli you can do this:

1:

wpa_cli remove_network 0

where 0 is the network_id you get after running wpa_cli add_network. It will remove the network and disconnect any interface using it.

Note that the network id is not the order of the network in the file. you can get configured network using wpa_cli list_networks

2:

wpa_cli save_config

This will persist the changes and the corresponding network block will be removed from etc/wpa_supplicant/wpa_supplicant.conf

Tychus
  • 170
  • 3
  • 14
0

You can write it you're self. Some very ugly Quick-n-Dirty Code would be for example:

file="/etc/wpa_supplicant/wpa_supplicant.conf"
foo="$(cat "$file" | awk '/myssid3/ { flag=1 }; flag==0 { print $0 }; /network={/ { flag=0 }' )"
if echo -e "$foo" | tail -1 | grep -q 'network={'; then
   foo=$(echo -e "$foo" | head -n -1)
fi
echo -e "$foo" > "$file"
Nudin
  • 351
  • 2
  • 11
0
SSID=$1 
temp_var=$(sudo awk -v RS= '!/${SSID}/{printf $0""RT}' etc/wpa_supplicant/wpa_supplicant.conf)
echo -e "$temp_var" | sudo tee etc/wpa_supplicant/wpa_supplicant.conf

The temp var is needed because this is the easiest way I found to actually make awk write to he file its processing. To see the effect of changing wpa_supplicant.conf, do

svc wifi disable && svc wifi enable

Some references:

Community
  • 1
  • 1
Asu
  • 1,723
  • 2
  • 21
  • 32
-1

i was able to get it done with the below script:

SSID_TO_DELETE=$1 
sed -n "1 !H 1 h $ { x s/[[:space:]]*network={\n[[:space:]]*ssid=\"${SSID_TO_DELETE}\"[^}]*}//g p }" /etc/wpa_supplicant/wpa_supplicant.conf > /etc/wpa_supplicant/wpa_supplicant.conf 
Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45