Skip to content

Commit

Permalink
fetch_command_output helper
Browse files Browse the repository at this point in the history
  • Loading branch information
qaxi committed Apr 23, 2024
1 parent 75cbe0a commit d74cfb9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 44 deletions.
47 changes: 47 additions & 0 deletions test/unit/fetch_command_output.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Config file for utility 'fetch_command_output.sh'
#
# This will help you to make bunch of command output files for napalm drivers unit tests
#
# There is not any IP, MAC or names obfuscation or anonymizations!
# Before using outputs as a unit test files it is recommendet to protect
# your privacy information yourself by editing output files.

# Required variables
# $INTERFACE - for detailed interface information
# $LLDPINTERFACE - for detailed LLDP information

# Define empty array of commands
COMMANDS=()

# This is example set of commands for napalm driver 's350' (Cisco SMB)

# system info
COMMANDS+=("show version")
COMMANDS+=("show system")
COMMANDS+=("show inventory")

# config commands
COMMANDS+=("show startup-config")
COMMANDS+=("show running-config")
COMMANDS+=("show running-config detailed")

# interface commands
COMMANDS+=("show interfaces status")
COMMANDS+=("show interfaces description")
COMMANDS+=("show interface switchport $INTERFACE")
COMMANDS+=("show ports jumbo-frame")

# ip commands
COMMANDS+=("show hosts")
COMMANDS+=("show ip interface")
COMMANDS+=("show ipv6 interface brief")
COMMANDS+=("show arp")

# lldp commands
COMMANDS+=("show lldp neighbors")
COMMANDS+=("show lldp local $LLDPINTERFACE")
COMMANDS+=("show lldp neighbors $LLDPINTERFACE")

# ntp commands
COMMANDS+=("show sntp status")

74 changes: 30 additions & 44 deletions test/unit/fetch_command_output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ DEF_METHOD="_send_command"
MDDIR="mocked_data"
# command output dir
CODIR="command_output"
# commands.in
CMDIN="./commands.in"
# config
CMDIN="./$(basename $0 .sh ).cfg"

GETOPTS=":hu:p:ft:m:i:l:v:d:"
function usage() {
MSG="usage: $(basename $0) [-h] [-u USERNAME] [-p PASSWORD] [-f]\n"
MSG+=" [-m METHOD] [-i INTERFACE] [-l LLDP_INTERFACE]\n"
MSG+=" -t TYPE -v VENDOR -d DEVICE\n"
MSG="usage: $(basename $0) [-h] -u USERNAME -p PASSWORD [-f]\n"
MSG+=" [-m METHOD] -i INTERFACE -l LLDP_INTERFACE\n"
MSG+=" -t TYPE -v VENDOR -d DEVICE [SINGLE_COMMAND]\n"
MSG+="\n"
MSG+=" This utility helps to get output of commands from device.\n"
MSG+=" Set of commands can be configured in '$CMDIN'\n"
Expand Down Expand Up @@ -102,70 +102,56 @@ while getopts $GETOPTS opt; do
esac
done

[ -z "$DEVUSERNAME" ] && usage "Parameter -u needed"
[ -z "$DEVPASSWORD" ] && usage "Parameter -p needed"
[ -z "$TYPE" ] && usage "Parameter -t needed"
[ -z "$VENDOR" ] && usage "Parameter -v needed"
[ -z "$DEVICE" ] && usage "Parameter -d needed"

### COMMANDS="
### show arp
### show startup-config
### show version
### show system
### show inventory
### show hosts
### show interface status
### show running-config
### show interfaces status
### show interfaces description
### show ports jumbo-frame
### show ip int
### show lldp neighbors
### show sntp status
### show running-config full
### show lldp local $LLDPINTERFACE
### show lldp neighbors $LLDPINTERFACE
### "
### COMMANDS="
### show inventory
### "

# read 'commands.in' files
# read commands from file
[ -f "$CMDIN" ] || usage "File '$CMDIN' does not exists."
[ -r "$CMDIN" ] || usage "Can not read file '$CMDIN'."

[ -z "$INTERFACE" ] && usage "Parameter -i needed"
[ -z "$LLDPINTERFACE" ] && usage "Parameter -l needed"

. "${CMDIN}"

# Get possible last arg and set it as only command
shift $(($OPTIND - 1))
[ -z "$1" ] || COMMANDS=("$1")

[ -z "$DEVUSERNAME" ] && usage "Parameter -u needed"
[ -z "$DEVPASSWORD" ] && usage "Parameter -p needed"
[ -z "$TYPE" ] && usage "Parameter -t needed"
[ -z "$VENDOR" ] && usage "Parameter -v needed"
[ -z "$DEVICE" ] && usage "Parameter -d needed"

# ensure directory structure exists
[ -d "$CODIR" ] || usage "Output directory '$CODIR' does not exist."
[ -w "$CODIR" ] || usage "Output directory '$CODIR' is not writeable."

mkdir -p "$CODIR/$TYPE" || usage "Can not make directory '$CODIR/$TYPE'"
[ -w "$CODIR/$TYPE" ] || usage "Output directory '$CODIR/$TYPE' is not writeable."


echo "########## Generate output for type $TYPE ##########"
# lets get commands outputs
OLDIFS="$IFS"
IFS=$'\t\n'
for CMD in ${COMMANDS[@]}
do
IFS="$OLDIFS"

CMDFILE=${CMD// /_}
CMDFILE=${CMDFILE//-/_}
CMDFILE=${CMDFILE/\\/_}
CMDFILE=${CMDFILE/./_}
CMDFILE=${CMDFILE////_}
CMDFILE="$CODIR/$TYPE/${CMDFILE}.txt"
CMDSTR=${CMD// /_}
CMDSTR=${CMDSTR//-/_}
CMDSTR=${CMDSTR/\\/_}
CMDSTR=${CMDSTR/./_}
CMDSTR=${CMDSTR////_}
CMDFILE="$CODIR/$TYPE/${CMDSTR}.txt"

# skip already fetched command outputs
if [ -f "$CMDFILE" -a "$FORCE" = "n" ]; then
echo "###### Skiping '$CMD' file '$CMDFILE' exists"
echo "###### Skiping '$CMD'. File '$CMDFILE' exists"
continue
fi

echo "###### Fetching '$CMD' to '$CMDFILE'"
echo -e "$(napalm --user "$DEVUSERNAME" --password "$DEVPASSWORD" --vendor "$VENDOR" "$DEVICE" call --method-kwargs "command='$CMD'" "$METHOD" | sed 's/^"//;s/"$//;s/\\"/"/g')" | tee "$CMDFILE"
echo "## Fetching '$CMD' to '$CMDSTR.txt'"
# --debug
echo -e "$(napalm --user "$DEVUSERNAME" --password "$DEVPASSWORD" --vendor "$VENDOR" "$DEVICE" call --method-kwargs "command='$CMD'" "$METHOD" | sed 's/^"//;s/"$//;s/\\"/"/g')" > "$CMDFILE"
done

0 comments on commit d74cfb9

Please sign in to comment.