0

I'm runnining Raspbian, but this is not a Pi specific question

I need to delete from within my C program an unused network profile from etc/wpa_supplicant/wpa_supplicant.conf.

My program runs as root.

Is there a shell command for this ?

I tried using combinations of grep, tr, and sed, but I'm not getting quite there. Also the white-spaces may vary.

I need to remove this block for a given ssid, disregarding white-spaces.

   network={
      ssid="MY_SSID_TO_DELETE"
      .........
   }

2 Answers2

2
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
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • Hi, I tried the 1st code snippet above I get this message ` -bash: !H: event not found` – Royce Pereira Nov 28 '13 at 16:12
  • The 2nd code snippet 'sort-of' works. The file contents before deletion and after, both print on the screen. If we could only get the latter (deleted version) to print, it would be perfect ! – Royce Pereira Nov 28 '13 at 16:34
  • try sed -n and change g;} with g;p;} at the end. FOr the snippet, i change and add a space (missing at copy) after the 2 first 1 of the sed – NeronLeVelu Nov 29 '13 at 07:18
  • Hi, Thank you for your replies ! Sorry for the delayed response...was a bit busy over the weekend... The 2nd code works now ! (I'll test it a bit more) ... the 1st code still gives -bash: !H: event not found` even after the edits you suggested ... I did not understand your instruction about adding the space ... where to add ? Also in the 1st code, the sed parameters are enclosed in " , while in the 2nd code they are enclosed in '. - is this OK ? It would be great if the 1st code could work as well, so I can add it in a script and then pass the ssid to it. Thank You ! – Royce Pereira Dec 02 '13 at 05:31
  • i just adapat the 1st snippet and test it here (AIX and KSH). No error occuring and display the good result. Do you still have the problem ? – NeronLeVelu Dec 02 '13 at 07:08
  • Yes, in the modified 1st code, i'm still getting : -bash: !H: event not found ... anything I need to check ? What _event_ does it expect ? Perhaps I could give you more information ? Thanks ! – Royce Pereira Dec 02 '13 at 07:59
  • try replace the ; by a carriage return (new line) after H and h – NeronLeVelu Dec 02 '13 at 08:21
  • You mean like this ? ........ SSID_TO_DELETE="TRIMURTI" sed -n "1 !H\n1 h\n$ {x;s/[[:space:]]*network={\n[[:space:]]*ssid=\"${SSID_TO_DELETE}\"[^}]*}//g;p;}" wpa.txt – Royce Pereira Dec 02 '13 at 08:28
  • no probleme, no not a \n a real CR (i will add a 3th snippet wit it) – NeronLeVelu Dec 02 '13 at 12:11
  • Neron, sorry I caused you needless trouble :P ! The original code(1st) works fine inside a bash script! I was trying it from the prompt (where it failed), since the 2nd worked from the prompt! I got the clue when I saw the modified code with the 'enter's. So it had to be inside a script ! It seems to work now (still checking). The modified code (with the 'Enter's is not needed ;) **Many Thanks!** Could you briefly explain how the code(s) work (if you have the time)? Thanks again ! I will marking this as 'Top Answer' shortly after I test the code a bit (right now I'm using a dummy file) :) – Royce Pereira Dec 04 '13 at 04:38
  • i will add the explaination in answer directly – NeronLeVelu Dec 04 '13 at 07:10
  • So there is no difference between Code snippets 1 & 3, right? Both are identical ? – Royce Pereira Dec 05 '13 at 04:03
  • for the algorythm your are right, same code. For interpretation by sed it's the same normaly, just the new line instead of ";". This is mainly due to some different interpretation by sed depending of OS version and sed version. By example, some GNU linux sed accept a ";t a;" and the same doesn't work on my AIX/KSH/sed where same "t a" with new line before and after works – NeronLeVelu Dec 05 '13 at 06:05
  • Hi Neron, I have a request ... could you isolate the part of your code that isolates the desired 'network= { ... } block, given the SSID ? .My code has progressed, thanks to you, but a bit slowly, as it is more of a hobby. I need a way to to do other things from my code, like change the passkey, etc. .So if I can ouput the relevant 'network= { ... }' block by passing the SSID, I can try a couple of things on my own .... Thanks ! – Royce Pereira Dec 29 '13 at 07:43
  • what do you mean by isolate (extend you question with a sample maybe) – NeronLeVelu Jan 02 '14 at 09:22
  • Hi, Neron, sorry for the delayed response, what i need is: (1) if I pass the SSID name to my script, it should just ouput the block relevant to that SSID i.e.: "network={ .... }" for the given SSID. (2) I also would like to replace the WiFi password from my program by passing the SSID & new password to the script. PS: Is it okay to ask these things here, or would you like me to open another topic ? Thank You ! – Royce Pereira Jan 11 '14 at 07:42
  • make some try and post a new question with your test if you could not find the solution. The principle of the site does not provide code for a problem but to help on a unresolved problem you have ;-) – NeronLeVelu Jan 13 '14 at 07:27
  • Hi Neron, as you suggested I tried something with my new knowledge of sed. I posted a new question here, with what i've tried so far: http://stackoverflow.com/questions/22215830/change-wifi-wpa2-passkey-from-a-script would you be kind enough to take a look ? Thank You. – Royce Pereira Mar 07 '14 at 16:30
1

Try this

SSID_TO_DELETE=$1
    sudo 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
Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45