从脚本更改WiFi WPA2密码 [英] Change WiFi WPA2 passkey from a script

查看:64
本文介绍了从脚本更改WiFi WPA2密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Raspbian Wheezy,但这不是Raspberry Pi特有的问题.

I'm using Raspbian Wheezy, but this is not a Raspberry Pi specific question.

我正在开发一个C应用程序,该应用程序允许用户更改其WiFi密码.

I am developing a C application, which allows the user to change their WiFi Password.

我没有为此准备好的脚本/命令,因此我尝试使用 sed .

I did not find a ready script/command for this, so I'm trying to use sed.

我将SSID名称和新密钥传递给bash脚本,并将该密钥替换为* etc/wpa_supplicant/wpa_supplicant.conf.*.中的ssid块.

I pass the SSID name and new key to a bash script, and the key is replaced for the that ssid block within *etc/wpa_supplicant/wpa_supplicant.conf.*.

我的应用程序以root身份运行.

My application runs as root.

下面显示了一个示例块.

A sample block is shown below.

    network={
        ssid="MY_SSID"
        scan_ssid=1
        psk="my_ssid_psk"
}

到目前为止,我已经尝试了以下操作(我已经将wpa_supplicant.conf复制到wpa.txt以进行尝试):

so far I've tried the following (I've copied the wpa_supplicant.conf to wpa.txt for trying) :

(1)尝试在一个范围之间进行替换,该范围在检测到我的SSID时开始,在右括号后跟换行符之间结束.

(1) This tries to do the replacement between a range, started when my SSID is detected, and ending when the closing brace, followed by a newline.

SSID="TRIMURTI"
PSK="12345678"

sed -n "1 !H;1 h;$ {x;/ssid=\"${SSID}\"/,/}\n/ s/[[:space:]]*psk=.*\n/\n   psk=\"${PSK}\"\n/p;}" wpa.txt

(2)这会尝试记住"匹配的模式,并使用新的键在输出中重现它.

(2) This tries to 'remember' the matched pattern, and reproduce it in the output, but with the new key.

SSID="TRIMURTI"
PSK="12345678"

sed -n "1 !H; 1 h;$ {x;s/\(ssid=\"${SSID}\".*psk=\).*\n/\1\"${PSK}\"/p;}" wpa.txt   

我已使用Hold&模式缓冲区,因为该模式可以跨越多行.

I have used hold & pattern buffers as the pattern can span multiple lines.

以上,第一个示例似乎忽略了范围&替换第一个实例,然后截断文件的其余部分.

Above, the first example seems to ignore the range & replaces the 1st instance, and then truncates the rest of the file.

第二个示例替换了最后找到的psk值&此后会截断文件.

The second example replaces the last found psk value & truncates the file thereafter.

因此,我需要纠正以上代码或尝试其他解决方案的帮助.

So I need help in correcting the above code, or trying a different solution.

推荐答案

如果我们可以假定字段始终严格按照 ssid = psk = 之前的顺序排列,code>,您真正需要的只是

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

但是,这非常脆弱.如果输入格式错误,或者 ssid 在代码块中的 psk 之后,它将中断.适当的解决方案(然而,在这种情况下严重过大)是为输入格式提供适当的解析器.从理论上讲,这在 sed 中是可行的,但是如果您要使用Python,Perl甚至Awk之类的高级语言,则要简单得多.

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.

这篇关于从脚本更改WiFi WPA2密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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