51

Odp: Skrypt /sbin/sysinfo.sh

Cezary napisał/a:

Pojawia się tylko raz podczas zalogowania się przez ssh do routera (w sensie wołane z automatu). Jak uruchamiam z palca to jest już normalnie. Gdzieś spacje wchodzą...

Zrobiłem wyświetlanie linii tak że treść nadpisuje kreski (pipe-y) więc to nie możliwe żeby były odsunięte. Może inny skrypt?

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

52

Odp: Skrypt /sbin/sysinfo.sh

Witam,
oto następna wersja skryptu:

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta bledu wysw. zajetosci Flash-a, dodanie kolorow
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkreśleń
#    1.07    RD    Modyfikacja związana z poprawnym wyświetlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wyświetlania DNS-ów dla wan, dodanie uptime dla interfejsów
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local Rouler
local NormalColor
local MachineColor
local ValueColor
local AddrColor
local RXTXColor

initialize() {
    local ColorMode="1"
    for Parameter in $@; do
        case  $Parameter  in
        -m) ColorMode="0";;
        -sr) StartRuler="0";;
        -er) EndRuler="0";;
        -w1) Width=80;;
        -w2) Width=100;;
        -w3) Width=120;;
        -h|*)    
            echo "Usage: $0 - [parameter]"
            echo "    -h    : This help."
            echo "    -m    : Display mono version."
            echo "    -sr    : Without start horizontal ruler."    
            echo "    -er    : Without end horizontal ruler."    
            exit 1;;
        esac
    done
    if [ "$ColorMode" == "1" ]; then
        NormalColor="\e[0m"
        MachineColor="\e[0;33m"
        ValueColor="\e[1;36m"
        AddrColor="\e[1;31m"
        RXTXColor="\e[2;32m"
    else
        NormalColor="\e[0m"
        MachineColor="\e[7m"
        ValueColor="\e[1m"
        AddrColor="\e[4m"
        RXTXColor="\e[1m"
    fi
    local i
    for i in $(seq $(expr $Width + 4 )); do 
        Rouler="$Rouler-";
    done
}

human_readable() {
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
    else
        printf "0B"
    fi
}

device_rx_tx() {
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%d\t%d",$2,$10}' /proc/net/dev)
    [ "$RXTX" != "" ] && printf "rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() {
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() {
    printf " | %-${Width}s |\r | $1\n"
}

print_horizontal_ruler() {
    printf " $Rouler\n"
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model)
    print_line "Machine: $MachineColor$Machine$NormalColor, Name: $MachineColor$HostName$NormalColor"
}

print_uptime() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line "Uptime: $ValueColor$Uptime$NormalColor, Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"%s, %s, %s",$1,$2,$3}' /proc/loadavg)
    print_line "Load: $ValueColor$LoadAvg$NormalColor"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%d\t%d\t%.1f",$4*1024,$2*1024,($2>0)?$3/$2*100:0}')
    local Free=$(echo "$Flash" | cut -f 1)
    local Total=$(echo "$Flash" | cut -f 2)
    local Used=$(echo "$Flash" | cut -f 3)
    print_line "Flash: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{printf"%d\t%d\t%.1f",Free*1024,Total*1024,(Total>0)?(((Total-Free)/Total)*100):0}' /proc/meminfo)
    local Free=$(echo "$Memory" | cut -f 1)
    local Total=$(echo "$Memory" | cut -f 2)
    local Used=$(echo "$Memory" | cut -f 3)
    print_line "Memory: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        for Device in $(uci -q get firewall.$Zone.network); do
            local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
            if [ "$Status" != "" ]; then
                local State=""
                local Iface=""
                local Uptime=""
                local IP4=""
                local IP6=""
                local Subnet4=""
                local Subnet6=""
                local Gateway4="n/a"
                local Gateway6="n/a"
                local DNS=""
                local Protocol=""
                json_load "${Status:-{}}"
                json_get_var State up
                json_get_var Uptime uptime
                json_get_var Iface l3_device
                json_get_var Protocol proto
                if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                    json_select ipv4_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP4 address
                        json_get_var Subnet4 mask
                        [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                    fi
                fi
                json_select
                if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                    json_select ipv6_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP6 address
                        json_get_var Subnet6 mask
                        [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                    fi
                fi
                json_select
                if json_get_type Status route && [ "$Status" = array ]; then
                    json_select route
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = object ]; do
                        json_select "$((Index++))"
                        json_get_var Status target
                        case "$Status" in
                            0.0.0.0)
                                json_get_var Gateway4 nexthop;;
                            ::)
                                json_get_var Gateway6 nexthop;;
                        esac
                        json_select ".."
                    done    
                fi
                json_select
                if json_get_type Status dns_server && [ "$Status" = array ]; then
                    json_select dns_server
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = string ]; do
                        json_get_var Status "$((Index++))"
                        DNS="${DNS:+$DNS }$Status"
                    done
                fi
                if [ "$State" == "1" ]; then
                    [ "$IP4" != "" ] && print_line "WAN: $AddrColor$IP4$NormalColor($Iface), gateway: $AddrColor$Gateway4$NormalColor"
                    [ "$IP6" != "" ] && print_line "WAN: $AddrColor$IP6$NormalColor($Iface), gateway: $AddrColor$Gateway6$NormalColor"
                    print_line "proto: $ValueColor$Protocol$NormalColor, uptime: $ValueColor$(uptime_str $Uptime)$NormalColor, $(device_rx_tx $Iface)"
                    [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                fi
            fi
        done
    done
}

print_lan() {
    local Device="lan"
    local State
    local Iface
    local IP4
    local IP6
    local Subnet4
    local Subnet6
    local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
    if [ "$Status" != "" ]; then
        json_load "${Status:-{}}"
        json_get_var State up
        json_get_var Iface device
        if json_get_type Status ipv4_address && [ "$Status" = array ]; then
            json_select ipv4_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP4 address
                json_get_var Subnet4 mask
                [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
            fi
        fi
        json_select
        if json_get_type Status ipv6_address && [ "$Status" = array ]; then
            json_select ipv6_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP6 address
                json_get_var Subnet6 mask
                [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
            fi
        fi
        [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor"
        [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor"
    fi
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
                fi
            fi
            print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor, conn: $ValueColor$Connection$NormalColor, $(device_rx_tx $RadioIface)"
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            Mode=$(uci -q get openvpn.$VPN.mode)
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line "VPN: $Mode, conn: $ValueColor$Connection$NormalColor, $(device_rx_tx $Device)"
        fi
    done
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_uptime
print_loadavg
print_flash
print_memory
print_wan
print_lan
print_wlan
print_vpn
[ "$EndRuler" == "1" ] && print_horizontal_ruler
exit 
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

53

Odp: Skrypt /sbin/sysinfo.sh

Wygląda na to że wyświetla wszystko jak trzeba.

Masz niepotrzebny router, uszkodzony czy nie - chętnie przygarnę go.

54

Odp: Skrypt /sbin/sysinfo.sh

Czyli faza RTM?;)

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

55

Odp: Skrypt /sbin/sysinfo.sh

Daj szansę innym, może się coś znajdzie jeszcze.

Masz niepotrzebny router, uszkodzony czy nie - chętnie przygarnę go.

56

Odp: Skrypt /sbin/sysinfo.sh

A mam. Jak jest jako klient to wyświetlają się dodatkowe informacje i linia te 60 znaków przekracza. Chyba dla sta warto z tego nową linię zrobić po prostu.

Masz niepotrzebny router, uszkodzony czy nie - chętnie przygarnę go.

57

Odp: Skrypt /sbin/sysinfo.sh

Mam propozycję, żeby przed oboma ($Iface) oraz ($Mode) postawić spację, bo są sklejone z poprzedzającym napisem.

Gargoyle fanatic user
WR1043ND v2 16MB / WR2543ND / WR1043ND v1 / MR3020 16MB|64MB / WR703N 8MB|64MB / WT3020 / E3372 / E3131

58

Odp: Skrypt /sbin/sysinfo.sh

Zamierzone to sklejenie:), jeżeli to kogoś jeszcze będzie raziło to zmienię.

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

59

Odp: Skrypt /sbin/sysinfo.sh

Nie wiedziałem, nie pisałbym głupot i zaśmiecał forum smile

Gargoyle fanatic user
WR1043ND v2 16MB / WR2543ND / WR1043ND v1 / MR3020 16MB|64MB / WR703N 8MB|64MB / WT3020 / E3372 / E3131

60

Odp: Skrypt /sbin/sysinfo.sh

A tak poważnie to dla mnie wszystkie opinie i uwagi są cenne, więc pisz śmiało.

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

61 (edytowany przez Rafciq 2013-05-27 22:18:11)

Odp: Skrypt /sbin/sysinfo.sh

Cezary napisał/a:

A mam. Jak jest jako klient to wyświetlają się dodatkowe informacje i linia te 60 znaków przekracza. Chyba dla sta warto z tego nową linię zrobić po prostu.

Witam,
zrobiłem zmianę, ale nie mam obecnie takiej konfiguracji żeby sprawdzić.

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta bledu wysw. zajetosci Flash-a, dodanie kolorow
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkreśleń
#    1.07    RD    Modyfikacja związana z poprawnym wyświetlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wyświetlania DNS-ów dla wan, dodanie uptime dla interfejsów
#    1.09    RD    Dodanie statusu "Down" dla wyłączonego wifi, zmiana wyświetlania dla WLAN(sta)
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local Rouler
local NormalColor
local MachineColor
local ValueColor
local AddrColor
local RXTXColor

initialize() {
    local ColorMode="1"
    for Parameter in $@; do
        case  $Parameter  in
        -m) ColorMode="0";;
        -sr) StartRuler="0";;
        -er) EndRuler="0";;
        -w1) Width=80;;
        -w2) Width=100;;
        -w3) Width=120;;
        -h|*)    
            echo "Usage: $0 - [parameter]"
            echo "    -h    : This help."
            echo "    -m    : Display mono version."
            echo "    -sr    : Without start horizontal ruler."    
            echo "    -er    : Without end horizontal ruler."    
            exit 1;;
        esac
    done
    if [ "$ColorMode" == "1" ]; then
        NormalColor="\e[0m"
        MachineColor="\e[0;33m"
        ValueColor="\e[1;36m"
        AddrColor="\e[1;31m"
        RXTXColor="\e[2;32m"
    else
        NormalColor="\e[0m"
        MachineColor="\e[7m"
        ValueColor="\e[1m"
        AddrColor="\e[4m"
        RXTXColor="\e[1m"
    fi
    local i
    for i in $(seq $(expr $Width + 4 )); do 
        Rouler="$Rouler-";
    done
}

human_readable() {
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
    else
        printf "0B"
    fi
}

device_rx_tx() {
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%d\t%d",$2,$10}' /proc/net/dev)
    [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() {
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() {
    printf " | %-${Width}s |\r | $1\n"
}

print_horizontal_ruler() {
    printf " $Rouler\n"
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model)
    print_line "Machine: $MachineColor$Machine$NormalColor, Name: $MachineColor$HostName$NormalColor"
}

print_uptime() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line "Uptime: $ValueColor$Uptime$NormalColor, Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"%s, %s, %s",$1,$2,$3}' /proc/loadavg)
    print_line "Load: $ValueColor$LoadAvg$NormalColor"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%d\t%d\t%.1f",$4*1024,$2*1024,($2>0)?$3/$2*100:0}')
    local Free=$(echo "$Flash" | cut -f 1)
    local Total=$(echo "$Flash" | cut -f 2)
    local Used=$(echo "$Flash" | cut -f 3)
    print_line "Flash: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{printf"%d\t%d\t%.1f",Free*1024,Total*1024,(Total>0)?(((Total-Free)/Total)*100):0}' /proc/meminfo)
    local Free=$(echo "$Memory" | cut -f 1)
    local Total=$(echo "$Memory" | cut -f 2)
    local Used=$(echo "$Memory" | cut -f 3)
    print_line "Memory: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        for Device in $(uci -q get firewall.$Zone.network); do
            local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
            if [ "$Status" != "" ]; then
                local State=""
                local Iface=""
                local Uptime=""
                local IP4=""
                local IP6=""
                local Subnet4=""
                local Subnet6=""
                local Gateway4="n/a"
                local Gateway6="n/a"
                local DNS=""
                local Protocol=""
                json_load "${Status:-{}}"
                json_get_var State up
                json_get_var Uptime uptime
                json_get_var Iface l3_device
                json_get_var Protocol proto
                if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                    json_select ipv4_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP4 address
                        json_get_var Subnet4 mask
                        [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                    fi
                fi
                json_select
                if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                    json_select ipv6_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP6 address
                        json_get_var Subnet6 mask
                        [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                    fi
                fi
                json_select
                if json_get_type Status route && [ "$Status" = array ]; then
                    json_select route
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = object ]; do
                        json_select "$((Index++))"
                        json_get_var Status target
                        case "$Status" in
                            0.0.0.0)
                                json_get_var Gateway4 nexthop;;
                            ::)
                                json_get_var Gateway6 nexthop;;
                        esac
                        json_select ".."
                    done    
                fi
                json_select
                if json_get_type Status dns_server && [ "$Status" = array ]; then
                    json_select dns_server
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = string ]; do
                        json_get_var Status "$((Index++))"
                        DNS="${DNS:+$DNS }$Status"
                    done
                fi
                if [ "$State" == "1" ]; then
                    [ "$IP4" != "" ] && print_line "WAN: $AddrColor$IP4$NormalColor($Iface), gateway: $AddrColor$Gateway4$NormalColor"
                    [ "$IP6" != "" ] && print_line "WAN: $AddrColor$IP6$NormalColor($Iface), gateway: $AddrColor$Gateway6$NormalColor"
                    print_line "proto: $ValueColor$Protocol$NormalColor, uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
                    [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                fi
            fi
        done
    done
}

print_lan() {
    local Device="lan"
    local State
    local Iface
    local IP4
    local IP6
    local Subnet4
    local Subnet6
    local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
    if [ "$Status" != "" ]; then
        json_load "${Status:-{}}"
        json_get_var State up
        json_get_var Iface device
        if json_get_type Status ipv4_address && [ "$Status" = array ]; then
            json_select ipv4_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP4 address
                json_get_var Subnet4 mask
                [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
            fi
        fi
        json_select
        if json_get_type Status ipv6_address && [ "$Status" = array ]; then
            json_select ipv6_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP6 address
                json_get_var Subnet6 mask
                [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
            fi
        fi
        [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor"
        [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor"
    fi
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            local Connection="Down"
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
                fi
            fi
            if [ "$Mode" == "ap" ]; then
                print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor, conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            else
                print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor(device_rx_tx $RadioIface)"
                print_line "conn: $ValueColor$Connection$NormalColor"
            fi
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            local Mode=$(uci -q get openvpn.$VPN.mode)
            local Connection="n/a"
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line "VPN: $Mode, conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
        fi
    done
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_uptime
print_loadavg
print_flash
print_memory
print_wan
print_lan
print_wlan
print_vpn
[ "$EndRuler" == "1" ] && print_horizontal_ruler
exit 
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

62

Odp: Skrypt /sbin/sysinfo.sh

Witam,
zrobiłem drobną korektę wyświetlania.

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta bledu wysw. zajetosci Flash-a, dodanie kolorow
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkre¶leń
#    1.07    RD    Modyfikacja zwi±zana z poprawnym wy¶wietlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wy¶wietlania DNS-ów dla wan, dodanie uptime dla interfejsów
#    1.09    RD    Dodanie statusu "Down" dla wył±czonego wifi, zmiana wy¶wietlania dla WLAN(sta)
#    1.10    RD    Korekta wy¶wietlania dla WLAN(sta)
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local Rouler
local NormalColor
local MachineColor
local ValueColor
local AddrColor
local RXTXColor

initialize() {
    local ColorMode="1"
    for Parameter in $@; do
        case  $Parameter  in
        -m) ColorMode="0";;
        -sr) StartRuler="0";;
        -er) EndRuler="0";;
        -w1) Width=80;;
        -w2) Width=100;;
        -w3) Width=120;;
        -h|*)    
            echo "Usage: $0 - [parameter]"
            echo "    -h    : This help."
            echo "    -m    : Display mono version."
            echo "    -sr    : Without start horizontal ruler."    
            echo "    -er    : Without end horizontal ruler."    
            exit 1;;
        esac
    done
    if [ "$ColorMode" == "1" ]; then
        NormalColor="\e[0m"
        MachineColor="\e[0;33m"
        ValueColor="\e[1;36m"
        AddrColor="\e[1;31m"
        RXTXColor="\e[2;32m"
    else
        NormalColor="\e[0m"
        MachineColor="\e[7m"
        ValueColor="\e[1m"
        AddrColor="\e[4m"
        RXTXColor="\e[1m"
    fi
    local i
    for i in $(seq $(expr $Width + 4 )); do 
        Rouler="$Rouler-";
    done
}

human_readable() {
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
    else
        printf "0B"
    fi
}

device_rx_tx() {
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%d\t%d",$2,$10}' /proc/net/dev)
    [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() {
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() {
    printf " | %-${Width}s |\r | $1\n"
}

print_horizontal_ruler() {
    printf " $Rouler\n"
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model)
    print_line "Machine: $MachineColor$Machine$NormalColor, Name: $MachineColor$HostName$NormalColor"
}

print_uptime() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line "Uptime: $ValueColor$Uptime$NormalColor, Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"%s, %s, %s",$1,$2,$3}' /proc/loadavg)
    print_line "Load: $ValueColor$LoadAvg$NormalColor"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%d\t%d\t%.1f",$4*1024,$2*1024,($2>0)?$3/$2*100:0}')
    local Free=$(echo "$Flash" | cut -f 1)
    local Total=$(echo "$Flash" | cut -f 2)
    local Used=$(echo "$Flash" | cut -f 3)
    print_line "Flash: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{printf"%d\t%d\t%.1f",Free*1024,Total*1024,(Total>0)?(((Total-Free)/Total)*100):0}' /proc/meminfo)
    local Free=$(echo "$Memory" | cut -f 1)
    local Total=$(echo "$Memory" | cut -f 2)
    local Used=$(echo "$Memory" | cut -f 3)
    print_line "Memory: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        for Device in $(uci -q get firewall.$Zone.network); do
            local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
            if [ "$Status" != "" ]; then
                local State=""
                local Iface=""
                local Uptime=""
                local IP4=""
                local IP6=""
                local Subnet4=""
                local Subnet6=""
                local Gateway4="n/a"
                local Gateway6="n/a"
                local DNS=""
                local Protocol=""
                json_load "${Status:-{}}"
                json_get_var State up
                json_get_var Uptime uptime
                json_get_var Iface l3_device
                json_get_var Protocol proto
                if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                    json_select ipv4_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP4 address
                        json_get_var Subnet4 mask
                        [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                    fi
                fi
                json_select
                if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                    json_select ipv6_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP6 address
                        json_get_var Subnet6 mask
                        [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                    fi
                fi
                json_select
                if json_get_type Status route && [ "$Status" = array ]; then
                    json_select route
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = object ]; do
                        json_select "$((Index++))"
                        json_get_var Status target
                        case "$Status" in
                            0.0.0.0)
                                json_get_var Gateway4 nexthop;;
                            ::)
                                json_get_var Gateway6 nexthop;;
                        esac
                        json_select ".."
                    done    
                fi
                json_select
                if json_get_type Status dns_server && [ "$Status" = array ]; then
                    json_select dns_server
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = string ]; do
                        json_get_var Status "$((Index++))"
                        DNS="${DNS:+$DNS }$Status"
                    done
                fi
                if [ "$State" == "1" ]; then
                    [ "$IP4" != "" ] && print_line "WAN: $AddrColor$IP4$NormalColor($Iface), gateway: $AddrColor$Gateway4$NormalColor"
                    [ "$IP6" != "" ] && print_line "WAN: $AddrColor$IP6$NormalColor($Iface), gateway: $AddrColor$Gateway6$NormalColor"
                    print_line "proto: $ValueColor$Protocol$NormalColor, uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
                    [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                fi
            fi
        done
    done
}

print_lan() {
    local Device="lan"
    local State
    local Iface
    local IP4
    local IP6
    local Subnet4
    local Subnet6
    local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
    if [ "$Status" != "" ]; then
        json_load "${Status:-{}}"
        json_get_var State up
        json_get_var Iface device
        if json_get_type Status ipv4_address && [ "$Status" = array ]; then
            json_select ipv4_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP4 address
                json_get_var Subnet4 mask
                [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
            fi
        fi
        json_select
        if json_get_type Status ipv6_address && [ "$Status" = array ]; then
            json_select ipv6_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP6 address
                json_get_var Subnet6 mask
                [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
            fi
        fi
        [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor"
        [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor"
    fi
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            local Connection="Down"
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
                fi
            fi
            if [ "$Mode" == "ap" ]; then
                print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor, conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            else
                print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor"
                print_line "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            fi
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            local Mode=$(uci -q get openvpn.$VPN.mode)
            local Connection="n/a"
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line "VPN: $Mode, conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
        fi
    done
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_uptime
print_loadavg
print_flash
print_memory
print_wan
print_lan
print_wlan
print_vpn
[ "$EndRuler" == "1" ] && print_horizontal_ruler
exit 0
# Done.
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

63

Odp: Skrypt /sbin/sysinfo.sh

Skoro coś się jeszcze dzieje w temacie, to chciałem zgłosić, że nadal coś nie tak mam z  info o miejscu we flash (extroot na pendrive) - skrypt podaje free: 2GB, total: 2GB, used: 3,6%, mimo że:
df -k zwraca
Filesystem           1K-blocks      Used Available Use% Mounted on
rootfs                 3934288    142476   3791812   4% /
/dev/root                 2560      2560         0 100% /rom
tmpfs                    14592       580     14012   4% /tmp
tmpfs                      512         0       512   0% /dev
/dev/sdb1              3934288    142476   3791812   4% /overlay
overlayfs:/overlay     3934288    142476   3791812   4% /

Gdy podmienię w skrypcie na df -h i pozbędę się *1024, to wartości daje dobre, ale human_readable przekłamuje wtedy jednoski...

64

Odp: Skrypt /sbin/sysinfo.sh

Dzięki, za info @dopsz. Kolejna wersja:

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta błędu wyśw. zajetości Flash-a, dodanie kolorów
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie @mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkreśleń
#    1.07    RD    Modyfikacja związana z poprawnym wyświetlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wyświetlania DNS-ów dla wan, dodanie uptime dla interfejsów
#    1.09    RD    Dodanie statusu "Down" dla wyłączonego wifi, zmiana wyświetlania dla WLAN(sta)
#    1.10    RD    Korekta wyświetlania dla WLAN(sta)
#    1.11    RD    Korekta wyświetlania stanu pamięci, sugestie @dopsz 
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local Rouler
local NormalColor
local MachineColor
local ValueColor
local AddrColor
local RXTXColor

initialize() {
    local ColorMode="1"
    for Parameter in $@; do
        case  $Parameter  in
        -m) ColorMode="0";;
        -sr) StartRuler="0";;
        -er) EndRuler="0";;
        -w1) Width=80;;
        -w2) Width=100;;
        -w3) Width=120;;
        -h|*)    
            echo "Usage: $0 - [parameter]"
            echo "    -h    : This help."
            echo "    -m    : Display mono version."
            echo "    -sr    : Without start horizontal ruler."    
            echo "    -er    : Without end horizontal ruler."    
            exit 1;;
        esac
    done
    if [ "$ColorMode" == "1" ]; then
        NormalColor="\e[0m"
        MachineColor="\e[0;33m"
        ValueColor="\e[1;36m"
        AddrColor="\e[1;31m"
        RXTXColor="\e[2;32m"
    else
        NormalColor="\e[0m"
        MachineColor="\e[7m"
        ValueColor="\e[1m"
        AddrColor="\e[4m"
        RXTXColor="\e[1m"
    fi
    local i
    for i in $(seq $(expr $Width + 4 )); do 
        Rouler="$Rouler-";
    done
}

human_readable() {
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
    else
        printf "0B"
    fi
}

device_rx_tx() {
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%.0f\t%.0f",$2,$10}' /proc/net/dev)
    [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() {
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() {
    printf " | %-${Width}s |\r | $1\n"
}

print_horizontal_ruler() {
    printf " $Rouler\n"
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model)
    print_line "Machine: $MachineColor$Machine$NormalColor, Name: $MachineColor$HostName$NormalColor"
}

print_uptime() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line "Uptime: $ValueColor$Uptime$NormalColor, Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"%s, %s, %s",$1,$2,$3}' /proc/loadavg)
    print_line "Load: $ValueColor$LoadAvg$NormalColor"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%.0f\t%.0f\t%.1f",$4*1024,$2*1024,($2>0)?$3/$2*100:0}')
    local Free=$(echo "$Flash" | cut -f 1)
    local Total=$(echo "$Flash" | cut -f 2)
    local Used=$(echo "$Flash" | cut -f 3)
    print_line "Flash: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{printf"%.0f\t%.0f\t%.1f",Free*1024,Total*1024,(Total>0)?(((Total-Free)/Total)*100):0}' /proc/meminfo)
    local Free=$(echo "$Memory" | cut -f 1)
    local Total=$(echo "$Memory" | cut -f 2)
    local Used=$(echo "$Memory" | cut -f 3)
    print_line "Memory: free: $ValueColor$(human_readable $Free)$NormalColor, total: $ValueColor$(human_readable $Total)$NormalColor, used: $ValueColor$Used$NormalColor%%"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        for Device in $(uci -q get firewall.$Zone.network); do
            local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
            if [ "$Status" != "" ]; then
                local State=""
                local Iface=""
                local Uptime=""
                local IP4=""
                local IP6=""
                local Subnet4=""
                local Subnet6=""
                local Gateway4="n/a"
                local Gateway6="n/a"
                local DNS=""
                local Protocol=""
                json_load "${Status:-{}}"
                json_get_var State up
                json_get_var Uptime uptime
                json_get_var Iface l3_device
                json_get_var Protocol proto
                if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                    json_select ipv4_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP4 address
                        json_get_var Subnet4 mask
                        [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                    fi
                fi
                json_select
                if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                    json_select ipv6_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP6 address
                        json_get_var Subnet6 mask
                        [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                    fi
                fi
                json_select
                if json_get_type Status route && [ "$Status" = array ]; then
                    json_select route
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = object ]; do
                        json_select "$((Index++))"
                        json_get_var Status target
                        case "$Status" in
                            0.0.0.0)
                                json_get_var Gateway4 nexthop;;
                            ::)
                                json_get_var Gateway6 nexthop;;
                        esac
                        json_select ".."
                    done    
                fi
                json_select
                if json_get_type Status dns_server && [ "$Status" = array ]; then
                    json_select dns_server
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = string ]; do
                        json_get_var Status "$((Index++))"
                        DNS="${DNS:+$DNS }$Status"
                    done
                fi
                if [ "$State" == "1" ]; then
                    [ "$IP4" != "" ] && print_line "WAN: $AddrColor$IP4$NormalColor($Iface), gateway: $AddrColor$Gateway4$NormalColor"
                    [ "$IP6" != "" ] && print_line "WAN: $AddrColor$IP6$NormalColor($Iface), gateway: $AddrColor$Gateway6$NormalColor"
                    print_line "proto: $ValueColor$Protocol$NormalColor, uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
                    [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                fi
            fi
        done
    done
}

print_lan() {
    local Device="lan"
    local State
    local Iface
    local IP4
    local IP6
    local Subnet4
    local Subnet6
    local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
    if [ "$Status" != "" ]; then
        json_load "${Status:-{}}"
        json_get_var State up
        json_get_var Iface device
        if json_get_type Status ipv4_address && [ "$Status" = array ]; then
            json_select ipv4_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP4 address
                json_get_var Subnet4 mask
                [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
            fi
        fi
        json_select
        if json_get_type Status ipv6_address && [ "$Status" = array ]; then
            json_select ipv6_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP6 address
                json_get_var Subnet6 mask
                [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
            fi
        fi
        [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor"
        [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor"
    fi
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            local Connection="Down"
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
                fi
            fi
            if [ "$Mode" == "ap" ]; then
                print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor, conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            else
                print_line "WLAN: $ValueColor$SSID$NormalColor($Mode), ch: $ValueColor$Channel$NormalColor"
                print_line "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            fi
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            local Mode=$(uci -q get openvpn.$VPN.mode)
            local Connection="n/a"
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line "VPN: $Mode, conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
        fi
    done
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_uptime
print_loadavg
print_flash
print_memory
print_wan
print_lan
print_wlan
print_vpn
[ "$EndRuler" == "1" ] && print_horizontal_ruler
exit 0
# Done.
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

65

Odp: Skrypt /sbin/sysinfo.sh

Dzięki! Teraz bajka!

66

Odp: Skrypt /sbin/sysinfo.sh

Co myślicie o tym żeby stan Flash i Memory zrobić w kolejności podobnej jak w df?
Teraz w sysinfo.sh jest tak:

 ----------------------------------------------------------------
 | Machine: TP-Link TL-WDR4900 v1, Name: Router-Domowy          |
 | Uptime: 01:51:12, Now: 2013-06-12 20:51:33                   |
 | Load: 0.01, 0.07, 0.11                                       |
 | Flash: free: 2.1MB, total: 4.7MB, used: 56.2%                |
 | Memory: free: 103.4MB, total: 123.7MB, used: 16.4%           |
 | WAN: 123.123.123.123/22(eth0.2), gateway: 123.123.123.123    |
 | proto: dhcp, uptime: 01:50:25, rx/tx: 2.1MB/1.4MB            |
 | dns: 8.8.8.8 8.8.4.4 8.8.6.6 8.8.2.2                         |
 | LAN: 192.168.1.1/24                                          |
 | WLAN: Domowa1(ap), ch: 9, conn: 0, rx/tx: 0B/396.3KB         |
 | WLAN: Domowa2(ap), ch: 44, conn: 0, rx/tx: 0B/396.3KB        |
 | VPN: Domowy(svr):1234, conn: 1, rx/tx: 403.8KB/383.1KB       |
 | VPN: Domowy2(svr):345, conn: 1, rx/tx: 241.3KB/278.8KB       |
 ----------------------------------------------------------------

a df
pokazuje w takiej kolejności:

df -h
Filesystem                Size      Used Available Use% Mounted on
rootfs                    4.7M      2.6M      2.1M  56% /
/dev/root                 2.3M      2.3M         0 100% /rom
tmpfs                    61.8M    604.0K     61.3M   1% /tmp
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/mtdblock3            4.7M      2.6M      2.1M  56% /overlay
overlayfs:/overlay        4.7M      2.6M      2.1M  56% /

gdyby było tak

 ----------------------------------------------------------------
 . . .
 | Flash: total: 4.7MB, used: 56.2%, free: 2.1MB                |
 | Memory: total: 123.7MB, used: 16.4%, free: 103.4MB           |
 . . .
 ----------------------------------------------------------------

co Wy na to?

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

67

Odp: Skrypt /sbin/sysinfo.sh

Czysta  kosmetyka smile

Masz niepotrzebny router, uszkodzony czy nie - chętnie przygarnę go.

68

Odp: Skrypt /sbin/sysinfo.sh

To prawda,  ale jak nikt nic nie chce wiecej;-)
Czas na fontanny i wodotryski...
A tak naprawdę to przyzwyczajenia do kolumn w df robią swoje.

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

69

Odp: Skrypt /sbin/sysinfo.sh

To zrób, niektórzy się przestawią smile

Masz niepotrzebny router, uszkodzony czy nie - chętnie przygarnę go.

70

Odp: Skrypt /sbin/sysinfo.sh

To jest:

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta błędu wyśw. zajetości Flash-a, dodanie kolorów
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie @mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkreśleń
#    1.07    RD    Modyfikacja związana z poprawnym wyświetlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wyświetlania DNS-ów dla wan, dodanie uptime dla interfejsów
#    1.09    RD    Dodanie statusu "Down" dla wyłączonego wifi, zmiana wyświetlania dla WLAN(sta)
#    1.10    RD    Korekta wyświetlania dla WLAN(sta)
#    1.11    RD    Korekta wyświetlania stanu pamięci, sugestie @dopsz 
#    1.12    RD    Zmiana kolejności wyświetlania wartości stanu pamięci + kosmetyka 
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local Rouler
local NormalColor
local MachineColor
local ValueColor
local AddrColor
local RXTXColor

initialize() { # <Script Parameters>
    local ColorMode="1"
    for Parameter in $@; do
        case  $Parameter  in
        -m) ColorMode="0";;
        -sr) StartRuler="0";;
        -er) EndRuler="0";;
        -w1) Width=80;;
        -w2) Width=100;;
        -w3) Width=120;;
        -h|*)    
            echo "Usage: $0 - [parameter]"
            echo "    -h    : This help."
            echo "    -m    : Display mono version."
            echo "    -sr    : Without start horizontal ruler."    
            echo "    -er    : Without end horizontal ruler."    
            exit 1;;
        esac
    done
    if [ "$ColorMode" == "1" ]; then
        NormalColor="\e[0m"
        MachineColor="\e[0;33m"
        ValueColor="\e[1;36m"
        AddrColor="\e[1;31m"
        RXTXColor="\e[2;32m"
    else
        NormalColor="\e[0m"
        MachineColor="\e[7m"
        ValueColor="\e[1m"
        AddrColor="\e[4m"
        RXTXColor="\e[1m"
    fi
    local i
    for i in $(seq $(expr $Width + 4 )); do 
        Rouler="$Rouler-";
    done
}

human_readable() { # <Number of bytes>
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
    else
        printf "0B"
    fi
}

device_rx_tx() { # <Device>
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%.0f\t%.0f",$2,$10}' /proc/net/dev)
    [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() { # <Time in Seconds>
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() { # <String to Print>, [[<String to Print>] ...]
    local Line="$@"
    printf " | %-${Width}s |\r | $Line\n"
}

print_horizontal_ruler() {
    printf " $Rouler\n"
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model)
    print_line     "Machine: $MachineColor${Machine:-n/a}$NormalColor,"\
                "Name: $MachineColor${HostName:-n/a}$NormalColor"
}

print_times() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line     "System uptime: $ValueColor$Uptime$NormalColor,"\
                "Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"'$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor'",$1,$2,$3}' /proc/loadavg)
    print_line "System load: $LoadAvg"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%.0f\t%.0f\t%.1f\t%.0f",$2*1024,$3*1024,($2>0)?$3/$2*100:0,$4*1024}')
    local Total=$(echo "$Flash" | cut -f 1)
    local Used=$(echo "$Flash" | cut -f 2)
    local UsedPercent=$(echo "$Flash" | cut -f 3)
    local Free=$(echo "$Flash" | cut -f 4)
    print_line     "Flash:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{Used=Total-Free;printf"%.0f\t%.0f\t%.1f\t%.0f",Total*1024,Used*1024,(Total>0)?((Used/Total)*100):0,Free*1024}' /proc/meminfo)
    local Total=$(echo "$Memory" | cut -f 1)
    local Used=$(echo "$Memory" | cut -f 2)
    local UsedPercent=$(echo "$Memory" | cut -f 3)
    local Free=$(echo "$Memory" | cut -f 4)
    print_line "Memory:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        for Device in $(uci -q get firewall.$Zone.network); do
            local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
            if [ "$Status" != "" ]; then
                local State=""
                local Iface=""
                local Uptime=""
                local IP4=""
                local IP6=""
                local Subnet4=""
                local Subnet6=""
                local Gateway4=""
                local Gateway6=""
                local DNS=""
                local Protocol=""
                json_load "${Status:-{}}"
                json_get_var State up
                json_get_var Uptime uptime
                json_get_var Iface l3_device
                json_get_var Protocol proto
                if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                    json_select ipv4_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP4 address
                        json_get_var Subnet4 mask
                        [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                    fi
                fi
                json_select
                if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                    json_select ipv6_address
                    json_get_type Status 1
                    if [ "$Status" = object ]; then
                        json_select 1
                        json_get_var IP6 address
                        json_get_var Subnet6 mask
                        [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                    fi
                fi
                json_select
                if json_get_type Status route && [ "$Status" = array ]; then
                    json_select route
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = object ]; do
                        json_select "$((Index++))"
                        json_get_var Status target
                        case "$Status" in
                            0.0.0.0)
                                json_get_var Gateway4 nexthop;;
                            ::)
                                json_get_var Gateway6 nexthop;;
                        esac
                        json_select ".."
                    done    
                fi
                json_select
                if json_get_type Status dns_server && [ "$Status" = array ]; then
                    json_select dns_server
                    local Index="1"
                    while json_get_type Status $Index && [ "$Status" = string ]; do
                        json_get_var Status "$((Index++))"
                        DNS="${DNS:+$DNS }$Status"
                    done
                fi
                if [ "$State" == "1" ]; then
                    [ "$IP4" != "" ] && print_line     "WAN: $AddrColor$IP4$NormalColor($Iface),"\
                                                    "gateway: $AddrColor${Gateway4:-n/a}$NormalColor"
                    [ "$IP6" != "" ] && print_line    "WAN: $AddrColor$IP6$NormalColor($Iface),"\
                                                    "gateway: $AddrColor${Gateway6:-n/a}$NormalColor"
                    print_line    "proto: $ValueColor${Protocol:-n/a}$NormalColor,"\
                                "uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
                    [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                fi
            fi
        done
    done
}

print_lan() {
    local Device="lan"
    local State
    local Iface
    local IP4
    local IP6
    local Subnet4
    local Subnet6
    local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
    if [ "$Status" != "" ]; then
        json_load "${Status:-{}}"
        json_get_var State up
        json_get_var Iface device
        if json_get_type Status ipv4_address && [ "$Status" = array ]; then
            json_select ipv4_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP4 address
                json_get_var Subnet4 mask
                [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
            fi
        fi
        json_select
        if json_get_type Status ipv6_address && [ "$Status" = array ]; then
            json_select ipv6_address
            json_get_type Status 1
            if [ "$Status" = object ]; then
                json_select 1
                json_get_var IP6 address
                json_get_var Subnet6 mask
                [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
            fi
        fi
        [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor"
        [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor"
    fi
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            local Connection="Down"
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
                fi
            fi
            if [ "$Mode" == "ap" ]; then
                print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
                            "ch: $ValueColor${Channel:-n/a}$NormalColor,"\
                            "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            else
                print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
                            "ch: $ValueColor${Channel:-n/a}$NormalColor"
                print_line    "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            fi
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            local Mode=$(uci -q get openvpn.$VPN.mode)
            local Connection="n/a"
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line    "VPN: $Mode,"\
                        "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
        fi
    done
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_times
print_loadavg
print_flash
print_memory
print_wan
print_lan
print_wlan
print_vpn
[ "$EndRuler" == "1" ] && print_horizontal_ruler
exit 0
# Done.
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

71 (edytowany przez bhb 2013-06-12 21:08:29)

Odp: Skrypt /sbin/sysinfo.sh

No to się przestawiłem bardzo ładne.
Dzięki.

72

Odp: Skrypt /sbin/sysinfo.sh

Witam,
zmieniłem sposób wyświetlania informacji o LAN, mam przypadki kilku LAN-ów i brakowało mi takiej informacji. Dodałem także informację o DHCP oraz infromację o ostatnich pięcu błędach z logu, jeżeli wystąpiły.
Dla tych, którzy chcieliby wyświetlać coś jeszcze dodałem extra linię.

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta błędu wyśw. zajetości Flash-a, dodanie kolorów
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie @mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkreśleń
#    1.07    RD    Modyfikacja związana z poprawnym wyświetlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wyświetlania DNS-ów dla wan, dodanie uptime dla interfejsów
#    1.09    RD    Dodanie statusu "Down" dla wyłączonego wifi, zmiana wyświetlania dla WLAN(sta)
#    1.10    RD    Korekta wyświetlania dla WLAN(sta)
#    1.11    RD    Korekta wyświetlania stanu pamięci, sugestie @dopsz 
#    1.12    RD    Zmiana kolejności wyświetlania wartości stanu pamięci + kosmetyka 
#    1.13    RD    Dodanie info o dhcp w LAN, zmiana sposobu wyświetlania informacji o LAN
#    1.14    RD    Dodanie informacji o ostatnich 5 błędach
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local Rouler
local LastErrors="1"
local NormalColor=""
local MachineColor=""
local ValueColor=""
local AddrColor=""
local RXTXColor=""
local ErrorColor=""
local ExtraName=""
local ExtraValue=""

initialize() { # <Script Parameters>
    local ColorMode="c"
    while [ -n "$1" ]; do
        case "$1" in
        -h|--help)    echo -e    "Usage: $0 [-h|--help] [[-m|--mono]|[-bw|-black-white]|[-c2|--color-2]] [-sr|--no-start-ruler] [-er|--no-end-ruler]"\
                            "[-w N|--width N] [-en Name|--extra-name Name] [-ev Value|--extra-value Value] [-le|--no-last-err]"\
                            "\n\t-h\t\tThis help,"\
                            "\n\t-m\t\tDisplay mono version,"\
                            "\n\t-bw\t\tDisplay black-white version,"\
                            "\n\t-c2\t\tDisplay alternative color version 2,"\
                            "\n\t-sr\t\tWithout start horizontal ruler,"\
                            "\n\t-er\t\tWithout end horizontal ruler,"\
                            "\n\t-w N\t\tSet width of text area to N characters (minimum 60)"\
                            "\n\t-en Name\tPrint extra name"\
                            "\n\t-ev Value\tPrint extra value"\
                            "\n\t-le\t\tDon't display last errors"
                    exit 1;;
        -m|--mono) ColorMode="m";;
        -bw|--black-white) ColorMode="bw";;
        -c2|--color-2) ColorMode="c2";;
        -sr|--no-start-ruler) StartRuler="0";;
        -er|--no-end-ruler) EndRuler="0";;
        -w|--width) shift; Width=$1;;
        -en|--extra-name)    while [ -n "$2" ] && [ "${2:0:1}" != "-" ]; do
                                shift
                                [ "$ExtraName" != "" ] && ExtraName="$ExtraName "
                                ExtraName="$ExtraName$1"
                            done;;
        -ev|--extra-value)    while [ -n "$2" ] && [ "${2:0:1}" != "-" ]; do
                                shift
                                [ "$ExtraValue" != "" ] && ExtraValue="$ExtraValue "
                                ExtraValue="$ExtraValue$1"
                            done;;
        -le|--no-last-err)    LastErrors="0";;
        *) echo "Invalid option: $1. Use -h for help";;
        esac
        shift;
    done
    case "$ColorMode" in
        c)    NormalColor="\e[0m"
            MachineColor="\e[0;33m"
            ValueColor="\e[1;36m"
            AddrColor="\e[1;31m"
            RXTXColor="\e[2;32m"
            ErrorColor="\e[0;31m";;
        c2)    NormalColor="\e[0m"
            MachineColor="\e[0;31m"
            ValueColor="\e[0;33m"
            AddrColor="\e[0;35m"
            RXTXColor="\e[0;36m"
            ErrorColor="\e[0;31m";;
        m)    NormalColor="\e[0m"
            MachineColor="\e[7m"
            ValueColor="\e[1m"
            AddrColor="\e[4m"
            RXTXColor="\e[1m"
            ErrorColor="\e[4";;
        *)    ;;
    esac
    ([ "$Width" == "" ] || [ "$Width" -lt 60 ]) && Width=60
    local i
    for i in $(seq $(expr $Width + 4 )); do 
        Rouler="$Rouler-";
    done
}

human_readable() { # <Number of bytes>
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
    else
        printf "0B"
    fi
}

device_rx_tx() { # <Device>
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%.0f\t%.0f",$2,$10}' /proc/net/dev)
    [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() { # <Time in Seconds>
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() { # <String to Print>, [[<String to Print>] ...]
    local Line="$@"
    printf " | %-${Width}s |\r | $Line\n"
}

print_horizontal_ruler() {
    printf " $Rouler\n"
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model)
    print_line     "Machine: $MachineColor${Machine:-n/a}$NormalColor,"\
                "Name: $MachineColor${HostName:-n/a}$NormalColor"
}

print_times() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line     "System uptime: $ValueColor$Uptime$NormalColor,"\
                "Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"'$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor'",$1,$2,$3}' /proc/loadavg)
    print_line "System load: $LoadAvg"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%.0f\t%.0f\t%.1f\t%.0f",$2*1024,$3*1024,($2>0)?$3/$2*100:0,$4*1024}')
    local Total=$(echo "$Flash" | cut -f 1)
    local Used=$(echo "$Flash" | cut -f 2)
    local UsedPercent=$(echo "$Flash" | cut -f 3)
    local Free=$(echo "$Flash" | cut -f 4)
    print_line     "Flash:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{Used=Total-Free;printf"%.0f\t%.0f\t%.1f\t%.0f",Total*1024,Used*1024,(Total>0)?((Used/Total)*100):0,Free*1024}' /proc/meminfo)
    local Total=$(echo "$Memory" | cut -f 1)
    local Used=$(echo "$Memory" | cut -f 2)
    local UsedPercent=$(echo "$Memory" | cut -f 3)
    local Free=$(echo "$Memory" | cut -f 4)
    print_line "Memory:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        if [ "$(uci -q get firewall.$Zone.masq)" == "1" ]; then
            for Device in $(uci -q get firewall.$Zone.network); do
                local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
                if [ "$Status" != "" ]; then
                    local State=""
                    local Iface=""
                    local Uptime=""
                    local IP4=""
                    local IP6=""
                    local Subnet4=""
                    local Subnet6=""
                    local Gateway4=""
                    local Gateway6=""
                    local DNS=""
                    local Protocol=""
                    json_load "${Status:-{}}"
                    json_get_var State up
                    json_get_var Uptime uptime
                    json_get_var Iface l3_device
                    json_get_var Protocol proto
                    if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                        json_select ipv4_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP4 address
                            json_get_var Subnet4 mask
                            [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                        fi
                    fi
                    json_select
                    if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                        json_select ipv6_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP6 address
                            json_get_var Subnet6 mask
                            [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                        fi
                    fi
                    json_select
                    if json_get_type Status route && [ "$Status" = array ]; then
                        json_select route
                        local Index="1"
                        while json_get_type Status $Index && [ "$Status" = object ]; do
                            json_select "$((Index++))"
                            json_get_var Status target
                            case "$Status" in
                                0.0.0.0)
                                    json_get_var Gateway4 nexthop;;
                                ::)
                                    json_get_var Gateway6 nexthop;;
                            esac
                            json_select ".."
                        done    
                    fi
                    json_select
                    if json_get_type Status dns_server && [ "$Status" = array ]; then
                        json_select dns_server
                        local Index="1"
                        while json_get_type Status $Index && [ "$Status" = string ]; do
                            json_get_var Status "$((Index++))"
                            DNS="${DNS:+$DNS }$Status"
                        done
                    fi
                    if [ "$State" == "1" ]; then
                        [ "$IP4" != "" ] && print_line     "WAN: $AddrColor$IP4$NormalColor($Iface),"\
                                                        "gateway: $AddrColor${Gateway4:-n/a}$NormalColor"
                        [ "$IP6" != "" ] && print_line    "WAN: $AddrColor$IP6$NormalColor($Iface),"\
                                                        "gateway: $AddrColor${Gateway6:-n/a}$NormalColor"
                        print_line    "proto: $ValueColor${Protocol:-n/a}$NormalColor,"\
                                    "uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
                        [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                    fi
                fi
            done
        fi 
    done
}

print_lan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep []]=zone | cut -f2 -d. | cut -f1 -d=); do
        if [ "$(uci -q get firewall.$Zone.masq)" != "1" ]; then
            for Device in $(uci -q get firewall.$Zone.network); do
                local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
                if [ "$Status" != "" ]; then
                    local State=""
                    local Iface=""
                    local IP4=""
                    local IP6=""
                    local Subnet4=""
                    local Subnet6=""
                    json_load "${Status:-{}}"
                    json_get_var State up
                    json_get_var Iface device
                    if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                        json_select ipv4_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP4 address
                            json_get_var Subnet4 mask
                            [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                        fi
                    fi
                    json_select
                    if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                        json_select ipv6_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP6 address
                            json_get_var Subnet6 mask
                            [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                        fi
                    fi
                    local DHCPConfig=$(uci -q show dhcp | grep .interface=$Device | cut -d. -f2)
                    if [ "$DHCPConfig" != "" ] && [ "$(uci -q get dhcp.$DHCPConfig.ignore)" != "1" ]; then
                        local DHCPStart=$(uci -q get dhcp.$DHCPConfig.start)
                        local DHCPLimit=$(uci -q get dhcp.$DHCPConfig.limit)
                        [ "$DHCPStart" != "" ] && [ "$DHCPLimit" != "" ] && DHCP="$(echo $IP4 | cut -d. -f1-3).$DHCPStart-$(expr $DHCPStart + $DHCPLimit - 1)"
                    fi
                    [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor($Iface), dhcp: $AddrColor${DHCP:-n/a}$NormalColor"
                    [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor($Iface)"
                fi
            done
        fi 
    done
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            local Connection="Down"
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
                fi
            fi
            if [ "$Mode" == "ap" ]; then
                print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
                            "ch: $ValueColor${Channel:-n/a}$NormalColor,"\
                            "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            else
                print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
                            "ch: $ValueColor${Channel:-n/a}$NormalColor"
                print_line    "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            fi
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            local Mode=$(uci -q get openvpn.$VPN.mode)
            local Connection="n/a"
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line    "VPN: $Mode,"\
                        "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
        fi
    done
}

print_extra() {
    ([ "$ExtraName" != "" ] || [ "$ExtraValue" != "" ]) && print_line "$ExtraName $ValueColor$ExtraValue$NormalColor"
}

print_error() {
    logread | awk '/\w{3}+\.(err|warn|alert|emerg|crit)/{err[++i]=$0}END{j=i-4;j=j>=1?j:1;while(j<=i)print" '$ErrorColor'"err[j++]"'$NormalColor'"}'
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_times
print_loadavg
print_flash
print_memory
print_wan
print_lan
print_wlan
print_vpn
print_extra
[ "$EndRuler" == "1" ] && print_horizontal_ruler
[ "$LastErrors" == "1" ] && print_error
exit 0
# Done.
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

73 (edytowany przez Rafciq 2013-06-23 13:32:09)

Odp: Skrypt /sbin/sysinfo.sh

Witam,
dodałem informacje o swap.

Aby informacja wyświetlała się automatycznie po zalogowaniu należy do pliku /etc/profile dodać na końcu linie (o ile jej już tam nie ma) :

[ -x /sbin/sysinfo.sh ] && /sbin/sysinfo.sh -sr

Skrypt /sbin/sysinfo.sh

#!/bin/sh
#
# sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
# 
#    1.00    CJ    Pierwsza wersja kodu
#    1.01    RD    Drobna przebudowa
#    1.02    RD    Korekta błędu wyśw. zajetości Flash-a, dodanie kolorów
#    1.03    RD    Dodanie nazwy routera, zmiana formatowania
#    1.04    RD    Kosmetyka, sugestie @mikhnal. Zmiana przetwarzania info. o wan.
#    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
#    1.06    RD    Parametryzacja kolorów i pojawiania się podkreśleń
#    1.07    RD    Modyfikacja związana z poprawnym wyświetlaniem interfejsu dla prot.3g
#    1.08    RD    Modyfikacja wyświetlania DNS-ów dla wan, dodanie uptime dla interfejsów
#    1.09    RD    Dodanie statusu "Down" dla wyłączonego wifi, zmiana wyświetlania dla WLAN(sta)
#    1.10    RD    Korekta wyświetlania dla WLAN(sta)
#    1.11    RD    Korekta wyświetlania stanu pamięci, sugestie @dopsz 
#    1.12    RD    Zmiana kolejności wyświetlania wartości stanu pamięci + kosmetyka 
#    1.13    RD    Dodanie info o dhcp w LAN, zmiana sposobu wyświetlania informacji o LAN
#    1.14    RD    Dodanie informacji o ostatnich 5 błędach
#    1.15    RD    Zmiana stderr
#    1.16    RD    Dodanie wyświetlania informacji o swap
#
# Destination /sbin/sysinfo.sh
#
. /usr/share/libubox/jshn.sh

local Width=60
local StartRuler="1"
local EndRuler="1"
local LastErrors="1"
local NormalColor=""
local MachineColor=""
local ValueColor=""
local AddrColor=""
local RXTXColor=""
local ErrorColor=""
local ExtraName=""
local ExtraValue=""

initialize() { # <Script Parameters>
    local ColorMode="c"
    while [ -n "$1" ]; do
        case "$1" in
        -h|--help)    echo -e    "Usage: $0 [-h|--help] [[-m|--mono]|[-bw|-black-white]|[-c2|--color-2]] [-sr|--no-start-ruler] [-er|--no-end-ruler]"\
                            "[-w N|--width N] [-en Name|--extra-name Name] [-ev Value|--extra-value Value] [-le|--no-last-err]"\
                            "\n\t-h\t\tThis help,"\
                            "\n\t-m\t\tDisplay mono version,"\
                            "\n\t-bw\t\tDisplay black-white version,"\
                            "\n\t-c2\t\tDisplay alternative color version 2,"\
                            "\n\t-sr\t\tWithout start horizontal ruler,"\
                            "\n\t-er\t\tWithout end horizontal ruler,"\
                            "\n\t-w N\t\tSet width of text area to N characters (minimum 60)"\
                            "\n\t-en Name\tPrint extra name"\
                            "\n\t-ev Value\tPrint extra value"\
                            "\n\t-le\t\tDon't display last errors"
                    exit 1;;
        -m|--mono) ColorMode="m";;
        -bw|--black-white) ColorMode="bw";;
        -c2|--color-2) ColorMode="c2";;
        -sr|--no-start-ruler) StartRuler="0";;
        -er|--no-end-ruler) EndRuler="0";;
        -w|--width) shift; Width=$1;;
        -en|--extra-name)    while [ -n "$2" ] && [ "${2:0:1}" != "-" ]; do
                                shift
                                [ "$ExtraName" != "" ] && ExtraName="$ExtraName "
                                ExtraName="$ExtraName$1"
                            done;;
        -ev|--extra-value)    while [ -n "$2" ] && [ "${2:0:1}" != "-" ]; do
                                shift
                                [ "$ExtraValue" != "" ] && ExtraValue="$ExtraValue "
                                ExtraValue="$ExtraValue$1"
                            done;;
        -le|--no-last-err)    LastErrors="0";;
        *) echo "Invalid option: $1. Use -h for help";;
        esac
        shift;
    done
    case "$ColorMode" in
        c)    NormalColor="\e[0m"
            MachineColor="\e[0;33m"
            ValueColor="\e[1;36m"
            AddrColor="\e[1;31m"
            RXTXColor="\e[2;32m"
            ErrorColor="\e[0;31m";;
        c2)    NormalColor="\e[0m"
            MachineColor="\e[0;31m"
            ValueColor="\e[0;33m"
            AddrColor="\e[0;35m"
            RXTXColor="\e[0;36m"
            ErrorColor="\e[0;31m";;
        m)    NormalColor="\e[0m"
            MachineColor="\e[7m"
            ValueColor="\e[1m"
            AddrColor="\e[4m"
            RXTXColor="\e[1m"
            ErrorColor="\e[4";;
        *)    ;;
    esac
    ([ "$Width" == "" ] || [ "$Width" -lt 60 ]) && Width=60
}

human_readable() { # <Number of bytes>
    if [ $1 -gt 0 ]; then
        printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}' 2>/dev/null)"
    else
        printf "0B"
    fi
}

device_rx_tx() { # <Device>
    local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%.0f\t%.0f",$2,$10}' /proc/net/dev 2>/dev/null)
    [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
}

uptime_str() { # <Time in Seconds>
    local Uptime=$1
    if [ $Uptime -gt 0 ]; then
        local Days=$(expr $Uptime / 60 / 60 / 24)
        local Hours=$(expr $Uptime / 60 / 60 % 24)
        local Minutes=$(expr $Uptime / 60 % 60)
        local Seconds=$(expr $Uptime % 60)
        if [ $Days -gt 0 ]; then
            Days=$(printf "%dd " $Days)
        else
            Days=""
        fi
        printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
    fi
}

print_line() { # <String to Print>, [[<String to Print>] ...]
    local Line="$@"
    printf " | %-${Width}s |\r | $Line\n" 2>/dev/null
}

print_horizontal_ruler() {
    printf "/%$(expr $Width + 4 )s\n" | tr ' /' '- ' 2>/dev/null
}

print_machine() {
    local Machine=""
    local HostName=$(uci -q get system.@system[0].hostname)
    [ -e /tmp/sysinfo/model ] && Machine=$(cat /tmp/sysinfo/model 2>/dev/null)
    print_line     "Machine: $MachineColor${Machine:-n/a}$NormalColor,"\
                "Name: $MachineColor${HostName:-n/a}$NormalColor"
}

print_times() {
    local SysUptime=$(cut -d. -f1 /proc/uptime)
    local Uptime=$(uptime_str $SysUptime)
    local Now=$(date +'%Y-%m-%d %H:%M:%S')
    print_line     "System uptime: $ValueColor$Uptime$NormalColor,"\
                "Now: $ValueColor$Now$NormalColor"
}

print_loadavg() {
    local LoadAvg=$(awk '{printf"'$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor'",$1,$2,$3}' /proc/loadavg 2>/dev/null)
    print_line "System load: $LoadAvg"
}

print_flash() {
    local Flash=$(df -k /overlay | awk '/overlay/{printf "%.0f\t%.0f\t%.1f\t%.0f",$2*1024,$3*1024,($2>0)?$3/$2*100:0,$4*1024}' 2>/dev/null)
    local Total=$(echo "$Flash" | cut -f 1)
    local Used=$(echo "$Flash" | cut -f 2)
    local UsedPercent=$(echo "$Flash" | cut -f 3)
    local Free=$(echo "$Flash" | cut -f 4)
    print_line     "Flash:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_memory() {
    local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{Used=Total-Free;printf"%.0f\t%.0f\t%.1f\t%.0f",Total*1024,Used*1024,(Total>0)?((Used/Total)*100):0,Free*1024}' /proc/meminfo 2>/dev/null)
    local Total=$(echo "$Memory" | cut -f 1)
    local Used=$(echo "$Memory" | cut -f 2)
    local UsedPercent=$(echo "$Memory" | cut -f 3)
    local Free=$(echo "$Memory" | cut -f 4)
    print_line "Memory:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_swap() {
    local Swap=$(awk 'BEGIN{Total=0;Free=0}$1~/^SwapTotal:/{Total=$2}$1~/^SwapFree:/{Free=$2}END{Used=Total-Free;printf"%.0f\t%.0f\t%.1f\t%.0f",Total*1024,Used*1024,(Total>0)?((Used/Total)*100):0,Free*1024}' /proc/meminfo 2>/dev/null)
    local Total=$(echo "$Swap" | cut -f 1)
    local Used=$(echo "$Swap" | cut -f 2)
    local UsedPercent=$(echo "$Swap" | cut -f 3)
    local Free=$(echo "$Swap" | cut -f 4)
    [ "$Total" -gt 0 ] && print_line "Swap:"\
                "total: $ValueColor$(human_readable $Total)$NormalColor,"\
                "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
                "free: $ValueColor$(human_readable $Free)$NormalColor"
}

print_wan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
        if [ "$(uci -q get firewall.$Zone.masq)" == "1" ]; then
            for Device in $(uci -q get firewall.$Zone.network); do
                local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
                if [ "$Status" != "" ]; then
                    local State=""
                    local Iface=""
                    local Uptime=""
                    local IP4=""
                    local IP6=""
                    local Subnet4=""
                    local Subnet6=""
                    local Gateway4=""
                    local Gateway6=""
                    local DNS=""
                    local Protocol=""
                    json_load "${Status:-{}}"
                    json_get_var State up
                    json_get_var Uptime uptime
                    json_get_var Iface l3_device
                    json_get_var Protocol proto
                    if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                        json_select ipv4_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP4 address
                            json_get_var Subnet4 mask
                            [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                        fi
                    fi
                    json_select
                    if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                        json_select ipv6_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP6 address
                            json_get_var Subnet6 mask
                            [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                        fi
                    fi
                    json_select
                    if json_get_type Status route && [ "$Status" = array ]; then
                        json_select route
                        local Index="1"
                        while json_get_type Status $Index && [ "$Status" = object ]; do
                            json_select "$((Index++))"
                            json_get_var Status target
                            case "$Status" in
                                0.0.0.0)
                                    json_get_var Gateway4 nexthop;;
                                ::)
                                    json_get_var Gateway6 nexthop;;
                            esac
                            json_select ".."
                        done    
                    fi
                    json_select
                    if json_get_type Status dns_server && [ "$Status" = array ]; then
                        json_select dns_server
                        local Index="1"
                        while json_get_type Status $Index && [ "$Status" = string ]; do
                            json_get_var Status "$((Index++))"
                            DNS="${DNS:+$DNS }$Status"
                        done
                    fi
                    if [ "$State" == "1" ]; then
                        [ "$IP4" != "" ] && print_line     "WAN: $AddrColor$IP4$NormalColor($Iface),"\
                                                        "gateway: $AddrColor${Gateway4:-n/a}$NormalColor"
                        [ "$IP6" != "" ] && print_line    "WAN: $AddrColor$IP6$NormalColor($Iface),"\
                                                        "gateway: $AddrColor${Gateway6:-n/a}$NormalColor"
                        print_line    "proto: $ValueColor${Protocol:-n/a}$NormalColor,"\
                                    "uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
                        [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
                    fi
                fi
            done
        fi 
    done
}

print_lan() {
    local Zone
    local Device
    for Zone in $(uci -q show firewall | grep []]=zone | cut -f2 -d. | cut -f1 -d=); do
        if [ "$(uci -q get firewall.$Zone.masq)" != "1" ]; then
            for Device in $(uci -q get firewall.$Zone.network); do
                local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
                if [ "$Status" != "" ]; then
                    local State=""
                    local Iface=""
                    local IP4=""
                    local IP6=""
                    local Subnet4=""
                    local Subnet6=""
                    json_load "${Status:-{}}"
                    json_get_var State up
                    json_get_var Iface device
                    if json_get_type Status ipv4_address && [ "$Status" = array ]; then
                        json_select ipv4_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP4 address
                            json_get_var Subnet4 mask
                            [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
                        fi
                    fi
                    json_select
                    if json_get_type Status ipv6_address && [ "$Status" = array ]; then
                        json_select ipv6_address
                        json_get_type Status 1
                        if [ "$Status" = object ]; then
                            json_select 1
                            json_get_var IP6 address
                            json_get_var Subnet6 mask
                            [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
                        fi
                    fi
                    local DHCPConfig=$(uci -q show dhcp | grep .interface=$Device | cut -d. -f2)
                    if [ "$DHCPConfig" != "" ] && [ "$(uci -q get dhcp.$DHCPConfig.ignore)" != "1" ]; then
                        local DHCPStart=$(uci -q get dhcp.$DHCPConfig.start)
                        local DHCPLimit=$(uci -q get dhcp.$DHCPConfig.limit)
                        [ "$DHCPStart" != "" ] && [ "$DHCPLimit" != "" ] && DHCP="$(echo $IP4 | cut -d. -f1-3).$DHCPStart-$(expr $DHCPStart + $DHCPLimit - 1)"
                    fi
                    [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor($Iface), dhcp: $AddrColor${DHCP:-n/a}$NormalColor"
                    [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor($Iface)"
                fi
            done
        fi 
    done
}

print_wlan() {
    local Iface
    for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
        local Device=$(uci -q get wireless.$Iface.device)
        local SSID=$(uci -q get wireless.$Iface.ssid)
        local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
        local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
        if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
            local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
            local Channel=$(uci -q get wireless.$Device.channel)
            local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
            local Connection="Down"
            if [ -n "$RadioIface" ]; then
                if [ "$Mode" == "ap" ]; then
                    Connection="$(iw dev $RadioIface station dump | grep Station | wc -l 2>/dev/null)"
                else
                    Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}' 2>/dev/null)"
                fi
            fi
            if [ "$Mode" == "ap" ]; then
                print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
                            "ch: $ValueColor${Channel:-n/a}$NormalColor,"\
                            "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            else
                print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
                            "ch: $ValueColor${Channel:-n/a}$NormalColor"
                print_line    "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
            fi
        fi
    done
}

print_vpn() {
    local VPN
    for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
        local Device=$(uci -q get openvpn.$VPN.dev)
        local Enabled=$(uci -q get openvpn.$VPN.enabled)
        if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
            local Mode=$(uci -q get openvpn.$VPN.mode)
            local Connection="n/a"
            if [ "$Mode" == "server" ]; then
                Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
                Status=$(uci -q get openvpn.$VPN.status)
                Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status 2>/dev/null)
            else
                Mode="$ValueColor$VPN$NormalColor(cli)"
                Connection="Down"
                ifconfig $Device &>/dev/null && Connection="Up"
            fi
            print_line    "VPN: $Mode,"\
                        "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
        fi
    done
}

print_extra() {
    ([ "$ExtraName" != "" ] || [ "$ExtraValue" != "" ]) && print_line "$ExtraName $ValueColor$ExtraValue$NormalColor"
}

print_error() {
    logread | awk '/\w{3}+\.(err|warn|alert|emerg|crit)/{err[++i]=$0}END{j=i-4;j=j>=1?j:1;while(j<=i)print" '$ErrorColor'"err[j++]"'$NormalColor'"}' 2>/dev/null
}

initialize $@
[ "$StartRuler" == "1" ] && print_horizontal_ruler
print_machine
print_times
print_loadavg
print_flash
print_memory
print_swap
print_wan
print_lan
print_wlan
print_vpn
print_extra
[ "$EndRuler" == "1" ] && print_horizontal_ruler
[ "$LastErrors" == "1" ] && print_error
exit 0
# Done.
install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub

74 (edytowany przez bhb 2013-06-20 21:41:09)

Odp: Skrypt /sbin/sysinfo.sh

Wersja 1.16 na moim systemie źle działa swap mam na /dev/sda1
df -h

 Filesystem                Size      Used Available Use% Mounted on
rootfs                  997.4M     47.7M    899.7M   5% /
/dev/root                 2.5M      2.5M         0 100% /rom
tmpfs                    14.2M    116.0K     14.1M   1% /tmp
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/sda2               997.4M     47.7M    899.7M   5% /overlay
overlayfs:/overlay      997.4M     47.7M    899.7M   5% /
/dev/sda3                 2.7G    228.3M      2.3G   9% /mnt/sda3

skrypt wyświetla

 ----------------------------------------------------------------
 |                                                              |
 | Gargoyle PL 1.5.10.3 (ec6e041)                               |
 | OpenWrt Attitude Adjustment 12.09 (r36936)                   |
 | Build time: 2013-06-15 13:16 CEST                            |
 |                                                              |
 | Cezary Jackiewicz (obsy), http://eko.one.pl                  |
 |                                                              |
 ----------------------------------------------------------------
 ----------------------------------------------------------------
 | Machine: TP-Link TL-MR3420 v1, Name: domsat                  |
 | System uptime: 00:01:45, Now: 2013-06-20 21:32:56            |
 | System load: 2.61, 0.79, 0.28                                |
 | Flash: total: 997.4MB, used: 47.7MB, 4.8%, free: 899.7MB     |
 | Memory: total: 28.5MB, used: 14.7MB, 51.7%, free: 13.8MB     |
 | Swap: total: 64.0MB, used: 0B, 0.0%, free: 64.0MB            |
 | WAN: 77.91.xx.xx/22(eth1), gateway: 77.91.xx.xxx             |
 | proto: dhcp, uptime: 00:00:51, rx/tx: 16.4KB/6.9KB           |
 | dns: 208.67.222.222 208.67.220.220                           |
 | LAN: 192.168.10.1/24(br-lan), dhcp: 192.168.10.100-249       |
 | WLAN: wlan-13(ap), ch: 2, conn: 1, rx/tx: 17.0KB/22.0KB      |
 ----------------------------------------------------------------
 Jun 20 21:32:47 domsat kern.warn kernel: [   95.820000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:47 domsat kern.warn kernel: [   95.820000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:51 domsat kern.warn kernel: [  100.460000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:51 domsat kern.warn kernel: [  100.460000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:55 domsat kern.err kernel: [  104.080000] EXT4-fs (sda3): couldn't mount as ext3 due to feature incompatibilities
 | Machine: TP-Link TL-MR3420 v1, Name: domsat                  |
 | System uptime: 00:01:46, Now: 2013-06-20 21:32:58            |
 | System load: 2.57, 0.81, 0.28                                |
 | Flash: total: 997.4MB, used: 47.7MB, 4.8%, free: 899.7MB     |
 | Memory: total: 28.5MB, used: 14.7MB, 51.7%, free: 13.8MB     |
 | Swap: total: 64.0MB, used: 0B, 0.0%, free: 64.0MB            |
 | WAN: 77.91.xx.xx/22(eth1), gateway: 77.91.xx.xxx             |
 | proto: dhcp, uptime: 00:00:53, rx/tx: 17.2KB/6.9KB           |
 | dns: 208.67.222.222 208.67.220.220                           |
 | LAN: 192.168.10.1/24(br-lan), dhcp: 192.168.10.100-249       |
 | WLAN: wlan-13(ap), ch: 2, conn: 1, rx/tx: 17.8KB/25.9KB      |
 ----------------------------------------------------------------
 Jun 20 21:32:47 domsat kern.warn kernel: [   95.820000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:47 domsat kern.warn kernel: [   95.820000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:51 domsat kern.warn kernel: [  100.460000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:51 domsat kern.warn kernel: [  100.460000] head: sending ioctl 540d to a partition!
 Jun 20 21:32:55 domsat kern.err kernel: [  104.080000] EXT4-fs (sda3): couldn't mount as ext3 due to feature incompatibilities

75 (edytowany przez Rafciq 2013-06-21 05:41:38)

Odp: Skrypt /sbin/sysinfo.sh

bhb co masz na myśli pisząc ze źle działa.
Wydaj proszę polecenie  free i pokaż.

install.sh - Aktualizacja systemu, sysinfo.sh - Info.o systemie, openvpn-auth.sh - Login dla OpenVPN
Tu moje skrypty na GitHub