UPDATED: Automating Unifi WiFi SSID password changes and QR code generation

Are there any errors in the logs when you run the automation? What kind of ubiquiti controller do you have?

I donā€™t know where the logs are. Can you tell me how to view them? I have the dream machine pro.

Can you tell me where the logs are?

PR is up for HA core Add WLAN QR code support to UniFi Image platform by Kane610 Ā· Pull Request #97171 Ā· home-assistant/core Ā· GitHub

3 Likes

Hi @exx ,

Iā€™m getting this error in the log

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:131
Integration: Shell Command (documentation, issues)
First occurred: 6:48:23 PM (1 occurrences)
Last logged: 6:48:23 PM

Error running command: `/bin/bash /config/scripts/wifichange.sh change {{ username }} {{ password }} {{ baseurl }} {{ network_id }} {{ ssid }}`, return code: 2
NoneType: None


Any idea where to look?
iā€™m confident the local user id and pw are correctly entered in the automation entry.

with the latest release of unifi integration in 2023.8, QR entity is available. I guess iā€™m just more concerned on how to automate the guest wifi password in udmpro. is there any other way out there?

thanks in advance for your response.

Regards

hi @it-luisfsb ,

iā€™m gettint return code: 2 as well.
i tried look at your solution but i donā€™t understand, please can you point what portion in the code you applied the fix, please?
thanks.

Thank you all for this great post. unfortunately, Iā€™m getting an error too

stdout: ""
stderr: "/config/scripts/wifichange.sh: line 2: $'\\r': command not found\n/config/scripts/wifichange.sh: line 5: $'\\r': command not found\n/config/scripts/wifichange.sh: line 7: $'\\r': command not found\n/config/scripts/wifichange.sh: line 10: $'\\r': command not found\nConnecting to api.qrserver.com (95.216.163.127:80)\nwget: server returned error: HTTP/1.1 400 Bad Request\n/config/scripts/wifichange.sh: line 13: $'\\r': command not found\ncurl: option --insecure\r: is unknown\ncurl: try 'curl --help' or 'curl --manual' for more information\n/config/scripts/wifichange.sh: line 16: $'\\r': command not found\ncurl: option --insecure\r: is unknown\ncurl: try 'curl --help' or 'curl --manual' for more information"
returncode: 2

my wifichange.sh

#!/bin/sh

cookie=$(mktemp)
headers=$(mktemp)

curl_cmd="curl --silent --output /dev/null --cookie ${cookie} --cookie-jar ${cookie} --insecure"

# generate new password
NEW_PWD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)

# generate QR code
wget --output-document="/config/www/${5}.png" "http://api.qrserver.com/v1/create-qr-code/?data=WIFI:T:WPA;S:${5};P:${NEW_PWD};H:;&size=100x100"

# authenticate against unifi controller
${curl_cmd} -H 'Content-Type: application/json' -D ${headers} -d "{\"username\":\"${1}\", \"password\":\"${2}\"}" https://${3}8443/api/login

# change wifi password
${curl_cmd} -k -X PUT https://${3}:8443/api/s/default/rest/wlanconf/${4} -H "Content-Type: application/json" -d "{\"_id\":\"${4}\",\"x_passphrase\":\"${NEW_PWD}\"}"

I got the same error with every iteration of the script.

  • unifi login requests should use the post method: -X POST. If omitted, curl defaults to -X GET
  • put a colon between ${3} and 8443

I donā€™t know how I missed the colon. unfortunately, that didnā€™t fix it. I made sure that the user and password, and all other variables are correct.

new code

#!/bin/sh

cookie=$(mktemp)
headers=$(mktemp)

curl_cmd="curl --silent --output /dev/null --cookie ${cookie} --cookie-jar ${cookie} --insecure"

# generate new password
NEW_PWD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)

# generate QR code
wget --output-document="/config/www/${5}.png" "http://api.qrserver.com/v1/create-qr-code/?data=WIFI:T:WPA;S:${5};P:${NEW_PWD};H:;&size=100x100"

# authenticate against unifi controller
${curl_cmd} -H 'Content-Type: application/json' -D ${headers} -d "{\"username\":\"${1}\", \"password\":\"${2}\"}" https://${3}:8443/api/login

# change wifi password
${curl_cmd} -k -X PUT https://${3}:8443/api/s/default/rest/wlanconf/${4} -H "Content-Type: application/json" -d "{\"_id\":\"${4}\",\"x_passphrase\":\"${NEW_PWD}\"}"

error

stdout: ""
stderr: "/config/scripts/wifichange.sh: line 2: $'\\r': command not found\n/config/scripts/wifichange.sh: line 5: $'\\r': command not found\n/config/scripts/wifichange.sh: line 7: $'\\r': command not found\n/config/scripts/wifichange.sh: line 10: $'\\r': command not found\nConnecting to api.qrserver.com (95.216.163.127:80)\nwget: server returned error: HTTP/1.1 400 Bad Request\n/config/scripts/wifichange.sh: line 13: $'\\r': command not found\ncurl: option --insecure\r: is unknown\ncurl: try 'curl --help' or 'curl --manual' for more information\n/config/scripts/wifichange.sh: line 16: $'\\r': command not found\ncurl: option --insecure\r: is unknown\ncurl: try 'curl --help' or 'curl --manual' for more information"
returncode: 2

in my troubleshooting, I hardcoded the variableā€™s values and got the same error. then removed the unifi portion of the code and got this error

stdout: ""
stderr: |-
  /config/scripts/wifichange.sh: line 2: $'\r': command not found
  /config/scripts/wifichange.sh: line 5: $'\r': command not found
  /config/scripts/wifichange.sh: line 7: $'\r': command not found
  /config/scripts/wifichange.sh: line 10: $'\r': command not found
  Connecting to api.qrserver.com (159.69.246.187:80)
  wget: server returned error: HTTP/1.1 400 Bad Request
  /config/scripts/wifichange.sh: line 13: $'\r': command not found
  /config/scripts/wifichange.sh: line 16: $'\r': command not found
returncode: 127

I donā€™t know but it seems to me that the problem is in the QR code part

Looks like you have an extra character on every line, specifically \r.

Youā€™ll need to open the script file in some type of editor that you show you non-printable characters to see it and remove it.

Actually, based on the line numbers, it looks like itā€™s complaining about every blank line. Just remove the blank lines and try that.

Hi @Laith_Alsunni ,

were you able to solve it?

thanks

iā€™m wanting to try to implement this on my system, but can i just check what the rationale is for using that online qr generator and the camera entity, rather than the new built in qr generator in the unifi integration? just want to make sure iā€™m understanding this whole thing correctly.

Scroll up.

i actually have read the whole thread but i noted you didnā€™t adjust your original code to use the built in unifi qr display so am curious if thereā€™s a reason for this? is it just because its not giving you a .png that you can manipulate?

Hello,
after many days, Iā€™m still not able to solve the return code 2 error.
i stumbled upon this GitHub - rootnegativ1/unifi-wifi: Home Assistant integration to change wireless passwords on UniFi Network controllers and seems to work really well. If anyone else wanna give it a try.

You read the whole thread?

So let me see if I have this straight - youā€™re asking me why I didnā€™t use the built-in integration that didnā€™t exist when I wrote this code, and was added because of the popularity of what I developed?

I donā€™t even know how to answer that.

yeah i did read the whole thread. before i posted. and again just in case after you replied.

and no, thatā€™s not what iā€™m asking. Iā€™m asking you if thereā€™s a reason you arenā€™t using that built-in integration now that it exists. Because, i noticed you had updated your code as various comments came in through the thread and as you found ways to make it better. This part you didnā€™t update so I was wondering if you had a specific reason you preferred this and wanted to understand why it might be advantageous. Thatā€™s why i said i noticed you didnā€™t ā€œupdateā€ your code to reflect it.

up to you if you want to answer the question, although i donā€™t get the passive aggression here. either way i appreciate your hard work on this it is great.

2 Likes

Ah. My apologies, I literally didnā€™t understand what you were asking. Not passive-aggressive, just extremely confused. LOL

Having said that, maybe you did read the entire thread, but Iā€™m thinking you didnā€™t pay close enough attention to the dates.

My last update to the code was a full year before the integration was released, soā€¦ I guess now I find myself wondering why I would update my code to include code that is already part of an integration that can just easily be added without going through all the manual shenanigans I went through to make it work originally?

So yeah, I guess Iā€™m still a bit confused by the question. LOL

If you want to use the integration, by all means - please do. In fact, I encourage it at this point. My code worked, but it was a bit ā€œdirtyā€, since Iā€™m not a full-time programmer like a lot of these guys are. I know enough to get by, but my formal education was a quarter century ago, and I ultimately went into the systems side of things, so Iā€™ve only ever coded out of necessity, which is what this was.

The integration is much cleaner, and I presume will remain supported for some time to come. If it meets your needs, then I would absolutely go that route.

I continue to be humbled by the fact that my dirty old code written in my basement home office was the genesis for this feature addition to the official integration, but Iā€™m a firm believer in ā€œif it ainā€™t broke, donā€™t fix itā€. It still works just fine in my environment, soā€¦ :wink:

Thank you for the kind words about my hard work, BTW. I definitely appreciate it.

1 Like

I should also add some password re-generation, I feel like that is the missing piece to offer a good guest experience :slight_smile:

I have iterated many times over the code to where it is now, I am only a coder by night and itā€™s been a long time since I worked as it. So donā€™t feel intimidated. It is really fun to work with home assistant.

Oh! I thought that had been added since it was a big part of what I wrote. Oh well, thatā€™ll teach me to not test out the new stuff before recommending it as a full replacement! LOL

And yes, coding is fun, I agree - my issue is that once I get it working, I usually stop there. It could be ugly, inefficient, etc - but if it works, Iā€™m happy. But thatā€™s the difference between developing for yourself vs developing for someone else. :slight_smile: