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 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 [2016/12/09 16:53]
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-09 
-# Recent Changes:-Added support for sending to multiple system names as an argument +# Recent Changes:-Added worker argument and return code for ssh commands; newline 
-               -Added support for sending to system names in a file +                after output; color to return code 
-#                -Added support for parallel processing +#                -Added verbose output argument option, default to no 
-################################################################################+####################################################################################
  
 ##### 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 "-p                    => Send commands in parallel." 
-  echo -e "-p                    => Send commands in parallel.(max workers=${max_workers})"+  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 112: Line 127:
 # 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 "hs:g:ac:pw:v" opt; do
   case "${opt}" in   case "${opt}" in
     h) # -h (help) argument     h) # -h (help) argument
Line 142: Line 160:
     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 214: Line 238:
 #=================== #===================
  
-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 281:
   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 296:
  
       # 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 309:
     # Serial Execution     # Serial Execution
     for node in $(echo ${system_name}); do     for node in $(echo ${system_name}); do
-      echo "->${node}" +      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")
-      ssh -qt -o ConnectTimeout=${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
     done     done
   fi   fi
Line 286: Line 328:
   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 343:
  
       # 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 356:
     # Serial Execution     # Serial Execution
     for node in $(cat ${system_name}); do     for node in $(cat ${system_name}); do
-      echo "->${node}" +      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")
-      ssh -qt -o ConnectTimeout=${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
     done     done
   fi   fi
Line 317: Line 375:
   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 397:
  
       # 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 410:
     # 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}" +      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")
-      ssh -qt -o ConnectTimeout=${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
     done     done
   fi   fi
Line 355: Line 429:
   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 444:
  
       # 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 457:
     # Serial Execution     # Serial Execution
     for node in $(${spacecmd_cmd} system_list); do      for node in $(${spacecmd_cmd} system_list); do 
-      echo "->${node}" +      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")
-      ssh -qt -o ConnectTimeout=${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
     done     done
   fi   fi
Line 386: Line 476:
 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>
  
  • linux_wiki/command_many_systems_part2_send_cmd.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)