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
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
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"
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:
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