This is primarily a formatting problem which I haven't been able to solve well.

I want to create a bash function or script which converts output from wpa-passphrase to a format that can be used by /etc/network/interfaces.

Example WPA-Supplicant

wpa_passphrase ssid password

Produces the following output


I need to convert that output to a format which can be used by /etc/network/interfaces

Example of the /etc/network/interface output format:

auto wlan0
iface wlan0 inet dhcp
        wpa-ssid MYSSID
        wpa-psk ccb290fd4fe6b22935cbae31449e050edd02ad44627b16ce0151668f5f53c01b

My first crude solution was to use:

wpa_passphrase MYSSID PASSWORD |grep -E 'ssid|psk' |grep -v "#psk" |cut -d '=' -f 2

Which gives me:


but I don't know how to get each of those lines into their proper place in the /etc/network/interfaces format

UPDATE: With my current solution

After further attempts I managed to get it to work with this:

while read line
        echo "auto wlp3s0"
        echo "iface wlp3s0 inet dhcp"           
        echo -e "\t wpa-ssid $line"
done < <(sudo wpa_passphrase $1 $2 |grep -E 'ssid' |cut -d '=' -f 2)

while read line
        echo -e "\t wpa-psk $line"
done < <(sudo wpa_passphrase $1 $2 |grep -E 'psk' |grep -v "#psk" |cut -d '=' -f 2)

My solution gives the output I need, but it does not seem efficient and I would still like to see other solutions.


You can make a function as below and use awk to format the output as you want:

generate() {
sudo wpa_passphrase "$1" "$2" | 
awk -F= 'BEGIN      { print "auto wlan0\n" "iface wlan0 inet dhcp" }
         /ssid/     { print "", "wpa-ssid", $2 }
         /[^#]psk=/ { print "", "wpa-psk" , $2 }' OFS='\t'

Then call generate "my SSID" "PASSWORD".

Output would be:

auto wlan0
iface wlan0 inet dhcp
        wpa-ssid        "my SSID"
        wpa-psk ccb290fd4fe6b22935cbae31449e050edd02ad44627b16ce0151668f5f53c01b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.