0

My conf file looks like this:

ssid="oldssid"
psk="oldpassword"

I'm using the following code to try and edit my Wifi settings in a Node JS app but it isn't making any changes to the file. Any advice would be much appreciated!

        var ssid_command = "sed -i \'s/ssid=\"oldssid\"/ssid=\"" + newssid + "\"/\' /etc/wpa_supplicant/wpa_supplicant.conf";
        var psk_command = "sed -i \'s/psk=\"oldpassword\"/psk=\"" + newpassword + "\"/\' /etc/wpa_supplicant/wpa_supplicant.conf";
        require('child_process').exec(ssid_command, function (msg) { console.log(msg) });
        require('child_process').exec(psk_command, function (msg) { console.log(msg) });
agniiyer
  • 1
  • 3
  • 2
    Please show what's in your `wpa_supplicant.conf` before and after... by clicking `edit`, not in the comments area. Thank you. – Mark Setchell Dec 05 '20 at 08:37
  • If your file is that simple, just overwrite it wholesale with the new settings rather than creating a bunch of unnecessary processes. – Mark Setchell Dec 05 '20 at 09:54

2 Answers2

0

If the contents of your conf file look like so...

ssid=oldssid
psk=oldpassword

...then your generated shell command of...

sed -i 's/ssid="oldssid"/ssid="newssid"/' wpa_supplicant.conf

...has one too many levels of quoting.

Everything within the single quote marks is treated by the shell as one uninterrupted, unparsed string, and is passed to sed as such. Sed treats quote marks just as normal characters to match and replace - so the marks around oldssid are sought, and presumably not found because they don't exist in the file.

You want to generate something more like this:

sed -i 's/ssid=oldssid/ssid=newssid/' wpa_supplicant.conf

...which you could do like so...

var command = "sed -i 's/ssid=oldssid/ssid=" + newssid + "/' wpa_supplicant.conf";

PS you also don't need to escape the single quotes within a doubly-quoted string in javascript - though doing so doesn't break anything

Jason Holloway
  • 682
  • 5
  • 9
  • Hi Jason. The contents of my conf file look like this: ssid="oldssid" psk="oldpassword" How would you do it with this change? Thanks in advance. – agniiyer Dec 05 '20 at 08:31
  • oh that's a shame - I was guessing it was that (always so many possibilities!) What I'd definitely do now is to split the problem up to narrow it down - remove node from the equation and confirm that the sed commands work properly just from the shell. Another way of narrowing it down (in a kind of pincer movement): try running simpler shell commands with child_process.exec - make sure they're behaving as expected (eg `echo hello > file`) – Jason Holloway Dec 05 '20 at 08:54
0

Turns out all I had to do was add sudo to the beginning so that I have permission to edit the file.

Dharman
  • 30,962
  • 25
  • 85
  • 135
agniiyer
  • 1
  • 3