Rolwin C Rolwin C - 6 months ago 68
Bash Question

How to delete a network profile from etc/wpa_supplicant/wpa_supplicant.conf through command line / shell script

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
}

Answer

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 
Comments