How to delete a network profile from etc/wpa_suppl

2020-02-29 03:58发布

问题:

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
    }

回答1:

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



回答2:

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"


回答3:

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 


回答4:

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:

  • https://stackoverflow.com/a/18955226/1246369
  • https://unix.stackexchange.com/a/89707/172003
  • http://ubuntuforums.org/showthread.php?t=981258#3