Differences
This shows you the differences between two versions of the page.
linux_wiki:command_many_systems_part2_send_cmd [2017/01/24 22:25] billdozor [Script Usage] |
linux_wiki:command_many_systems_part2_send_cmd [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Command Many Systems Part2: Send Cmd ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | Script using techniques from [[linux_wiki: | ||
- | |||
- | **Checklist** | ||
- | * Distro(s): Any | ||
- | * Other: Use of [[linux_wiki: | ||
- | * Understanding of [[linux_wiki: | ||
- | |||
- | ---- | ||
- | |||
- | ====== Script Usage ====== | ||
- | |||
- | Help screen for send-cmd.sh: | ||
- | |||
- | ./ | ||
- | <code bash> | ||
- | ==== Send Command ==== | ||
- | |||
- | Description: | ||
- | |||
- | --Usage-- | ||
- | send-cmd.sh < | ||
- | |||
- | SYSTEM OPTIONS | ||
- | -s system | ||
- | -s ' | ||
- | -s filename | ||
- | -g system-group | ||
- | -a => All Spacewalk registered systems. | ||
- | |||
- | COMMAND SYNTAX | ||
- | -c ' | ||
- | |||
- | OTHER OPTIONS | ||
- | -h => Display usage. | ||
- | -i => Interactive mode.(Default: | ||
- | -p => Send commands in parallel. | ||
- | -w num => Set the max workers for parallel mode (default: | ||
- | -v => Verbose output. | ||
- | |||
- | --Other Requirements-- | ||
- | -> Spacecmd and config file setup required.(-g or -a only) | ||
- | -> Run as a user with system list privileges on the Spacewalk server.(-g or -a only) | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== The Scripts ====== | ||
- | |||
- | ===== Main Script File ===== | ||
- | |||
- | Main send-cmd.sh script file. | ||
- | |||
- | <code bash send-cmd.sh> | ||
- | #!/bin/bash | ||
- | # Name: send-cmd.sh | ||
- | # Description: | ||
- | # Last Updated: 2016-12-20 | ||
- | # Recent Changes: | ||
- | # -Added worker argument and return code for ssh commands; newline | ||
- | # after output; color to return code | ||
- | #################################################################################### | ||
- | |||
- | ##### Customize These Variables ##### | ||
- | # Spacecmd command and any default arguments | ||
- | spacecmd_cmd=" | ||
- | |||
- | # Max number of workers at a time | ||
- | max_workers=10 | ||
- | |||
- | # Get base path | ||
- | base_path=" | ||
- | |||
- | # Worker script | ||
- | worker_script=" | ||
- | |||
- | ## Define colors ## | ||
- | # End/reset color | ||
- | color_end=' | ||
- | |||
- | # Colors | ||
- | color_green=' | ||
- | color_red=' | ||
- | color_yellow=' | ||
- | ##### End of Customize Variables ##### | ||
- | |||
- | # | ||
- | # Functions; Main starts after | ||
- | # | ||
- | function show_usage | ||
- | { | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e "-s system | ||
- | echo -e "-s ' | ||
- | echo -e "-s filename | ||
- | echo -e "-g system-group | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e "-c ' | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e "-w num => Set the max workers for parallel mode (default: | ||
- | echo -e " | ||
- | echo -e " | ||
- | echo -e "-> Spacecmd and config file setup required.(-g or -a only)" | ||
- | echo -e "-> Run as a user with system list privileges on the Spacewalk server.(-g or -a only)" | ||
- | echo -e | ||
- | } | ||
- | |||
- | # | ||
- | # Get Script Arguments | ||
- | # | ||
- | # Reset POSIX variable in case it has been used previously in this shell | ||
- | OPTIND=1 | ||
- | |||
- | ## Default settings ## | ||
- | # Non-interactive by default | ||
- | interactive_mode=" | ||
- | |||
- | # Do not send to all systems by default | ||
- | all_systems=" | ||
- | |||
- | # Send commands in serial by default | ||
- | parallel_cmds=" | ||
- | |||
- | # Verbose to no by default | ||
- | verbose=" | ||
- | |||
- | ## Get command line arguments ## | ||
- | while getopts " | ||
- | case " | ||
- | h) # -h (help) argument | ||
- | show_usage | ||
- | exit 0 | ||
- | ;; | ||
- | i) # -i (interactive) argument | ||
- | interactive_mode=" | ||
- | ;; | ||
- | s) # -s system | ||
- | system_name=" | ||
- | # Determine if cmd type is single system(s) argument or filename | ||
- | if [[ -f ${system_name} ]]; then | ||
- | cmd_type=" | ||
- | else | ||
- | cmd_type=" | ||
- | fi | ||
- | ;; | ||
- | g) # -g system-group | ||
- | system_group=" | ||
- | cmd_type=" | ||
- | ;; | ||
- | a) # -a (all systems) | ||
- | all_systems=" | ||
- | cmd_type=" | ||
- | ;; | ||
- | c) # command to send | ||
- | send_cmd=" | ||
- | ;; | ||
- | p) # send commands in parallel | ||
- | parallel_cmds=" | ||
- | ;; | ||
- | w) # max workers in parallel | ||
- | max_workers=" | ||
- | ;; | ||
- | v) # verbose output | ||
- | verbose=" | ||
- | ;; | ||
- | *) # invalid argument | ||
- | show_usage | ||
- | exit 0 | ||
- | ;; | ||
- | esac | ||
- | done | ||
- | |||
- | ## Argument Sanity Checks ## | ||
- | # Scenario: No arguments set | ||
- | if [[ ${all_systems} == " | ||
- | if [[ -z ${system_name} && -z ${system_group} ]]; then | ||
- | echo -e " | ||
- | show_usage | ||
- | exit 1 | ||
- | fi | ||
- | fi | ||
- | |||
- | # Scenario: All systems AND single system(s) argument/ | ||
- | if [[ ${all_systems} == " | ||
- | echo -e " | ||
- | show_usage | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Scenario: All systems AND system group set | ||
- | if [[ ${all_systems} == " | ||
- | echo -e " | ||
- | show_usage | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Scenario: Single system(s) argument AND system group set | ||
- | if [[ ${system_name} && ${system_group} ]]; then | ||
- | echo -e " | ||
- | show_usage | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Scenario: No command set | ||
- | if [[ -z ${send_cmd} ]]; then | ||
- | echo -e " | ||
- | show_usage | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Parallel Send and Interactive Mode set: Issue warning | ||
- | if [[ ${parallel_cmds} == " | ||
- | echo -e " | ||
- | interactive_mode=" | ||
- | fi | ||
- | |||
- | ## Command Type: Single or Filename ## | ||
- | |||
- | # | ||
- | # Pre-checks: Ensure dependencies exist | ||
- | # | ||
- | |||
- | # Only check for Spacewalk dependencies if NOT sending to a single system(s) argument/ | ||
- | if [[ -z ${system_name} ]]; then | ||
- | # Check for Spacecmd | ||
- | which spacecmd &> /dev/null | ||
- | if [[ $? -ne 0 ]]; then | ||
- | echo " | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Check to see if a spacecmd config file exists | ||
- | if [[ ! -f ${HOME}/ | ||
- | echo -e " | ||
- | exit 1 | ||
- | fi | ||
- | fi | ||
- | |||
- | # | ||
- | # Main starts here | ||
- | # | ||
- | |||
- | if [[ ${verbose} == " | ||
- | echo -e " | ||
- | echo -e "#### | ||
- | echo -e " | ||
- | |||
- | echo -e "NOTE: Commands with spaces and multiple system names must be quoted.\n" | ||
- | |||
- | if [[ ${system_name} ]]; then | ||
- | if [[ ${cmd_type} == " | ||
- | echo -e "Send command to these system(s) from file(${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 | ||
- | echo -e "Send command to ALL systems." | ||
- | fi | ||
- | |||
- | if [[ ${parallel_cmds} == " | ||
- | echo -e "Send Mode: Parallel with (${max_workers}) workers" | ||
- | else | ||
- | echo -e "Send Mode: Serial" | ||
- | fi | ||
- | |||
- | echo -e " | ||
- | echo -e " | ||
- | read run_script | ||
- | |||
- | if [[ ${run_script} != " | ||
- | echo -e " | ||
- | exit 1 | ||
- | fi | ||
- | fi # end of verbose check | ||
- | |||
- | # If we are using parallel commands, set the current number of workers | ||
- | if [[ ${parallel_cmds} == " | ||
- | current_workers=0 | ||
- | fi | ||
- | |||
- | case ${cmd_type} in | ||
- | single) | ||
- | ## Single system(s) argument ## | ||
- | if [[ ${verbose} == " | ||
- | echo -e " | ||
- | fi | ||
- | |||
- | if [[ ${parallel_cmds} == " | ||
- | # Parallel Execution | ||
- | for node in $(echo ${system_name}); | ||
- | |||
- | # If the current number of workers equals the max, wait for them to complete, then reset to zero | ||
- | if [[ ${current_workers} -ge ${max_workers} ]]; then | ||
- | wait | ||
- | current_workers=0 | ||
- | fi | ||
- | |||
- | # Start a new worker in the background | ||
- | if [[ ${verbose} == " | ||
- | echo "-> Working on ${node}..." | ||
- | fi | ||
- | (${worker_script} ${node} " | ||
- | |||
- | # Increase the number of current workers | ||
- | current_workers=$(( ${current_workers} + 1 )) | ||
- | done | ||
- | # Wait for all remaining workers to complete | ||
- | wait | ||
- | else | ||
- | # Serial Execution | ||
- | for node in $(echo ${system_name}); | ||
- | |||
- | # Non-interactive (default) | ||
- | if [[ ${interactive_mode} != " | ||
- | output=" | ||
- | return_code=$(echo $?) | ||
- | case " | ||
- | 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} " | ||
- | echo | ||
- | fi | ||
- | done | ||
- | fi | ||
- | |||
- | ;; | ||
- | file) | ||
- | ## File with one or more systems ## | ||
- | if [[ ${verbose} == " | ||
- | echo -e " | ||
- | fi | ||
- | |||
- | if [[ ${parallel_cmds} == " | ||
- | # Parallel Execution | ||
- | for node in $(cat ${system_name}); | ||
- | |||
- | # If the current number of workers equals the max, wait for them to complete, then reset to zero | ||
- | if [[ ${current_workers} -ge ${max_workers} ]]; then | ||
- | wait | ||
- | current_workers=0 | ||
- | fi | ||
- | |||
- | # Start a new worker in the background | ||
- | if [[ ${verbose} == " | ||
- | echo "-> Working on ${node}..." | ||
- | fi | ||
- | (${worker_script} ${node} " | ||
- | |||
- | # Increase the number of current workers | ||
- | current_workers=$(( ${current_workers} + 1 )) | ||
- | done | ||
- | # Wait for all remaining workers to complete | ||
- | wait | ||
- | else | ||
- | # Serial Execution | ||
- | for node in $(cat ${system_name}); | ||
- | |||
- | # Non-interactive (default) | ||
- | if [[ ${interactive_mode} != " | ||
- | output=" | ||
- | return_code=$(echo $?) | ||
- | case " | ||
- | 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} " | ||
- | echo | ||
- | fi | ||
- | done | ||
- | fi | ||
- | |||
- | ;; | ||
- | group) | ||
- | ## Group of systems (Spacewalk Group) ## | ||
- | if [[ ${verbose} == " | ||
- | echo -e " | ||
- | fi | ||
- | |||
- | # Check to see if the Spacewalk group exists; exit if it does not | ||
- | ${spacecmd_cmd} group_list | grep ${system_group} > /dev/null | ||
- | if [[ $? -ne 0 ]]; then | ||
- | echo -e "-> ERROR! Could not find Spacewalk group: ${system_group}" | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | if [[ ${parallel_cmds} == " | ||
- | # Parallel Execution | ||
- | for node in $(${spacecmd_cmd} group_listsystems ${system_group}); | ||
- | |||
- | # If the current number of workers equals the max, wait for them to complete, then reset to zero | ||
- | if [[ ${current_workers} -ge ${max_workers} ]]; then | ||
- | wait | ||
- | current_workers=0 | ||
- | fi | ||
- | |||
- | # Start a new worker in the background | ||
- | if [[ ${verbose} == " | ||
- | echo "-> Working on ${node}..." | ||
- | fi | ||
- | (${worker_script} ${node} " | ||
- | |||
- | # Increase the number of current workers | ||
- | current_workers=$(( ${current_workers} + 1 )) | ||
- | done | ||
- | # Wait for all remaining workers to complete | ||
- | wait | ||
- | else | ||
- | # Serial Execution | ||
- | for node in $(${spacecmd_cmd} group_listsystems ${system_group}); | ||
- | |||
- | # Non-interactive (default) | ||
- | if [[ ${interactive_mode} != " | ||
- | output=" | ||
- | return_code=$(echo $?) | ||
- | case " | ||
- | 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} " | ||
- | echo | ||
- | fi | ||
- | done | ||
- | fi | ||
- | |||
- | ;; | ||
- | all) | ||
- | ## All Systems ## | ||
- | if [[ ${verbose} == " | ||
- | echo -e " | ||
- | fi | ||
- | |||
- | if [[ ${parallel_cmds} == " | ||
- | # Parallel Execution | ||
- | for node in $(${spacecmd_cmd} system_list); | ||
- | |||
- | # If the current number of workers equals the max, wait for them to complete, then reset to zero | ||
- | if [[ ${current_workers} -ge ${max_workers} ]]; then | ||
- | wait | ||
- | current_workers=0 | ||
- | fi | ||
- | |||
- | # Start a new worker in the background | ||
- | if [[ ${verbose} == " | ||
- | echo "-> Working on ${node}..." | ||
- | fi | ||
- | (${worker_script} ${node} " | ||
- | |||
- | # Increase the number of current workers by one | ||
- | current_workers=$(( ${current_workers} + 1 )) | ||
- | done | ||
- | # Wait for all remaining workers to complete | ||
- | wait | ||
- | else | ||
- | # Serial Execution | ||
- | for node in $(${spacecmd_cmd} system_list); | ||
- | # Non-interactive (default) | ||
- | if [[ ${interactive_mode} != " | ||
- | output=" | ||
- | return_code=$(echo $?) | ||
- | case " | ||
- | 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} " | ||
- | echo | ||
- | fi | ||
- | done | ||
- | fi | ||
- | |||
- | ;; | ||
- | esac | ||
- | |||
- | if [[ ${verbose} == " | ||
- | echo -e " | ||
- | echo -e "=- Send Command Completed. -=" | ||
- | echo -e " | ||
- | fi | ||
- | </ | ||
- | |||
- | ===== Worker Script File ===== | ||
- | |||
- | Worker script file used for parallel commands only. | ||
- | |||
- | <code bash worker_send-cmd.sh> | ||
- | #!/bin/bash | ||
- | # Name: worker_send-cmd.sh | ||
- | # Description: | ||
- | # Last Updated: 2016-12-09 | ||
- | # Recent Changes: | ||
- | # color to return code output | ||
- | # -Moved ' | ||
- | #################################################################################### | ||
- | |||
- | if [[ -z ${1} ]]; then | ||
- | echo -e " | ||
- | echo -e "For usage see: ./ | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | ## Configure colors ## | ||
- | # End/reset color | ||
- | color_end=' | ||
- | |||
- | # Colors | ||
- | color_green=' | ||
- | color_red=' | ||
- | color_yellow=' | ||
- | ## End of configure colors ## | ||
- | |||
- | # Set system name to the first argument | ||
- | system_name=" | ||
- | |||
- | # Shift arguments and set the command to send as the remaining arguments | ||
- | shift | ||
- | send_cmd=" | ||
- | |||
- | # Send command to system and capture output | ||
- | output=" | ||
- | return_code=$(echo $?) | ||
- | |||
- | case " | ||
- | 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 | ||
- | </ | ||
- | |||
- | ---- | ||