-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathwifi-diag
157 lines (129 loc) · 4.69 KB
/
wifi-diag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/lua
-- Required modules
require "ubus"
require "luci.util"
local json = require "luci.jsonc"
local nixio = require "nixio"
local http = require("luci.httpclient")
-- ubus show network interface info
local function get_network_info(conn)
local status = conn:call("network.interface", "dump", {})
if not status then
error("Failed to get network interface info")
end
local html = "<h1>Network Interface Info</h1>"
for _, v in ipairs(status.interface) do
html = html .. "<h2>Interface: " .. v.interface .. "</h2>"
html = html .. "<p>Status: " .. (v.up and "UP" or "DOWN") .. "</p>"
html = html .. "<p>Device: " .. (v.device or "N/A") .. "</p>"
html = html .. "<p>Protocol: " .. v.proto .. "</p>"
html = html .. "<p>Available: " .. (v.available and "Yes" or "No") .. "</p>"
if v.uptime then
html = html .. "<p>Uptime: " .. v.uptime .. " seconds</p>"
end
-- IPv4 Addresses
if v["ipv4-address"] and #v["ipv4-address"] > 0 then
html = html .. "<p>IPv4 Address: " .. v["ipv4-address"][1].address .. "/" .. v["ipv4-address"][1].mask .. "</p>"
end
-- Routes
if v.route and #v.route > 0 then
html = html .. "<p>Default Gateway: " .. v.route[1].nexthop .. "</p>"
end
-- DNS Servers
if v["dns-server"] and #v["dns-server"] > 0 then
html = html .. "<p>DNS Server: " .. v["dns-server"][1] .. "</p>"
end
html = html .. "<hr>"
end
return html
end
local function is_internet_connected()
-- Execute curl command
local cmd = 'curl -s -m 5 -L -w "%{http_code}" "http://api64.ipify.org?format=json"'
local handle = io.popen(cmd)
local result = handle:read("*a")
handle:close()
-- Extract HTTP code (last 3 characters) and response body
local body = result:sub(1, -4) -- Remove last 3 chars (HTTP code)
local code = result:sub(-3) -- Get last 3 chars (HTTP code)
-- Check HTTP response code
if code ~= "200" then
return string.format("HTTP request failed with status code: %s", code)
end
-- Parse JSON response
local parsed = json.parse(body)
if not parsed then
return "Failed to parse JSON response"
end
-- Extract IP address
local ip = parsed.ip or "unknown"
return string.format("Internet is connected. Public IP: %s", ip)
end
local function is_authserver_connected()
local cmd = 'curl -s -m 5 -L -w "%{http_code}" "http://portal.sharewifi.cc/pages/portal/portal"'
local handle = io.popen(cmd)
local result = handle:read("*a")
handle:close()
-- Extract HTTP code (last 3 characters) and response body
local body = result:sub(1, -4) -- Remove last 3 chars (HTTP code)
local code = result:sub(-3) -- Get last 3 chars (HTTP code)
-- Check HTTP response code
if code ~= "200" then
return string.format("HTTP request failed with status code: %s", code)
end
return 'Authserver is connected. HTTP status code: 200 OK'
end
local function get_nft_info()
local cmd = "nft list ruleset"
local f = io.popen(cmd)
local output = f:read("*a")
f:close()
return output
end
local function get_wifidogx_info()
local cmd = "wdctlx status"
local f = io.popen(cmd)
local output = f:read("*a")
f:close()
return output
end
local function get_mqtt_watchdog_timestamp()
local file = "/tmp/apfree/mqtt-watchdog"
local f = io.open(file, "r")
if not f then
return "N/A"
end
local timestamp = f:read("*a")
f:close()
-- translate timestamp to human readable format
timestamp = os.date("%c", tonumber(timestamp))
return timestamp
end
local function main()
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubusd")
end
print("Content-Type: text/html\n")
local html = "<html><head><title>Wifi Diagnostics</title></head><body>"
html = html .. get_network_info(conn)
html = html .. "<hr>"
html = html .. "<h1>Internet Connectivity</h1>"
html = html .. is_internet_connected()
html = html .. "<hr>"
html = html .. "<h1>Authserver Connectivity</h1>"
html = html .. is_authserver_connected()
html = html .. "<hr>"
html = html .. "<h1>NFT Ruleset</h1>"
html = html .. "<pre>" .. get_nft_info() .. "</pre>"
html = html .. "<hr>"
html = html .. "<h1>Wifidogx Status</h1>"
html = html .. "<pre>" .. get_wifidogx_info() .. "</pre>"
html = html .. "<hr>"
html = html .. "<h1>MQTT Watchdog Timestamp</h1>"
html = html .. "<p>" .. get_mqtt_watchdog_timestamp() .. "</p>"
html = html .. "</body></html>"
print(html)
conn:close()
end
main()