cli: replace show interfaces with new pretty printer

Replace the old "ip" output with data from the sysrepo operational
datastore. This data is piped through the new json-cfg-pretty script,
which formats the data in a nice human readable way.

Signed-off-by: Richard Alpe <richard@bit42.se>
This commit is contained in:
Richard Alpe
2023-08-23 14:45:38 +02:00
committed by Tobias Waldekranz
parent 2656f17f58
commit 62f0958b65
2 changed files with 109 additions and 10 deletions
+99
View File
@@ -0,0 +1,99 @@
#!/bin/bash
CYAN='\033[0;36m' # Used in headers
NC='\033[0m' # No Color
usage() {
printf "Please provide a valid base field as the first argument\n"
exit 1
}
if [ -z "$1" ]; then
usage
fi
json=$(cat)
show_ietf_interface() {
name="$1"
shift
iface=$(echo "$json" | jq --arg name "$name" \
'.["ietf-interfaces:interfaces"].interface[] | select(.name == $name)')
if [ -z "$iface" ]; then
printf "Error, interface named \"$name\" not found"
exit 1
fi
printf "%-20s : %s\n" "name" "$name"
printf "%-20s : %s\n" "interface-index" "$(echo "$iface" | jq -r '.["if-index"]')"
printf "%-20s : %s\n" "operational-status" "$(echo "$iface" | jq -r '.["oper-status"]')"
printf "%-20s : %s\n" "physical-address" "$(echo "$iface" | jq -r '.["phys-address"]')"
# MTU isn't set for loopback
mtu="$(echo "$iface" | jq -r '.["ietf-ip:ipv4"] | select(.mtu != null) | .mtu')"
if [ -n "$mtu" ]; then
printf "%-20s : %s\n" "mtu" "$mtu"
fi
printf "\n${CYAN}%-5s${NC}\n" "ipv4:"
addresses=$(echo "$iface" | jq -c --arg field "$field" '.["ietf-ip:ipv4"].address[]' 2>/dev/null)
for addr in $addresses; do
ip="$(echo "$addr" | jq -r '."ip"')"
prefix="$(echo "$addr" | jq -r '."prefix-length"')"
printf "%-5s %s/%s\n" "" "$ip" "$prefix"
done
printf "\n${CYAN}%-5s %5s\n${NC}" "in:" "octets"
printf "%-5s %s\n" "" "$(echo "$iface" | jq -r '.statistics["in-octets"]')"
printf "\n${CYAN}%-5s %5s${NC}\n" "out:" "octets"
printf "%-5s %s\n" "" "$(echo "$iface" | jq -r '.statistics["out-octets"]')"
}
show_ietf_interfaces() {
field="ietf-interfaces:interfaces"
if [ $# -ne 0 ]; then
show_ietf_interface "$1"
return
fi
interfaces=$(echo "$json" | jq -c --arg field "$field" '.[$field].interface[]')
printf "${CYAN}%-15s %-15s %-18s %s${NC}\n" "NAME" "STATE" "MAC" "IPv4"
for iface in $interfaces; do
name="$(echo "$iface" | jq -r '.["name"]')"
status="$(echo "$iface" | jq -r '.["oper-status"]')"
mac="$(echo "$iface" | jq -r '.["phys-address"]')"
printf "%-15s %-15s %-18s " "$name" "$status" "$mac"
addresses=$(echo "$iface" | jq -c --arg field "$field" '.["ietf-ip:ipv4"].address[]' 2>/dev/null)
first="true"
for addr in $addresses; do
ip="$(echo "$addr" | jq -r '."ip"')"
prefix="$(echo "$addr" | jq -r '."prefix-length"')"
if [ $first = "true" ]; then
printf "%s/%s\n" "$ip" "$prefix"
first="false"
else
printf "%-50s %s/%s\n" "" "$ip" "$prefix"
fi
done
if [ $first = "true" ]; then
printf "\n"
fi
done
}
field="$1"
shift
case "$field" in
"ietf-interfaces")
show_ietf_interfaces $*
;;
*)
usage
;;
esac
+10 -10
View File
@@ -231,18 +231,18 @@
</COMMAND>
<!-- https://www.cisco.com/c/en/us/td/docs/wireless/access_point/mob_exp/83/cmd-ref/me_cr_book/me_ports_and_interfaces_cli.html -->
<COMMAND name="interfaces" help="Show link layer interfaces">
<ACTION sym="script">ip -color link</ACTION>
<SWITCH name="optional" min="0">
<COMMAND name="brief" help="Simplified (human-readable) output" mode="switch">
<ACTION sym="script">ip -color -br link</ACTION>
</COMMAND>
<COMMAND name="detailed" help="Detailed (full) output" mode="switch">
<ACTION sym="script">ip -color -d link</ACTION>
<COMMAND name="interfaces" help="Show interface info">
<SWITCH name="optional" min="0" max="1">
<COMMAND name="name" help="Show detailed info about NAME">
<PARAM name="name" ptype="/STRING" help="Hash algorithm.">
</PARAM>
</COMMAND>
</SWITCH>
<ACTION sym="script" interactive="false">
name=${KLISH_PARAM_name:-}
sysrepocfg -f json -X -d operational -m ietf-interfaces | \
/lib/infix/json-cfg-pretty "ietf-interfaces" $name
</ACTION>
</COMMAND>
<COMMAND name="ip" help="Show IP level status (Layer-3)">