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

后端 未结 4 1954
甜味超标
甜味超标 2021-01-03 07:20

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

相关标签:
4条回答
  • 2021-01-03 08:00

    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 
    
    0 讨论(0)
  • 2021-01-03 08:04

    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"
    
    0 讨论(0)
  • 2021-01-03 08:08
    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
    0 讨论(0)
  • 2021-01-03 08:24

    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

    0 讨论(0)
提交回复
热议问题