linux_wiki:command_many_systems_part2_send_cmd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
linux_wiki:command_many_systems_part2_send_cmd [2016/11/28 23:02]
billdozor created
linux_wiki:command_many_systems_part2_send_cmd [2019/05/25 23:50] (current)
Line 23: Line 23:
  
 --Usage-- --Usage--
-send-cmd.sh [SYSTEM-OPTION-c 'command to send'+send-cmd.sh <SYSTEM-OPTION-c 'command to send' [OTHER OPTIONS]
  
 SYSTEM OPTIONS SYSTEM OPTIONS
--s system             => Single system name(unquoted). +-s system             => Single system hostname(unquoted). 
--s 'system1 system2'  => Multiple systems(quoted).+-s 'system1 system2'  => Multiple system hostnames(quoted).
 -s filename           => File with system name(s). -s filename           => File with system name(s).
 -g system-group       => Spacewalk system group. -g system-group       => Spacewalk system group.
 -a                    => All Spacewalk registered systems. -a                    => All Spacewalk registered systems.
 +
 +COMMAND SYNTAX
 +-c 'command to send'  => Send the quoted command.(Can be unquoted if only 1 word)
  
 OTHER OPTIONS OTHER OPTIONS
 -h                    => Display usage. -h                    => Display usage.
--c 'command to send'  => Send the quoted command+-i                    => Interactive mode.(Default: Non-interactive) 
--p                    => Send commands in parallel.(max workers=5)+-p                    => Send commands in parallel. 
 +-w num                => Set the max workers for parallel mode (default:10) 
 +-v                    => Verbose output.
  
 --Other Requirements-- --Other Requirements--
Line 54: Line 59:
 # Name: send-cmd.sh # Name: send-cmd.sh
 # Description: Execute a command(s) on the specified system(s) # Description: Execute a command(s) on the specified system(s)
-# Last Updated: 11/25/2016 +# Last Updated: 2016-12-20 
-# Recent Changes:-Added support for sending to multiple system names as an argument +# Recent Changes:-Added interactive mode (-i) 
-#                -Added support for sending to system names in a file +#                -Added worker argument and return code for ssh commands; newline 
-               -Added support for parallel processing +                after output; color to return code 
-################################################################################+####################################################################################
  
 ##### Customize These Variables ##### ##### Customize These Variables #####
Line 65: Line 70:
  
 # Max number of workers at a time # Max number of workers at a time
-max_workers=5+max_workers=10 
 + 
 +# Get base path 
 +base_path="$(echo ${0} | sed 's/send-cmd.sh//')"
  
 # Worker script # Worker script
-worker_script="/admin/scripts/worker_send-cmd.sh"+worker_script="${base_path}worker_send-cmd.sh" 
 + 
 +## Define colors ## 
 +# End/reset color 
 +color_end='\033[0m' 
 + 
 +# Colors 
 +color_green='\033[0;32m' 
 +color_red='\033[0;31m' 
 +color_yellow='\033[0;33m'
 ##### End of Customize Variables ##### ##### End of Customize Variables #####
  
Line 79: Line 96:
   echo -e "\nDescription: Execute a command on the specified system(s)."   echo -e "\nDescription: Execute a command on the specified system(s)."
   echo -e "\n--Usage--"   echo -e "\n--Usage--"
-  echo -e "send-cmd.sh [SYSTEM-OPTION-c 'command to send'"+  echo -e "send-cmd.sh <SYSTEM-OPTION-c 'command to send' [OTHER OPTIONS]"
   echo -e "\nSYSTEM OPTIONS"   echo -e "\nSYSTEM OPTIONS"
-  echo -e "-s system             => Single system name(unquoted)." +  echo -e "-s system             => Single system hostname(unquoted)." 
-  echo -e "-s 'system1 system2'  => Multiple systems(quoted)."+  echo -e "-s 'system1 system2'  => Multiple system hostnames(quoted)."
   echo -e "-s filename           => File with system name(s)."   echo -e "-s filename           => File with system name(s)."
   echo -e "-g system-group       => Spacewalk system group."   echo -e "-g system-group       => Spacewalk system group."
   echo -e "-a                    => All Spacewalk registered systems."   echo -e "-a                    => All Spacewalk registered systems."
 +  echo -e "\nCOMMAND SYNTAX"
 +  echo -e "-c 'command to send'  => Send the quoted command.(Can be unquoted if only 1 word)"
   echo -e "\nOTHER OPTIONS"   echo -e "\nOTHER OPTIONS"
   echo -e "-h                    => Display usage."   echo -e "-h                    => Display usage."
-  echo -e "-c 'command to send'  => Send the quoted command." +  echo -e "-i                    => Interactive mode.(Default: Non-interactive)
-  echo -e "-p                    => Send commands in parallel.(max workers=${max_workers})"+  echo -e "-p                    => Send commands in parallel.
 +  echo -e "-w num                => Set the max workers for parallel mode (default:${max_workers})
 +  echo -e "-v                    => Verbose output."
   echo -e "\n--Other Requirements--"   echo -e "\n--Other Requirements--"
   echo -e "-> Spacecmd and config file setup required.(-g or -a only)"   echo -e "-> Spacecmd and config file setup required.(-g or -a only)"
Line 103: Line 124:
  
 ## Default settings ## ## Default settings ##
 +# Non-interactive by default
 +interactive_mode="no"
 +
 # Do not send to all systems by default # Do not send to all systems by default
 all_systems="no" all_systems="no"
Line 108: Line 132:
 # Send commands in serial by default # Send commands in serial by default
 parallel_cmds="no" parallel_cmds="no"
 +
 +# Verbose to no by default
 +verbose="no"
  
 ## Get command line arguments ## ## Get command line arguments ##
-while getopts "hs:g:ac:p" opt; do+while getopts "his:g:ac:pw:v" opt; do
   case "${opt}" in   case "${opt}" in
     h) # -h (help) argument     h) # -h (help) argument
       show_usage       show_usage
       exit 0       exit 0
 +    ;;
 +    i) # -i (interactive) argument
 +      interactive_mode="yes"
     ;;     ;;
     s) # -s system     s) # -s system
Line 138: Line 168:
     p) # send commands in parallel     p) # send commands in parallel
       parallel_cmds="yes"       parallel_cmds="yes"
 +    ;;
 +    w) # max workers in parallel
 +      max_workers="${OPTARG}"
 +    ;;
 +    v) # verbose output
 +      verbose="yes"
     ;;     ;;
     *) # invalid argument     *) # invalid argument
Line 182: Line 218:
   show_usage   show_usage
   exit 1   exit 1
 +fi
 +
 +# Parallel Send and Interactive Mode set: Issue warning
 +if [[ ${parallel_cmds} == "yes" && ${interactive_mode} == "yes" ]]; then
 +  echo -e "\n>> WARNING: Interactive mode (-i) ignored due to parallel send mode (-p).\n"
 +  interactive_mode="no"
 fi fi
  
Line 210: Line 252:
 #=================== #===================
  
-echo -e "=============================" +if [[ ${verbose} == "yes" ]]; then 
-echo -e "####=== Send Command ====####" +  echo -e "=============================" 
-echo -e "============================="+  echo -e "####=== Send Command ====####" 
 +  echo -e "============================="
  
-echo -e "NOTE: Commands with spaces and multiple system names must be quoted.\n"+  echo -e "NOTE: Commands with spaces and multiple system names must be quoted.\n"
  
-if [[ ${system_name} ]]; then +  if [[ ${system_name} ]]; then 
-  if [[ ${cmd_type} == "file" ]]; then +    if [[ ${cmd_type} == "file" ]]; then 
-    echo -e "Send command to these system(s) from file(${system_name}): \n$(cat ${system_name})"+      echo -e "Send command to these system(s) from file(${system_name}): \n$(cat ${system_name})
 +    else 
 +      echo -e "Send command to system(s): ${system_name}" 
 +    fi 
 +  elif [[ ${system_group} ]]; then 
 +    echo -e "Send command to systems in this Spacewalk group: ${system_group}"
   else   else
-    echo -e "Send command to system(s): ${system_name}"+    echo -e "Send command to ALL systems."
   fi   fi
-elif [[ ${system_group} ]]; then 
-  echo -e "Send command to systems in this Spacewalk group: ${system_group}" 
-else 
-  echo -e "Send command to ALL systems." 
-fi 
  
-if [[ ${parallel_cmds} == "yes" ]]; then +  if [[ ${parallel_cmds} == "yes" ]]; then 
-  echo -e "Send Mode: Parallel with workers" +    echo -e "Send Mode: Parallel with (${max_workers}) workers" 
-else +  else 
-  echo -e "Send Mode: Serial" +    echo -e "Send Mode: Serial" 
-fi+  fi
  
-echo -e "Command to send: ${send_cmd}" +  echo -e "Command to send: ${send_cmd}" 
-echo -e "\n=>Continue?[y/n]:\c" +  echo -e "=>Continue?[y/n]:\c" 
-read run_script+  read run_script
  
-if [[ ${run_script} != "y" ]]; then +  if [[ ${run_script} != "y" ]]; then 
-  echo -e "\n>>Will not run the send command script. Exiting..." +    echo -e "\n>>Will not run the send command script. Exiting..." 
-  exit 1 +    exit 1 
-fi+  fi 
 +fi # end of verbose check
  
 # If we are using parallel commands, set the current number of workers # If we are using parallel commands, set the current number of workers
Line 251: Line 295:
   single)   single)
   ## Single system(s) argument ##   ## Single system(s) argument ##
-  echo -e "\n>> Sending command(s) to system(s)..."+  if [[ ${verbose} == "yes" ]]; then 
 +    echo -e "\n>> Sending command(s) to system(s)..." 
 +  fi
  
   if [[ ${parallel_cmds} == "yes" ]]; then   if [[ ${parallel_cmds} == "yes" ]]; then
Line 264: Line 310:
  
       # Start a new worker in the background       # Start a new worker in the background
 +      if [[ ${verbose} == "yes" ]]; then
 +        echo "-> Working on ${node}..."
 +      fi
       (${worker_script} ${node} "${send_cmd}") &       (${worker_script} ${node} "${send_cmd}") &
  
Line 274: Line 323:
     # Serial Execution     # Serial Execution
     for node in $(echo ${system_name}); do     for node in $(echo ${system_name}); do
-      echo "->${node}" + 
-      ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}"+      # Non-interactive (default) 
 +      if [[ ${interactive_mode} != "yes" ]]; then 
 +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
 +        return_code=$(echo $?) 
 +        case "${return_code}" in 
 +          0) # 0 return code - show green return code 
 +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          1) # 1 return code  - show red return code 
 +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          *) # any other return code  - show yellow return code 
 +            echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +        esac 
 + 
 +      else 
 +        # Interactive mode set 
 +        echo -e "-> ${node}" 
 +        ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}" 
 +        echo 
 +      fi
     done     done
   fi   fi
Line 282: Line 352:
   file)   file)
   ## File with one or more systems ##   ## File with one or more systems ##
-  echo -e "\n>> Sending command(s) to system(s) in file (${system_name})..."+  if [[ ${verbose} == "yes" ]]; then 
 +    echo -e "\n>> Sending command(s) to system(s) in file (${system_name})..." 
 +  fi
  
   if [[ ${parallel_cmds} == "yes" ]]; then   if [[ ${parallel_cmds} == "yes" ]]; then
Line 295: Line 367:
  
       # Start a new worker in the background       # Start a new worker in the background
 +      if [[ ${verbose} == "yes" ]]; then
 +        echo "-> Working on ${node}..."
 +      fi
       (${worker_script} ${node} "${send_cmd}") &       (${worker_script} ${node} "${send_cmd}") &
  
Line 305: Line 380:
     # Serial Execution     # Serial Execution
     for node in $(cat ${system_name}); do     for node in $(cat ${system_name}); do
-      echo "->${node}" + 
-      ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}"+      # Non-interactive (default) 
 +      if [[ ${interactive_mode} != "yes" ]]; then 
 +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
 +        return_code=$(echo $?) 
 +        case "${return_code}" in 
 +          0) # 0 return code - show green return code 
 +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          1) # 1 return code  - show red return code 
 +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          *) # any other return code  - show yellow return code 
 +            echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +        esac 
 + 
 +      else 
 +        # Interactive mode set 
 +        echo -e "-> ${node}" 
 +        ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}" 
 +        echo 
 +      fi
     done     done
   fi   fi
Line 313: Line 409:
   group)   group)
   ## Group of systems (Spacewalk Group) ##   ## Group of systems (Spacewalk Group) ##
-  echo -e "\n>> Sending command(s) to a group of systems (${system_group})..."+  if [[ ${verbose} == "yes" ]]; then 
 +    echo -e "\n>> Sending command(s) to a group of systems (${system_group})..." 
 +  fi
  
   # Check to see if the Spacewalk group exists; exit if it does not   # Check to see if the Spacewalk group exists; exit if it does not
Line 333: Line 431:
  
       # Start a new worker in the background       # Start a new worker in the background
 +      if [[ ${verbose} == "yes" ]]; then
 +        echo "-> Working on ${node}..."
 +      fi
       (${worker_script} ${node} "${send_cmd}") &       (${worker_script} ${node} "${send_cmd}") &
  
Line 343: Line 444:
     # Serial Execution     # Serial Execution
     for node in $(${spacecmd_cmd} group_listsystems ${system_group}); do     for node in $(${spacecmd_cmd} group_listsystems ${system_group}); do
-      echo "->${node}" + 
-      ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}"+      # Non-interactive (default) 
 +      if [[ ${interactive_mode} != "yes" ]]; then 
 +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
 +        return_code=$(echo $?) 
 +        case "${return_code}" in 
 +          0) # 0 return code - show green return code 
 +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          1) # 1 return code  - show red return code 
 +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          *) # any other return code  - show yellow return code 
 +            echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +        esac 
 + 
 +      else 
 +        # Interactive mode set 
 +        echo -e "-> ${node}" 
 +        ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}" 
 +        echo 
 +      fi
     done     done
   fi   fi
Line 351: Line 473:
   all)   all)
   ## All Systems ##   ## All Systems ##
-  echo -e "\n>> Sending command(s) to All systems..."+  if [[ ${verbose} == "yes" ]]; then 
 +    echo -e "\n>> Sending command(s) to All systems..." 
 +  fi
  
   if [[ ${parallel_cmds} == "yes" ]]; then   if [[ ${parallel_cmds} == "yes" ]]; then
Line 364: Line 488:
  
       # Start a new worker in the background       # Start a new worker in the background
 +      if [[ ${verbose} == "yes" ]]; then
 +        echo "-> Working on ${node}..."
 +      fi
       (${worker_script} ${node} "${send_cmd}") &       (${worker_script} ${node} "${send_cmd}") &
  
Line 374: Line 501:
     # Serial Execution     # Serial Execution
     for node in $(${spacecmd_cmd} system_list); do      for node in $(${spacecmd_cmd} system_list); do 
-      echo "->${node}" +      # Non-interactive (default) 
-      ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}"+      if [[ ${interactive_mode} != "yes" ]]; then 
 +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
 +        return_code=$(echo $?) 
 +        case "${return_code}" in 
 +          0) # 0 return code - show green return code 
 +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          1) # 1 return code  - show red return code 
 +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +          *) # any other return code  - show yellow return code 
 +            echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" 
 +          ;; 
 +        esac 
 + 
 +      else 
 +        # Interactive mode set 
 +        echo -e "-> ${node}" 
 +        ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}" 
 +        echo 
 +      fi
     done     done
   fi   fi
Line 382: Line 529:
 esac esac
  
-echo -e "\n=============================" +if [[ ${verbose} == "yes" ]]; then 
-echo -e "=- Send Command Completed. -=" +  echo -e "\n=============================" 
-echo -e "============================="+  echo -e "=- Send Command Completed. -=" 
 +  echo -e "=============================" 
 +fi
 </code> </code>
  
Line 395: Line 544:
 # Name: worker_send-cmd.sh # Name: worker_send-cmd.sh
 # Description: Worker script for the parent "send-cmd.sh" # Description: Worker script for the parent "send-cmd.sh"
-# Last Updated: 2016-11-25 +# Last Updated: 2016-12-09 
-# Recent Changes:-File rename +# Recent Changes:-Added return code to output; newline after output;  
-##############################################################+#                 color to return code output 
 +#                -Moved 'Working on...' output to parent script 
 +####################################################################################
  
 if [[ -z ${1} ]]; then if [[ -z ${1} ]]; then
Line 404: Line 555:
   exit 1   exit 1
 fi fi
 +
 +## Configure colors ##
 +# End/reset color
 +color_end='\033[0m'
 +
 +# Colors
 +color_green='\033[0;32m'
 +color_red='\033[0;31m'
 +color_yellow='\033[0;33m'
 +## End of configure colors ##
  
 # Set system name to the first argument # Set system name to the first argument
Line 413: Line 574:
  
 # Send command to system and capture output # Send command to system and capture output
-echo "-> Working on ${system_name}..." 
 output="$(ssh -qt -o ConnectTimeout=5 ${system_name} "${send_cmd}")" output="$(ssh -qt -o ConnectTimeout=5 ${system_name} "${send_cmd}")"
 +return_code=$(echo $?)
  
-Show formatted output +case "${return_code}" in 
-echo -e ">> Returned output from ${system_name}:\n${output}"+  0) 0 return code - show green return code 
 +    echo -e "-> ${system_name} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
 +  ;; 
 + 1) # 1 return code  - show red return code 
 +    echo -e "-${system_name} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
 +  ;; 
 + *) # any other return code  - show yellow return code 
 +    echo -e "-> ${system_name} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" 
 +  ;; 
 +esac
 </code> </code>
  
 ---- ----
  
  • linux_wiki/command_many_systems_part2_send_cmd.1480392120.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)