Skip to content

Instantly share code, notes, and snippets.

@knakayama
Created September 11, 2012 07:41
Show Gist options
  • Save knakayama/3696714 to your computer and use it in GitHub Desktop.
Save knakayama/3696714 to your computer and use it in GitHub Desktop.
parallelで何かする
#!/bin/bash
SSH_CONFIG_PATH="${HOME}/.ssh/config"
is_ssh_config_exist() {
if [[ -f "$SSH_CONFIG_PATH" ]]; then
return 0
else
return 1
fi
}
is_parallel_installed() {
if [[ -x "$(which parallel)" ]]; then
return 0
else
return 1
fi
}
echo_err() {
echo "ERROR: $1" >&2
}
create_host_name_file() {
local tmp_file_path=$(mktemp "/tmp/${0##*/}.XXXXXX") || { echo_err "Can't create tmp file"; exit 1; }
cat "$SSH_CONFIG_PATH" | sed 's/^ \+//g' | grep -vE '(^$|^#|\*)' | grep 'Host ' | cut -d' ' -f2 > "$tmp_file_path"
echo "$tmp_file_path"
}
_parallel() {
parallel -u ssh {} 'ip addr show' :::: "$1"
}
rm_tmp_file_on_exit() {
if [[ -f "$tmp_file_path" ]]; then
rm "$tmp_file_path"
fi
}
trap rm_tmp_file_on_exit EXIT
is_ssh_config_exist
if [[ "$?" -ne 0 ]]; then
echo_err "$SSH_CONFIG_PATH does not exist"
fi
is_parallel_installed
if [[ "$?" -ne 0 ]]; then
echo_err "$parallel not found"
fi
tmp_file_path=$(create_host_name_file)
_parallel "$tmp_file_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment