From cc880e85147f59ead6a087089b3cea89cf864b96 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sat, 22 Jun 2024 20:02:36 -0600 Subject: [PATCH] use flags for check_nfs_shares --- check_nfs_shares.sh | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/check_nfs_shares.sh b/check_nfs_shares.sh index 9c0e019..d567144 100755 --- a/check_nfs_shares.sh +++ b/check_nfs_shares.sh @@ -1,40 +1,42 @@ #!/bin/bash -# Function to print help print_help() { - echo "Usage: $0 " + echo "Usage: $0 -h -s " echo "Checks if the specified NFS shares are exported on the given host." echo "" - echo " The IP address or hostname of the NFS server." - echo " A comma-separated list of NFS shares to check. For example: '/mnt/test,/mnt/export'" + echo "-h The IP address or hostname of the NFS server." + echo "-s A comma-separated list of NFS shares to check. For example: '/mnt/test,/mnt/export'" echo "" echo "You can list the exported shares on an NFS server using the command 'showmount -e '" echo "This script assumes that 'showmount' is installed on the system where it is run." exit 1 } -# Check if help is requested or the correct number of arguments is provided -if [ "$#" -ne 2 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then +if [ "$#" -ne 4 ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then print_help fi -# Assign arguments to variables -host=$1 -shares=$2 +while getopts "h:s:" opt; do + case ${opt} in + h ) + host=$OPTARG + ;; + s ) + shares=$OPTARG + ;; + \? ) + print_help + ;; + esac +done + share_count=0 - -# Convert comma-separated shares into an array -IFS=',' read -r -a share_array <<< "$shares" - -# Flag to track if all shares are exported all_ok=true -# Iterate over each share -for share in "${share_array[@]}"; do - # Use showmount to check if the share is exported - output=$(showmount -e $host | grep $share) +IFS=',' read -r -a share_array <<< "$shares" - # Check if the share is found in the output +for share in "${share_array[@]}"; do + output=$(showmount -e $host | grep $share) if [[ $output != *"$share"* ]]; then echo "CRITICAL - $share is not exported on $host" exit 2 @@ -42,7 +44,6 @@ for share in "${share_array[@]}"; do share_count=$((share_count+1)) done -# Print final status if $all_ok; then echo "OK - All specified shares are exported on $host | share_count=$share_count" fi