From 0b6e4f80ffaa0a1095f1c5c9458226df0c045689 Mon Sep 17 00:00:00 2001 From: Andrew Herson Date: Thu, 25 Sep 2025 16:27:32 -0700 Subject: [PATCH] feat(scripts): created convenience shell scripts --- .../examples/scripts/check_config_status.sh | 14 ++++ .../examples/scripts/get_running_config.sh | 21 +++++ docs/recipes/examples/scripts/port_power.sh | 82 +++++++++++++++++++ .../examples/scripts/set_running_config.sh | 19 +++++ 4 files changed, 136 insertions(+) create mode 100755 docs/recipes/examples/scripts/check_config_status.sh create mode 100755 docs/recipes/examples/scripts/get_running_config.sh create mode 100755 docs/recipes/examples/scripts/port_power.sh create mode 100755 docs/recipes/examples/scripts/set_running_config.sh diff --git a/docs/recipes/examples/scripts/check_config_status.sh b/docs/recipes/examples/scripts/check_config_status.sh new file mode 100755 index 0000000..eedf8a7 --- /dev/null +++ b/docs/recipes/examples/scripts/check_config_status.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# Usage: ./check_config_status.sh + +if [[ $# -ne 0 ]]; then + echo "Usage: $0" + exit 1 +fi + +# Output file name +fname="request.jsonl" + +echo '{ "method": "icfg.status.copy.get", "params": [], "id": 99 }' > "$fname" + +gs-rpc post -f $fname diff --git a/docs/recipes/examples/scripts/get_running_config.sh b/docs/recipes/examples/scripts/get_running_config.sh new file mode 100755 index 0000000..367d966 --- /dev/null +++ b/docs/recipes/examples/scripts/get_running_config.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# Usage: ./get_running_config + +if [[ $# -ne 2 ]]; then + echo "Usage: $0 " + exit 1 +fi + +# Create JSON +TFTP_SERVER_IP=$1 + +# strip leading "/" from CONFIG if present +# CONFIG=${2#/} +# just the filename (file must be in the tftp dir +CONFIG=$(basename "$2") + +# Output file name +fname="request.jsonl" + +echo '{ "method": "icfg.control.copy.set", "params": [{ "Copy": true, "SourceConfigType": "runningConfig", "SourceConfigFile": "", "DestinationConfigType": "configFile", "DestinationConfigFile": "tftp://'"$TFTP_SERVER_IP"'/'"$CONFIG"'", "Merge": false }], "id": 1 }' > "$fname" +gs-rpc post -f $fname diff --git a/docs/recipes/examples/scripts/port_power.sh b/docs/recipes/examples/scripts/port_power.sh new file mode 100755 index 0000000..c0ac646 --- /dev/null +++ b/docs/recipes/examples/scripts/port_power.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# Usage: ./make_port_file.sh + +MANAGEMENT_PORT=29 + +if [[ $# -ne 2 ]]; then + echo "Usage: $0 " + exit 1 +fi + +port_num=$1 +power_mode=$2 + +# Validate input +if ! [[ "$port_num" =~ ^[0-9]+$ ]] || (( port_num < 1 || port_num > 29 )); then + echo "Error: port number must be between 1 and 29" + exit 1 +fi + +if [[ "$power_mode" != "0" && "$power_mode" != "1" ]]; then + echo "Error: power mode must be 0 or 1" + exit 1 +fi + +# List of ports +ports=( +"Gi 1/1" +"Gi 1/2" +"Gi 1/3" +"Gi 1/4" +"Gi 1/5" +"Gi 1/6" +"Gi 1/7" +"Gi 1/8" +"Gi 1/9" +"Gi 1/10" +"Gi 1/11" +"Gi 1/12" +"Gi 1/13" +"Gi 1/14" +"Gi 1/15" +"Gi 1/16" +"Gi 1/17" +"Gi 1/18" +"Gi 1/19" +"Gi 1/20" +"Gi 1/21" +"Gi 1/22" +"Gi 1/23" +"Gi 1/24" +"10G 1/1" +"10G 1/2" +"10G 1/3" +"10G 1/4" +"Gi 1/25" +) + +# Pick correct port (subtract 1 because arrays are 0-based) +port_name="${ports[$((port_num-1))]}" + +# Output file name +fname="request.jsonl" + +# Shutdown true if power_mode=1, false if 0 +if [[ "$power_mode" == "1" ]]; then + shutdown="false" +else + shutdown="true" +fi + +# Create JSON +jq -n --arg port "$port_name" --argjson shutdown "$shutdown" \ + '{method:"port.config.set", params:[$port, {Shutdown:$shutdown}], id:1}' \ + > "$fname" + +if [[ "$port_num" == $MANAGEMENT_PORT ]]; then + echo "Warning, do not modify the management port power mode" + echo "If you really want to turn of the management port, type:" + echo " gs-rpc post -f $fname" +else + gs-rpc post -f $fname +fi diff --git a/docs/recipes/examples/scripts/set_running_config.sh b/docs/recipes/examples/scripts/set_running_config.sh new file mode 100755 index 0000000..cc904bc --- /dev/null +++ b/docs/recipes/examples/scripts/set_running_config.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# Usage: ./set_running_config + +if [[ $# -ne 2 ]]; then + echo "Usage: $0 " + exit 1 +fi + +# Create JSON +TFTP_SERVER_IP=$1 + +# just the filename (file must be in the tftp dir) +CONFIG=$(basename "$2") + +# Output file name +fname="request.jsonl" + +echo '{ "method": "icfg.control.copy.set", "params": [{ "Copy": true, "DestinationConfigType": "runningConfig", "DestinationConfigFile": "", "SourceConfigType": "configFile", "SourceConfigFile": "tftp://'"$TFTP_SERVER_IP"'/'"$CONFIG"'", "Merge": false }], "id": 1 }' > "$fname" +gs-rpc post -f $fname