Differences
This shows you the differences between two versions of the page.
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: | # Description: | ||
- | # Last Updated: | + | # Last Updated: 2016-12-20 |
- | # Recent Changes: | + | # Recent Changes: |
- | # -Added | + | # -Added |
- | # | + | # 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=" | ||
# Worker script | # Worker script | ||
- | worker_script=" | + | worker_script=" |
+ | |||
+ | ## Define colors ## | ||
+ | # End/reset color | ||
+ | color_end=' | ||
+ | |||
+ | # Colors | ||
+ | color_green=' | ||
+ | color_red=' | ||
+ | color_yellow=' | ||
##### End of Customize Variables ##### | ##### End of Customize Variables ##### | ||
Line 83: | Line 95: | ||
echo -e " | echo -e " | ||
echo -e " | echo -e " | ||
- | echo -e " | + | echo -e " |
echo -e " | echo -e " | ||
- | echo -e "-s system | + | echo -e "-s system |
- | echo -e "-s ' | + | echo -e "-s ' |
echo -e "-s filename | echo -e "-s filename | ||
echo -e "-g system-group | echo -e "-g system-group | ||
echo -e " | echo -e " | ||
+ | echo -e " | ||
+ | echo -e "-c ' | ||
echo -e " | echo -e " | ||
echo -e " | echo -e " | ||
- | echo -e "-c ' | + | echo -e "-i |
- | echo -e " | + | echo -e " |
+ | echo -e "-w num => Set the max workers | ||
+ | echo -e " | ||
echo -e " | echo -e " | ||
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=" | ||
+ | |||
# Do not send to all systems by default | # Do not send to all systems by default | ||
all_systems=" | all_systems=" | ||
Line 112: | Line 131: | ||
# Send commands in serial by default | # Send commands in serial by default | ||
parallel_cmds=" | parallel_cmds=" | ||
+ | |||
+ | # Verbose to no by default | ||
+ | verbose=" | ||
## 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 " | case " | ||
h) # -h (help) argument | h) # -h (help) argument | ||
show_usage | show_usage | ||
exit 0 | exit 0 | ||
+ | ;; | ||
+ | i) # -i (interactive) argument | ||
+ | interactive_mode=" | ||
;; | ;; | ||
s) # -s system | s) # -s system | ||
Line 142: | Line 167: | ||
p) # send commands in parallel | p) # send commands in parallel | ||
parallel_cmds=" | parallel_cmds=" | ||
+ | ;; | ||
+ | w) # max workers in parallel | ||
+ | max_workers=" | ||
+ | ;; | ||
+ | v) # verbose output | ||
+ | verbose=" | ||
;; | ;; | ||
*) # 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} == " | ||
+ | echo -e " | ||
+ | interactive_mode=" | ||
fi | fi | ||
Line 214: | Line 251: | ||
# | # | ||
- | echo -e " | + | if [[ ${verbose} == " |
- | echo -e "#### | + | |
- | 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} == " | + | if [[ ${cmd_type} == " |
- | echo -e "Send command to these system(s) from file(${system_name}): | + | 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 | 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} == " | + | |
- | echo -e "Send Mode: Parallel with workers" | + | echo -e "Send Mode: Parallel with (${max_workers}) |
- | else | + | else |
- | echo -e "Send Mode: Serial" | + | echo -e "Send Mode: Serial" |
- | fi | + | fi |
- | echo -e " | + | |
- | echo -e "\n=> | + | echo -e " |
- | read run_script | + | read run_script |
- | if [[ ${run_script} != " | + | |
- | echo -e " | + | echo -e " |
- | 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 " | + | |
+ | | ||
+ | fi | ||
if [[ ${parallel_cmds} == " | if [[ ${parallel_cmds} == " | ||
Line 268: | Line 309: | ||
# Start a new worker in the background | # Start a new worker in the background | ||
+ | if [[ ${verbose} == " | ||
+ | echo "-> Working on ${node}..." | ||
+ | fi | ||
(${worker_script} ${node} " | (${worker_script} ${node} " | ||
Line 278: | Line 322: | ||
# Serial Execution | # Serial Execution | ||
for node in $(echo ${system_name}); | for node in $(echo ${system_name}); | ||
- | | + | |
- | ssh -qt -o ConnectTimeout=5 ${node} " | + | # Non-interactive (default) |
+ | if [[ ${interactive_mode} != " | ||
+ | output=" | ||
+ | return_code=$(echo $?) | ||
+ | case " | ||
+ | 0) # 0 return code - show green return code | ||
+ | echo -e "-> ${node} | ||
+ | ;; | ||
+ | 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 | ||
+ | |||
+ | | ||
+ | # Interactive mode set | ||
+ | echo -e "-> ${node}" | ||
+ | | ||
+ | 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 " | + | |
+ | | ||
+ | fi | ||
if [[ ${parallel_cmds} == " | if [[ ${parallel_cmds} == " | ||
Line 299: | Line 366: | ||
# Start a new worker in the background | # Start a new worker in the background | ||
+ | if [[ ${verbose} == " | ||
+ | echo "-> Working on ${node}..." | ||
+ | fi | ||
(${worker_script} ${node} " | (${worker_script} ${node} " | ||
Line 309: | Line 379: | ||
# Serial Execution | # Serial Execution | ||
for node in $(cat ${system_name}); | for node in $(cat ${system_name}); | ||
- | | + | |
- | ssh -qt -o ConnectTimeout=5 ${node} " | + | # Non-interactive (default) |
+ | if [[ ${interactive_mode} != " | ||
+ | output=" | ||
+ | return_code=$(echo $?) | ||
+ | case " | ||
+ | 0) # 0 return code - show green return code | ||
+ | echo -e "-> ${node} | ||
+ | ;; | ||
+ | 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 | ||
+ | |||
+ | | ||
+ | # Interactive mode set | ||
+ | echo -e "-> ${node}" | ||
+ | | ||
+ | echo | ||
+ | fi | ||
done | done | ||
fi | fi | ||
Line 317: | Line 408: | ||
group) | group) | ||
## Group of systems (Spacewalk Group) ## | ## Group of systems (Spacewalk Group) ## | ||
- | echo -e " | + | |
+ | | ||
+ | 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} == " | ||
+ | echo "-> Working on ${node}..." | ||
+ | fi | ||
(${worker_script} ${node} " | (${worker_script} ${node} " | ||
Line 347: | Line 443: | ||
# Serial Execution | # Serial Execution | ||
for node in $(${spacecmd_cmd} group_listsystems ${system_group}); | for node in $(${spacecmd_cmd} group_listsystems ${system_group}); | ||
- | | + | |
- | ssh -qt -o ConnectTimeout=5 ${node} " | + | # Non-interactive (default) |
+ | if [[ ${interactive_mode} != " | ||
+ | output=" | ||
+ | return_code=$(echo $?) | ||
+ | case " | ||
+ | 0) # 0 return code - show green return code | ||
+ | echo -e "-> ${node} | ||
+ | ;; | ||
+ | 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 | ||
+ | |||
+ | | ||
+ | # Interactive mode set | ||
+ | echo -e "-> ${node}" | ||
+ | | ||
+ | echo | ||
+ | fi | ||
done | done | ||
fi | fi | ||
Line 355: | Line 472: | ||
all) | all) | ||
## All Systems ## | ## All Systems ## | ||
- | echo -e " | + | |
+ | | ||
+ | fi | ||
if [[ ${parallel_cmds} == " | if [[ ${parallel_cmds} == " | ||
Line 368: | Line 487: | ||
# Start a new worker in the background | # Start a new worker in the background | ||
+ | if [[ ${verbose} == " | ||
+ | echo "-> Working on ${node}..." | ||
+ | fi | ||
(${worker_script} ${node} " | (${worker_script} ${node} " | ||
Line 378: | Line 500: | ||
# Serial Execution | # Serial Execution | ||
for node in $(${spacecmd_cmd} system_list); | for node in $(${spacecmd_cmd} system_list); | ||
- | echo " | + | |
- | ssh -qt -o ConnectTimeout=5 ${node} " | + | if [[ ${interactive_mode} != " |
+ | output=" | ||
+ | return_code=$(echo $?) | ||
+ | case " | ||
+ | 0) # 0 return code - show green return code | ||
+ | echo -e "-> ${node} | ||
+ | ;; | ||
+ | 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 | ||
+ | |||
+ | | ||
+ | # Interactive mode set | ||
+ | echo -e "-> ${node}" | ||
+ | | ||
+ | echo | ||
+ | fi | ||
done | done | ||
fi | fi | ||
Line 386: | Line 528: | ||
esac | esac | ||
- | echo -e " | + | if [[ ${verbose} == " |
- | echo -e "=- Send Command Completed. -=" | + | |
- | echo -e " | + | echo -e "=- Send Command Completed. -=" |
+ | echo -e " | ||
+ | fi | ||
</ | </ | ||
Line 399: | Line 543: | ||
# Name: worker_send-cmd.sh | # Name: worker_send-cmd.sh | ||
# Description: | # Description: | ||
- | # 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 ' | ||
+ | ###################### | ||
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=' | ||
+ | |||
+ | # Colors | ||
+ | color_green=' | ||
+ | color_red=' | ||
+ | color_yellow=' | ||
+ | ## 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=" | output=" | ||
+ | return_code=$(echo $?) | ||
- | # Show formatted | + | case " |
- | echo -e ">> Returned | + | 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 | ||
+ | | ||
+ | ;; | ||
+ | *) # any other return code - show yellow return code | ||
+ | echo -e " | ||
+ | ;; | ||
+ | esac | ||
</ | </ | ||
---- | ---- | ||