How to fetch address and modify the value in key/value format file?

2

I'm currently writing a shell-script for my Raspberry Pi so it sets up an access-point with a unique SSID. To do this I want to use the mac-address of the WiFi dongle as my SSID. For doing that I want to use awk, with

awk '{ gsub(/:/, ""); print > "file" }' /sys/class/net/wlan0/address

I can write the properly formatted mac address to a file, but how can I insert it into a specific line/place at another file? I have in mind something like

awk '{ gsub(/:/, ""); "replace ssid= at /etc/hostapd/hostapd.conf"  }' /sys/class/net/wlan0/address

Any help plus explanation for a newbie like me is appreciated.


My input file is like:

$ cat /sys/class/net/wlan0/address
01:23:45:67:89:ab

and I'm trying to fetch it and replace the value of ssid in the file which is like:

interface=wlan0 driver=nl80211 ssid=7cdd907f6b07 hw_mode=g channel=11 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=1 wpa_passphrase=My_Passphrase wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP

Jabaal

Posted 2015-10-26T23:04:59.847

Reputation: 23

Can you paste some example content of /sys/class/net/wlan0/address to parse? And what are you expected results? – kenorb – 2015-10-26T23:54:34.423

@kenorb the content is a mac address: 7c:dd:90:7f:6b:07 and the expected results are ssid=pi_7cdd907f6b07 within the /etc/hostapd/hostapd.conf – Jabaal – 2015-10-27T09:46:45.910

Just single line with something like 01:23:45:67:89:ab? Can you simply use grep: grep -o ..:..:..:..:..:.. >> file? Or the issue is rather that you want it at the particular line? Do you have ex in Rasberry Pi? – kenorb – 2015-10-27T09:50:39.247

I have multiple lines within the /etc/hostapd/hostapd.conf - I just need to modify the one with ssid=some_ssid - the grep command just writes to a file. – Jabaal – 2015-10-27T09:52:03.277

Answers

0

 awk 'NR==FNR{gsub(":","");macadd=$0;next}{sub(/^ssid=.*/,"ssid="macadd)}1' /sys/class/net/wlan0/address /etc/hostapd/hostapd.conf

Current record no. (NR) equals current file record no. just for the first file, then :'s substitution is done, subsequently the line is placed on the macadd variable, and next skips line output.

The other instructions are applied to the hostapd.conf file; final 1 stands for "true", thus lines are printed.

If everyting works fine, you may redirect command output to a new file, and replace hostapd.conf.

Notes:

  • files order does matter, as you can tell
  • gawk features in-place replacement starting from 4.1.0

SΛLVΘ

Posted 2015-10-26T23:04:59.847

Reputation: 1 157

perfect! exactly what I was looking for. – Jabaal – 2015-10-29T20:35:39.247

Thank you for your feedback! A remark, although you've probably stepped into this definition: when you read "record" in awk, you may think of a line (as in this case) or part of it (a "field") – SΛLVΘ – 2015-10-29T20:38:58.040

0

sed -i.bak "s/^ssid=.*/ssid=$(tr -d ':'</sys/class/net/wlan0/address|tee file)/" /etc/hostapd/hostapd.conf

Once tested, you can remove the .bak part, which creates a backup of hostapd.conf. I think you are also going to delete |tee file.

The ^ sign in regexps stands for "beginning of line"; the s operator in sed substitutes a regexp with a given string. Given string here has a subshell called thru $(...), getting address content, purging it from :'s with tr, and returning the output after a copy is stored into the file file by the tee command.

SΛLVΘ

Posted 2015-10-26T23:04:59.847

Reputation: 1 157

1Your answer is helpful and works, although I'm specifically looking for a awk solution. – Jabaal – 2015-10-27T09:54:42.750

will the whole line be replaced or just ssid:=? – Jabaal – 2015-10-27T20:01:12.180

No colon in my regexp. Rules that apply: ^ means start of line; . means any single character; a* means zero or more of a. Test it yourself. BTW, did you miss the awk solution I tried to write down?

– SΛLVΘ – 2015-10-27T22:49:46.707

0

Then load and save it in-place using ex editor, try:

ex +'let @m = substitute(system("cat /sys/class/net/wlan0/address"), "[^0-9a-z]", "", "g")' +'%s/ssid=\zs[a-f0-9]\{12}/\=@m/' -scwq /etc/hostapd/hostapd.conf

which:

  • takes MAC address from the file (system("cat file") which is equivalent to readfile('file')
  • substitute output by removing everything exempt [0-9a-z]
  • then assign it into @m register
  • then substitute on hostapd.conf by finding ssid and replacing \{12} characters of [a-f0-9] with value of m register (\=@m),

    in other words, it's like: %s/^key=\zs.\+/new_value/

  • then save the file in-place (-cwq)

To debug it, you may:

  • add extra +%p (to print it) and change -scwq into -scq! for dry-run (without changing the file)
  • add -V1 for more verbose

To fetch just MAC address from ifconfig it's easy to do with grep, like:

ifconfig wlan0 | egrep -om1 "..(:..){5}"

or:

grep -o ..:..:..:..:..:.. <(ifconfig wlan0)

kenorb

Posted 2015-10-26T23:04:59.847

Reputation: 16 795