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

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
linux_wiki:command_many_systems_part2_send_cmd [2016/12/09 16:53]
billdozor [Script Usage]
linux_wiki:command_many_systems_part2_send_cmd [2017/01/24 22:24]
billdozor [Main Script File]
Line 58: Line 58:
 # 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 69: Line 69:
  
 # 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 83: Line 95:
   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 107: Line 123:
  
 ## 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 112: Line 131:
 # 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 142: Line 167:
     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 186: Line 217:
   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 214: Line 251:
 #=================== #===================
  
-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 255: Line 294:
   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 268: Line 309:
  
       # 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 278: Line 322:
     # 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 286: Line 351:
   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 299: Line 366:
  
       # 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 309: Line 379:
     # 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 317: Line 408:
   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 337: Line 430:
  
       # 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 347: Line 443:
     # 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 355: Line 472:
   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 368: Line 487:
  
       # 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 378: Line 500:
     # 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 386: Line 528:
 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 399: Line 543:
 # 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 408: Line 554:
   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 417: Line 573:
  
 # 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.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)