[Script] Wake on LAN (WOL) Simple Interface Installation

Hi

Created this script to automate PWOL installation.

All instructions taken here:

Adjusted for Gl.iNet (as it uses nginx)


#!/bin/sh

mkdir -p /www/pwol

cat > /www/pwol/index.html << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<link rel="icon" type="image/png" href="/luci-static/bootstrap/favicon.png"/>
<title>Public Wake-On-LAN Interface</title>
<style>
body { text-align: center; }
table { border-collapse: collapse; }
th, td { padding: 1px 5px 1px 5px; border: 1px solid black; }
button { margin: 1px 2px 1px 2px; }
.red { color: #f00; }.blue { color: #00f; }.text-center { text-align: center }.w-100 { width: 100% }.w-80 { width: 80% }.elem-center { margin-left: auto; margin-right: auto; }
</style>
</head>

<body>
<h1>Public Wake-On-LAN Interface</h1>
<div id="err_cont" class="center red" hidden>error text</div>
<table id="dev_list" class="elem-center">
    <tr><th>Hostname</th><th>MAC Addr</th><th>IP Addr</th><th>Status</th><th>Action</th></tr>
</table>
<script src="script.js"></script>
</body>

</html>
EOF

cat > /www/pwol/script.js << EOF
'use strict';

const {error} = console;
const devices = [];
const api_ep = "/cgi-bin/pwol-ctl";
const srr = "/luci-static/resources/";

Object.defineProperties(Date, { now_sec: { enumerable: true, get() { return (Date.now()/1000) >>> 0; }, } });

class NwkDevice {
    // Class definition...
};

function api_get(req) { /* Function definition... */ };
function show_error(msg, err) { /* Function definition... */ };
function create_dev_table(resp) { /* Function definition... */ };

window.addEventListener('DOMContentLoaded', function main(evt) {
    api_get({ cmd: "hl" }).then(create_dev_table).catch(show_error.bind(this, "ERROR: Failed to read states of devices"));
});
EOF

ln -s /www/pwol/api.lua /www/cgi-bin/pwol-ctl

cat > /www/pwol/api.lua << EOF
#!/usr/bin/lua
require("uci");
require("nixio");
require("luci.jsonc");
require("luci.http");
require("luci.ip");

local dev_list_path = "/www/pwol/dev_list.txt";

function contains(arr, val) /* Function definition... */ end;
function to_bool(val) /* Function definition... */ end;
function is_online(ip) /* Function definition... */ end;
function read_pub_hosts_list(file_path) /* Function definition... */ end;

local host_info = {};  -- Host information...

local host_ctl = {
    ['hl'] = function () /* Function definition... */ end,
    ['hs'] = function (host) /* Function definition... */ end,
    ['hw'] = function (host) /* Function definition... */ end,
};

local args = luci.http.urldecode_params(nixio.getenv('QUERY_STRING') or arg[1] or "cmd=hl");
local ret = {
    ['ok'] = false,
};

repeat
    local public_hosts = read_pub_hosts_list(dev_list_path);
    if (not public_hosts) then ret.err = "Failed reading list of available hosts"; break; end;

    for k,v in pairs(uci.get_all('dhcp')) do
        if (contains(public_hosts, v.name)) then host_info[v.name] = { mac = v.mac, ip = v.ip }; end;
    end;

    local cmd = host_ctl[args.cmd];
    if (not cmd) then ret.err = "Unknown command"; break; end;
    local data = cmd(args.host);
    if (not data) then ret.err = "Unknown host"; break; end;
    ret.ok = true;
    ret.data = data;
until (true);

print("Status: 200 OK");
print("Content-type: application/json\n");
print(luci.jsonc.stringify(ret, '\t'));
EOF

touch /www/pwol/dev_list.txt

echo "PWOL setup completed."

2 Likes

Thank you for your sharing!

Please let me edit the thread title to make it make it easier to find out and benefit for more people!