在Linux(raspbian)中,从wpa_supplicant.conf删除网络配置文件。 [英] Delete a network profile from wpa_supplicant.conf in Linux (raspbian).

查看:367
本文介绍了在Linux(raspbian)中,从wpa_supplicant.conf删除网络配置文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在游荡Raspbian,但这不是Pi特有的问题

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

我需要从C程序中删除一个未使用的来自etc / wpa_supplicant / wpa_supplicant.conf的网络配置文件。

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

我的程序以root用户身份运行。

My program runs as root.

是否存在我使用shell命令吗?

Is there a shell command for this ?

我尝试使用grep,tr和sed的组合,但是我还没到那儿。
而且空白可能会有所不同。

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

我需要针对给定的ssid删除此块,而无需考虑空白。

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

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


推荐答案

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

$ b C中的
$ b

可以直接在命令中生成您的SSID信息(将Put_your_ssid_here替换为ssid的值)

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

第一个代码段,用\n代替;

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

原理(基于升AST代码段)

Principle (based on last snippet)


  • sed -n:读取时不打印行(除非代码中有特定要求)

  • 1!H和1 h将所有行加载到保持缓冲区中(因此所有文件都在内存中,默认情况下逐行执行sed工作)

  • $表示何时最后一行到达

  • x,将保持缓冲区(加载文件)加载到工作缓冲区(一个sed可以工作)

  • s /。 ..将包含您的网络模式的文本部分替换为下一行的SSID之后的第一个},直到第一个}(g:表示所有情况)

  • p打印最终结果

  • 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

这篇关于在Linux(raspbian)中,从wpa_supplicant.conf删除网络配置文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆