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
linux_wiki:command_many_systems_part2_send_cmd [2016/12/09 16:53]
billdozor [Main Script File]
linux_wiki:command_many_systems_part2_send_cmd [2019/05/25 23:50] (current)
Line 37: Line 37:
 OTHER OPTIONS OTHER OPTIONS
 -h                    => Display usage. -h                    => Display usage.
 +-i                    => Interactive mode.(Default: Non-interactive)
 -p                    => Send commands in parallel. -p                    => Send commands in parallel.
 -w num                => Set the max workers for parallel mode (default:10) -w num                => Set the max workers for parallel mode (default:10)
Line 58: 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: 2016-12-09 +# Last Updated: 2016-12-20 
-# Recent Changes:-Added worker argument and return code for ssh commands; newline+# Recent Changes:-Added interactive mode (-i) 
 +#                -Added worker argument and return code for ssh commands; newline
 #                 after output; color to return code #                 after output; color to return code
-#                -Added verbose output argument option, default to no 
 #################################################################################### ####################################################################################
  
Line 106: Line 107:
   echo -e "\nOTHER OPTIONS"   echo -e "\nOTHER OPTIONS"
   echo -e "-h                    => Display usage."   echo -e "-h                    => Display usage."
 +  echo -e "-i                    => Interactive mode.(Default: Non-interactive)"
   echo -e "-p                    => Send commands in parallel."   echo -e "-p                    => Send commands in parallel."
   echo -e "-w num                => Set the max workers for parallel mode (default:${max_workers})"   echo -e "-w num                => Set the max workers for parallel mode (default:${max_workers})"
Line 122: 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 132: Line 137:
  
 ## Get command line arguments ## ## Get command line arguments ##
-while getopts "hs:g:ac:pw:v" 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 210: 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 309: Line 323:
     # Serial Execution     # Serial Execution
     for node in $(echo ${system_name}); do     for node in $(echo ${system_name}); do
-      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" + 
-      return_code=$(echo $?) +      # Non-interactive (default) 
-      case "${return_code}" in +      if [[ ${interactive_mode} != "yes" ]]; then 
-        0) # 0 return code - show green return code +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
-          echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" +        return_code=$(echo $?) 
-        ;; +        case "${return_code}" in 
-       1) # 1 return code  - show red return code +          0) # 0 return code - show green return code 
-          echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
-        ;; +          ;; 
-       *) # any other return code  - show yellow return code +          1) # 1 return code  - show red return code 
-          echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
-        ;; +          ;; 
-      esac+          *) # 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 356: Line 380:
     # Serial Execution     # Serial Execution
     for node in $(cat ${system_name}); do     for node in $(cat ${system_name}); do
-      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" + 
-      return_code=$(echo $?) +      # Non-interactive (default) 
-      case "${return_code}" in +      if [[ ${interactive_mode} != "yes" ]]; then 
-        0) # 0 return code - show green return code +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
-          echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" +        return_code=$(echo $?) 
-        ;; +        case "${return_code}" in 
-       1) # 1 return code  - show red return code +          0) # 0 return code - show green return code 
-          echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
-        ;; +          ;; 
-       *) # any other return code  - show yellow return code +          1) # 1 return code  - show red return code 
-          echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
-        ;; +          ;; 
-      esac+          *) # 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 410: 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
-      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" + 
-      return_code=$(echo $?) +      # Non-interactive (default) 
-      case "${return_code}" in +      if [[ ${interactive_mode} != "yes" ]]; then 
-        0) # 0 return code - show green return code +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
-          echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" +        return_code=$(echo $?) 
-        ;; +        case "${return_code}" in 
-       1) # 1 return code  - show red return code +          0) # 0 return code - show green return code 
-          echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
-        ;; +          ;; 
-       *) # any other return code  - show yellow return code +          1) # 1 return code  - show red return code 
-          echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
-        ;; +          ;; 
-      esac+          *) # 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 457: Line 501:
     # Serial Execution     # Serial Execution
     for node in $(${spacecmd_cmd} system_list); do      for node in $(${spacecmd_cmd} system_list); do 
-      output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" +      # Non-interactive (default) 
-      return_code=$(echo $?) +      if [[ ${interactive_mode} != "yes" ]]; then 
-      case "${return_code}" in +        output="$(ssh -qt -o ConnectTimeout=5 ${node} "${send_cmd}")" 
-        0) # 0 return code - show green return code +        return_code=$(echo $?) 
-          echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" +        case "${return_code}" in 
-        ;; +          0) # 0 return code - show green return code 
-       1) # 1 return code  - show red return code +            echo -e "-> ${node} (${color_green}retcode=${return_code}${color_end})\n${output}\n" 
-          echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" +          ;; 
-        ;; +          1) # 1 return code  - show red return code 
-       *) # any other return code  - show yellow return code +            echo -e "-> ${node} (${color_red}retcode=${return_code}${color_end})\n${output}\n" 
-          echo -e "-> ${node} (${color_yellow}retcode=${return_code}${color_end})\n${output}\n" +          ;; 
-        ;; +          *) # any other return code  - show yellow return code 
-      esac+            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 491: 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 500: 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 509: 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.1481320433.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)