How to Delete a Network Profile from etc/Wpa_Supplicant/Wpa_Supplicant.Conf Through Command Line/Shell Script

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

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

Delete a network profile from wpa_supplicant.conf in Linux (raspbian).


SSID_TO_DELETE="Put your ssid here"
sed -n "1 !H;1 h;$ {x;s/[[:space:]]*network={\n[[:space:]]*ssid=\"${SSID_TO_DELETE}\"[^}]*}//g;p;}" YourFile

in a C that can generate your SSID info directly in command (replace Put_your_ssid_here with the value of the ssid)

sed '1 !H;1 h;$ {x;s/[[:space:]]*network={\n[[:space:]]*ssid="Put_your_ssid_here"[^}]*}//g;}' YourFile

1st snippet with \n in place of ;

SSID_TO_DELETE="Put your ssid here"
sed -n "1 !H
1 h
$ {
x
s/[[:space:]]*network={\n[[:space:]]*ssid=\"${SSID_TO_DELETE}\"[^}]*}//g
p
}" YourFile

Principle (based on last snippet)

  • sed -n: don't print line when read (unless specific request in code)
  • 1 !H and 1 h load all the lines into the the hold buffer (so all the file is in memory, sed work by default line by line)
  • $ mean when last line is reach
  • x, load hold buffer (the load file) into working buffer (the one sed can work on)
  • s/... replace the part of text containing your network pattern until first } after your SSID on next line by nothing (g: for all occurence)
  • p print the final result

Change WiFi WPA2 passkey from a script

If we can assume the fields will always be in a strict order where the ssid= goes before psk=, all you really need is

 sed "/^[[:space:]]*ssid=\"$SSID\"[[:space:]]*$/,/}/s/^\([[:space:]]*psk=\"\)[^\"]*/\1$PSK/" wpa.txt

This is fairly brittle, though. If the input is malformed, or if the ssid goes after the psk in your block, it will break. The proper solution (which however is severe overkill in this case) is to have a proper parser for the input format; while that is in theory possible in sed, it would be much simpler if you were to swtich a higher-level language like Python or Perl, or even Awk.

How to parse string fragment between two others using sed or other linux terminal utilities?

Using the first example file from https://www.systutorials.com/docs/linux/man/5-wpa_supplicant.conf/:

$ cat file
# allow frontend (e.g., wpa_cli) to be used by all users in 'wheel' group
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
#
# home network; allow all valid ciphers
network={
ssid="home"
scan_ssid=1
key_mgmt=WPA-PSK
psk="very secret passphrase"
}
#
# work network; use EAP-TLS with WPA; allow only CCMP and TKIP ciphers
network={
ssid="work"
scan_ssid=1
key_mgmt=WPA-EAP
pairwise=CCMP TKIP
group=CCMP TKIP
eap=TLS
identity="user [at] example.com"
ca_cert="/etc/cert/ca.pem"
client_cert="/etc/cert/user.pem"
private_key="/etc/cert/user.prv"
private_key_passwd="password"
}

Here's how to find a tag with a given value and print the value of some other tag using a standard UNIX tool:

$ cat tst.awk
{ gsub(/^[[:space:]]+|[[:space:]]+$/,"") }
/^network[[:space:]]*=/ {
inNw = 1
next
}
inNw {
if ( /^[[:alnum:]_]+[[:space:]]*=/ ) {
tag = val = $0
sub(/[[:space:]]*=.*/,"",tag)
sub(/[^=]+=[[:space:]]*/,"",val)
f[tag] = val
}
else if ( /^}/ ) {
if ( ( f[fndTag] == fndVal ) ||
( f[fndTag] == "\"" fndVal "\"" ) ) {
print f[prtTag]
}
delete f
inNw = 0
}
}

.

$ awk -v fndTag='ssid' -v fndVal='home' -v prtTag='psk' -f tst.awk file
"very secret passphrase"

$ awk -v fndTag='ssid' -v fndVal='work' -v prtTag='key_mgmt' -f tst.awk file
WPA-EAP

$ awk -v fndTag='key_mgmt' -v fndVal='WPA-EAP' -v prtTag='identity' -f tst.awk file
"user [at] example.com"

The above will work using any awk in any shell on any UNIX box.

Connecting to WiFi using adb shell

You can add a network entry into the wpa_supplicant.conf yourself (or within your script) Essentially connect manually once, then do:

adb pull /data/misc/wifi/wpa_supplicant.conf

and integrate the network entry into your script for automation. Example simple script:

#!/bin/bash
#

# Get this information by connecting manually once, and do
# adb pull /data/misc/wifi/wpa_supplicant.conf
ADB_PULL="adb pull /data/misc/wifi/wpa_supplicant.conf"
WIRELESS_CTRL_INTERFACE=wlan0
WIRELESS_SSID=Gondolin
WIRELESS_KEY_MGMT="WPA-EAP IEEE8021X"
WIRELESS_EAP=PEAP
WIRELESS_USER=Turgon
WIRELESS_PASSWORD=IdrilCelebrindal

adb start-server
adb wait-for-device
echo "adb connection....[CONNECTED]"
adb root
adb wait-for-device
adb remount
adb wait-for-device

pushd /tmp
rm wpa_supplicant.conf 2>/dev/null # Remove any old one
adbpull_status=`$ADB_PULL 2>&1`
echo -e "\nAttempting: $ADB_PULL"
if [ `echo $adbpull_status | grep -wc "does not exist"` -gt 0 ]; then
echo " wpa_supplicant.conf does not exist yet on your device yet."
echo "This means you have not used your wireless yet."
echo ""
echo "Taking our best shot at creating this file with default config.."

echo "ctrl_interface=$WIRELESS_CTRL_INTERFACE" >> wpa_supplicant.conf
echo "update_config=1" >> wpa_supplicant.conf
echo "device_type=0-00000000-0" >> wpa_supplicant.conf
else
echo $adbpull_status
echo " wpa_supplicant.conf exists!"
fi

echo ""
echo "Add network entry for wpa_supplicant.conf.."
echo "" >> wpa_supplicant.conf
echo "network={" >> wpa_supplicant.conf
echo " ssid=\"$WIRELESS_SSID\"" >> wpa_supplicant.conf
echo " key_mgmt=$WIRELESS_KEY_MGMT" >> wpa_supplicant.conf
echo " eap=$WIRELESS_EAP" >> wpa_supplicant.conf
echo " identity=\"$WIRELESS_USER\"" >> wpa_supplicant.conf
echo " password=\"$WIRELESS_PASSWORD\"" >> wpa_supplicant.conf
echo " priority=1" >> wpa_supplicant.conf
echo "}" >> wpa_supplicant.conf
echo "Pushing wpa_supplicant.conf.."
adb push wpa_supplicant.conf /data/misc/wifi/wpa_supplicant.conf
popd #/tmp

adb shell chown system.wifi /data/misc/wifi/wpa_supplicant.conf
adb shell chmod 660 /data/misc/wifi/wpa_supplicant.conf

echo ""
echo "Finished!"
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings
echo "Please toggle wifi off/on now.. (ifconfig not sufficient, monkey this)"

linux shell script does not capture input params

They are being passed to the script, you could see that if you put echo "$1" immediately before the line:

setup_interfaces

Where they're not being passed is from that line to your function. Within the function, $1..n are the arguments to the function, not the program.

The first call is the snippet below is basically what you're doing, the second call forwards the script arguments on to the function:

#!/usr/bin/env bash

myfunc() {
for arg in "$@" ; do
echo " ${arg}"
done
}

echo "No args:"
myfunc
echo "With args:"
myfunc "$@"

The output is shown in the following transcript:

pax> ./prog.sh a b c "d e f"
No args:
With args:
a
b
c
d e f

The solution, in your case, is as simple as calling the function with something like:

setup_interfaces "$1" "$2" "$3" "$4"

You may also want to properly use the variables within your function as well:

INTERFACE_0="$1"
SSID_0="$2"
PSK_0="$3"
WIRELESS_POWER_0="$4"
echo "interface: $INTERFACE_0 ssid: $SSID_0 psk: $PSK_0 wireless_power: $WIRELESS_POWER_0"

and realise that, within ' single quotes, variable expansion is not done, so you probably want " double quotes on the sed commands, such as:

sudo sed -i "s/<interface_0>/$INTERFACE_0/g" /etc/network/interfaces


Related Topics



Leave a reply



Submit