From patchwork Wed Dec 30 22:06:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Leonardo_M=C3=B6rlein?= X-Patchwork-Id: 1421461 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=irrelefant.net Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=hgnFd1+s; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=kYOYcg7W; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4D5lmz2VwRz9sSf for ; Thu, 31 Dec 2020 09:09:07 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=1p33iOrG7IBXw9ElkesFTGk6+cv2kac2vjrHjiQK5Vo=; b=hgnFd1+sfZQZbO0cILxVGmKxs6 +ecSebHUiuWYHlHbxGF+t09ktgnUzIG/4TZd7lT6oudySgd6OY21pelqzxptVDOKPRkGzGvlbnTQ3 dBwOeqMEBnUmd7OZJGRUS1wMdb3xj60t+SaSiV17p9OF5UY2LfaWn/Rb4HPxk95JQQ5cK5F/9BP6q f2sgW5jUgYzeqZKdYxRGXCLT1cbW5kowCGDYcfNNHZYcrEBYjBKKgzuKJvJTBLgjC4N3P7tPb4McX gni5I4oE0AXhOFu4hMx/NWz76V3143MyQD2GrDOB+cm8xM4HI/9vr7ZrfjU3YCbRd3xNIVlOp/MSx v/2Sc+ug==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kujcP-0005Tt-67; Wed, 30 Dec 2020 22:06:57 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kujcM-0005Tk-CO for openwrt-devel@merlin.infradead.org; Wed, 30 Dec 2020 22:06:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:In-Reply-To:References; bh=A5O9HLWIAxtwmYM5YN5tHorkY4kvuVwAZeCvU0yyz9g=; b=kYOYcg7WbyF9+GwvBi1KFGgDp5 4J54o/6ZqE1CqVw+t2XOEtzYIt9vRAhrcTRS9hCG2G+tVPJvl5oFaGUOwzF1gk0vrpC6N7a6CYbgx wTZdAdf42tAgOLs9192G+TLLk+rIcfeBzaSw294QdOHc5eBVA2qhx9H2+k4Eg1wcVO41R6nFp52VU 86e+ulFKTWR3iUxXaCroDOaxGRyJU8o4XukMi04ai63/uHMTra/9WWx5RRwreWle5ku87AHUiid20 IvVtDrebCx53yjTxTSG9FmU064SHTwU9J0fS00h/W+G/pBLNW5vlg0AnICWWbL2B2ecvd3zHj8JUZ qNb8CLgA==; Received: from smtprelay04.ispgateway.de ([80.67.18.16]) by casper.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1kujcG-0006Td-OU for openwrt-devel@lists.openwrt.org; Wed, 30 Dec 2020 22:06:50 +0000 Received: from [81.3.6.94] (helo=orange.ffh.zone) by smtprelay04.ispgateway.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) (envelope-from ) id 1kujbE-0001sv-3p; Wed, 30 Dec 2020 23:05:44 +0100 From: =?utf-8?q?Leonardo_M=C3=B6rlein?= To: openwrt-devel@lists.openwrt.org Subject: [PATCH netifd] scripts: Add scripts/netifd-netinfo.sh Date: Wed, 30 Dec 2020 23:06:37 +0100 Message-Id: <20201230220637.525822-1-me@irrelefant.net> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-Df-Sender: bWVAaXJyZWxlZmFudC5uZXQ= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201230_220650_008659_5E7FF12C X-CRM114-Status: GOOD ( 11.09 ) X-Spam-Note: SpamAssassin invocation failed X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Leonardo_M=C3=B6rlein?= , Felix Fietkau Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This script aims to give users a quick insight into the current netifd state by calling the ubus api and displaying the results in a human readable format. ---snip--- $ netinfo INTERFACE L3_DEVICE UP AVAILABLE AUTOSTART DYNAMIC PENDING PROTO ====================================================================================================== client br-client x x x dhcpv6 wan br-wan x x x dhcp wan6 br-wan x x x dhcpv6 $ netinfo wan client INTERFACE L3_DEVICE UP AVAILABLE AUTOSTART DYNAMIC PENDING PROTO ====================================================================================================== client br-client x x x dhcpv6 wan br-wan x x x dhcp $ netinfo -d DEVICE UP CARRIER PRESENT EXTERNAL TYPE ============================================================================= bat0 x x x Network device br_client x x x bridge br_wan x x x bridge eth0 x x x Network device eth1 x x x Network device lo x x x Network device $ netinfo -d -e ^eth DEVICE UP CARRIER PRESENT EXTERNAL TYPE ============================================================================= eth0 x x x Network device eth1 x x x Network device $ netinfo -l client wan wan6 $ netinfo -h /sbin/netinfo [OPTIONS] [PATTERN] [PATTERN] [...] -h print help -i show interfaces (default) -d show devices instead -l list only [PATTERN] only show elements matching PATTERN (optional) -e use regex for PATTERN matching ---snap--- Signed-off-by: Leonardo Mörlein --- scripts/netifd-netinfo.sh | 143 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100755 scripts/netifd-netinfo.sh diff --git a/scripts/netifd-netinfo.sh b/scripts/netifd-netinfo.sh new file mode 100755 index 0000000..887443e --- /dev/null +++ b/scripts/netifd-netinfo.sh @@ -0,0 +1,143 @@ +#!/bin/sh + +. /usr/share/libubox/jshn.sh + + +help() { + echo "$0 [OPTIONS] [PATTERN] [PATTERN] [...]" + echo + echo " -h print help" + echo " -i show interfaces (default)" + echo " -d show devices instead" + echo " -l list only" + echo + echo " [PATTERN] only show elements matching PATTERN (optional)" + echo " -e use regex for PATTERN matching" + echo +} + +load_row() { + # load all variables with names "$@" from json attributes + for var in "$@"; do + if [ "$var" = "__key" ]; then + continue + fi + + if json_is_a "$var" boolean; then + json_get_var b "$var" + if [ "$b" -eq 0 ]; then + eval "$var=\" \"" + else + eval "$var=x" + fi + else + json_get_var "$var" "$var" + fi + done +} + +print_row() { + local args="" + for var in "$@"; do + args="$args \"\$$var\"" + done + + eval "printf \"\$FMT\" $args" +} + +print_header() { + upper () { + tr 'abcdefghijklmnopqrstuvxyz' 'ABCDEFGHIJKLMNOPQRSTUVXYZ' + } + + # shellcheck disable=SC2059 + printf "$FMT" "$@" | upper + + local nchars + # shellcheck disable=SC2059 + nchars="$(printf "$FMT" "$@" | wc -c)" + printf "%${nchars}s\n" |tr " " "=" +} + +print_table() { + local keys + json_get_keys keys + + for __key in $keys; do + json_select "$__key" + load_row "$@" + + local match_value + match_value="$(eval printf "%s" "\$$match_key")" + + if [ -z "$ARGS" ]; then + print_row "$@" + else + for arg in $ARGS; do + if [ "$USE_REGEX" -eq 0 ]; then + arg="^$arg\$" + fi + if echo "$match_value" | grep "$arg" > /dev/null; then + print_row "$@" + fi + done + fi + + json_select ".." + done +} + +USE_REGEX=0 +LIST_ONLY=0 +ACTION=interfaces +while getopts dehil opt +do + case "$opt" in + i) ACTION=interfaces;; + h) ACTION=help;; + d) ACTION=devices;; + l) LIST_ONLY=1;; + e) USE_REGEX=1;; + *) help; echo "ERROR: Invalid option '$1'!"; exit 1;; + esac + shift; +done + +ARGS="$*" + +case "$ACTION" in + help) + help + ;; + interfaces) + json_init + json_load "$(ubus call network.interface dump)" + json_select interface + + match_key=interface + + if [ "$LIST_ONLY" -eq 1 ]; then + FMT="%s\n" + print_table interface + else + FMT="%17s %17s %6s %10s %10s %8s %8s %-17s\n" + print_header interface l3_device up available autostart dynamic pending proto + print_table interface l3_device up available autostart dynamic pending proto + fi + ;; + devices) + json_init + json_load "$(ubus call network.device status)" + + match_key=__key + if [ "$LIST_ONLY" -eq 1 ]; then + FMT="%s\n" + print_table __key + else + FMT="%17s %6s %10s %10s %10s %-17s\n" + print_header device up carrier present external type + print_table __key up carrier present external type + fi + + ;; +esac