Advice wanted for a Lua script to dynamically change wireguard endpoints

My below Lua script kinda works insofar that it does change the endpoint host and the related public key. That is to say these params show up modified in "uci show network". For example a german mullvad server is selected, while I started of with a swiss one.
But "what is my ip address.com" still reports the swiss server. That's not good.
In addition when running the code this error pops up twice : uci: Entry not found.
Which is strange coz the params appear to have been modified. Luci confirms this.

I'm not new to scripting but I'm totally to Lua and UCI. Thanks for any pointers.



--This Lua script selects a random wireguard endpoint and its associated public key and
--refreshes your config. Schedule it using cron. To run it:  lua scriptname.lua
--You should modify 2 things in the PARAMETERS section :
--      1. change the list of endpoints & their public keys to your preference
--      2. find the name of your wireguard interface using "uci show network" and
--          populate the variable "wireguard_iface" accordingly.

endpoints = {}
endpoints.dk2 = 'egl+0TkpFU39F5O6r6+hIBMPQLOa8/t5CymOZV6CC3Y='
endpoints.dk3 = 'R5LUBgM/1UjeAR4lt+L/yA30Gee6/VqVZ9eAB3ZTajs='
endpoints.fi1 = 'rGL76wTvmRKI2f8VHdGEZnTQbwRG4+RTO1sokGouVGU='
endpoints.fi2 = 'PbgdQd49ejV2cbyEvbufUNLGczh0cq2P5E77qbm8M3A='
endpoints.fi3 = 'CYFNNfntzhRHn0+QHWv2eBOeU/sCZ5f6FLbTMUyIQHk='
endpoints.fr4 = 'ov323GyDOEHLT0sNRUUPYiE3BkvFDjpmi1a4fzv49hE='
endpoints.fr5 = 'R5Ve+PJD24QjNXi2Dim7szwCiOLnv+6hg+WyTudAYmE='
endpoints.fr6 = 'w4r/o6VImF7l0/De3JpOGnpzjAFv9wcCu8Rop5eZkWc='
endpoints.de5 = 'bRcOjl6Yc+2x0dHCO6eSGoo9Y9euv9DljDJEYoLA0ks='
endpoints.de7 = '+0BEfUZ3D0DEM/fJVPUUhYYDdkkLjqedVerm8dV4bmE='
endpoints.de8 = 'XIge3HgGEHf52e4Jpzk8iFOrrp6q7trq0udhufFlDVo='
endpoints.nl3 = 'hnRorSW0YHlHAzGb4Uc/sjOqQIrqDnpJnTQi/n7Rp1c='
endpoints.nl4 = 'hnRyse6QxPPcZOoSwRsHUtK1W+APWXnIoaDTmH6JsHQ='
endpoints.nl5 = '33BoONMGCm2vknq2eq72eozRsHmHQY6ZHEEZ4851TkY='
endpoints.no2 = 'IhhpKphSFWpwja1P4HBctZ367G3Q53EgdeFGZro29Tc='
endpoints.no3 = 'zOBWmQ3BEOZKsYKbj4dC2hQjxCbr3eKa6wGWyEDYbC4='
endpoints.no4 = 'veeEoYS9a2T6K8WMs/MvRCdNJG580XbhnLfbFjp3B0M='
endpoints.se23 = 'XscA5gebj51nmhAr6o+aUCnMHWGjbS1Gvvd0tuLRiFE='
endpoints.se28 = 'V6RHmYEXDDXvCPZENmhwk5VEn6KgSseTFHw/IkXFzGg='
endpoints.se13 = '615mnBGkvjZnD/vRbyL/6da7YhtctfB+jimN+wfV724='
endpoints.ch7 = '5Ms10UxGjCSzwImTrvEjcygsWY8AfMIdYyRvgFuTqH8='
endpoints.ch8 = 'C3jAgPirUZG6sNYe4VuAgDEYunENUyG34X42y+SBngQ='
endpoints.ch9 = 'dV/aHhwG0fmp0XuvSvrdWjCtdyhPDDFiE/nuv/1xnRM='

wireguard_iface = 'wireguard_WGmullvad[0]'


idx = 0 ; arr_endpoints = {}
for k,v in pairs(endpoints) do
    idx = idx + 1
    arr_endpoints[idx] = { k,v }
end

math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)))
math.random(); math.random(); math.random()
selected_idx = math.random(idx)
selected_endpoint = arr_endpoints[selected_idx][1]..'-wireguard.mullvad.net'
selected_pubkey = arr_endpoints[selected_idx][2]

os.execute("uci set network.@"..wireguard_iface..".endpoint_host='"..selected_endpoint.."'")
os.execute("uci set network.@"..wireguard_iface..".public_key='"..selected_pubkey.."'")
os.execute("uci commit")
os.execute("/etc/init.d/network restart")


The commit command was incomplete, once I it replaced with

os.execute("uci commit network")

everything worked and no errors

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.