|
1 |
| -#!/bin/sh |
2 |
| - |
3 |
| -a=$(ls /sys/class/net/*/device/uevent | grep eth | awk -F '/' '{print $5}') |
4 |
| -b=$(echo "$a" | wc -l) |
5 |
| -rm -f /tmp/state/ethinfo |
6 |
| - |
7 |
| -echo -n "[" > /tmp/state/ethinfo |
8 |
| - |
9 |
| -for i in $(seq 1 $b) |
10 |
| -do |
11 |
| - h=$(echo '{"name":' ) |
12 |
| - c=$(echo "$a" | sed -n ${i}p) |
13 |
| - d=$(ethtool $c) |
14 |
| - |
15 |
| - e=$(echo "$d" | grep "Link detected" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g') |
16 |
| - if [ $e = yes ]; then |
17 |
| - l=1 |
18 |
| - else |
19 |
| - l=0 |
20 |
| - fi |
21 |
| - |
22 |
| - f=$(echo "$d" | grep "Speed" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g' | tr -d "Unknown!") |
23 |
| - [ -z "$f" ] && f=" - " |
24 |
| - |
25 |
| - g=$(echo "$d" | grep "Duplex" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g') |
26 |
| - if [ "$g" == "Full" ]; then |
27 |
| - x=1 |
28 |
| - else |
29 |
| - x=0 |
30 |
| - fi |
31 |
| - |
32 |
| - echo -n "$h \"$c\", \"status\": $l, \"speed\": \"$f\", \"duplex\": $x}," >> /tmp/state/ethinfo |
33 |
| -done |
34 |
| - |
35 |
| -sed -i 's/.$//' /tmp/state/ethinfo |
36 |
| - |
37 |
| -echo -n "]" >> /tmp/state/ethinfo |
38 |
| - |
39 |
| -cat /tmp/state/ethinfo |
| 1 | +#!/usr/bin/lua |
| 2 | +-- Copyright (C) 2022 Tianling Shen <[email protected]> |
| 3 | + |
| 4 | +local util = require "luci.util" |
| 5 | +local jsonc = require "luci.jsonc" |
| 6 | + |
| 7 | +local eth_info = {} |
| 8 | +local ifname, stat |
| 9 | +for ifname, stat in pairs(util.ubus("network.device", "status")) do |
| 10 | + if ifname:match("^(eth%d+)$") == ifname then |
| 11 | + local status, speed, duplex |
| 12 | + |
| 13 | + status = stat.carrier and 1 or 0 |
| 14 | + |
| 15 | + if stat.speed:sub(1, 1) == "-" then |
| 16 | + speed = " - " |
| 17 | + else |
| 18 | + speed = stat.speed:sub(1, -2) .. "Mb/s" |
| 19 | + end |
| 20 | + |
| 21 | + if stat.carrier and stat.speed:sub(-1) == "F" then |
| 22 | + duplex = 1 |
| 23 | + else |
| 24 | + duplex = 0 |
| 25 | + end |
| 26 | + |
| 27 | + eth_info[#eth_info+1] = { name = ifname, status = status, |
| 28 | + speed = speed, duplex = duplex } |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +table.sort(eth_info, |
| 33 | + function(a, b) |
| 34 | + return a.name < b.name |
| 35 | + end) |
| 36 | + |
| 37 | +print(jsonc.stringify(eth_info)) |
0 commit comments