autocore: ethinfo: rewritten in lua

This commit is contained in:
lean 2022-09-21 18:12:59 +08:00
parent ac7d6a4778
commit 4f056f8ef3

View File

@ -1,39 +1,37 @@
#!/bin/sh #!/usr/bin/lua
-- Copyright (C) 2022 Tianling Shen <cnsztl@immortalwrt.org>
a=$(ls /sys/class/net/*/device/uevent | grep eth | awk -F '/' '{print $5}') local util = require "luci.util"
b=$(echo "$a" | wc -l) local jsonc = require "luci.jsonc"
rm -f /tmp/state/ethinfo
echo -n "[" > /tmp/state/ethinfo local eth_info = {}
local ifname, stat
for ifname, stat in pairs(util.ubus("network.device", "status")) do
if ifname:match("^(eth%d+)$") == ifname then
local status, speed, duplex
for i in $(seq 1 $b) status = stat.carrier and 1 or 0
do
h=$(echo '{"name":' )
c=$(echo "$a" | sed -n ${i}p)
d=$(ethtool $c)
e=$(echo "$d" | grep "Link detected" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g') if stat.speed:sub(1, 1) == "-" then
if [ $e = yes ]; then speed = " - "
l=1
else else
l=0 speed = stat.speed:sub(1, -2) .. "Mb/s"
fi end
f=$(echo "$d" | grep "Speed" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g' | tr -d "Unknown!") if stat.carrier and stat.speed:sub(-1) == "F" then
[ -z "$f" ] && f=" - " duplex = 1
g=$(echo "$d" | grep "Duplex" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g')
if [ "$g" == "Full" ]; then
x=1
else else
x=0 duplex = 0
fi end
echo -n "$h \"$c\", \"status\": $l, \"speed\": \"$f\", \"duplex\": $x}," >> /tmp/state/ethinfo eth_info[#eth_info+1] = { name = ifname, status = status,
done speed = speed, duplex = duplex }
end
end
sed -i 's/.$//' /tmp/state/ethinfo table.sort(eth_info,
function(a, b)
return a.name < b.name
end)
echo -n "]" >> /tmp/state/ethinfo print(jsonc.stringify(eth_info))
cat /tmp/state/ethinfo