You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.2 KiB

7 years ago
#!/usr/bin/env bash
6 years ago
declare -r esc=$'\033'
declare -r c_reset="${esc}[0m"
declare -r c_red="${esc}[31m"
7 years ago
declare -r fifo='/tmp/sshget.fifo'
declare -A domains=()
declare -A paths=()
declare -a files=()
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
exit 1
}
6 years ago
usage() {
LESS=-FEXR less <<'HELP'
sshget <user@host1:/path/to/search ...>
HELP
}
has() {
6 years ago
local verbose=0
if [[ $1 = '-v' ]]; then
6 years ago
verbose=1
shift
fi
6 years ago
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
6 years ago
(( verbose > 0 )) && err "$c not found"
return 1
fi
done
}
has -v fzf rsync || die
7 years ago
cleanup() {
[[ -e "$fifo" ]] && rm "$fifo"
}
trap cleanup SIGHUP SIGINT SIGTERM
mkfifo "$fifo"
6 years ago
if (( $# < 1 )); then
usage
die
fi
6 years ago
for a; do
6 years ago
host="${a%:*}"
7 years ago
path="${a##*:}"
domains+=( ["$a"]="$host" )
paths+=( ["$a"]="$path" )
shift
done
for s in "${!domains[@]}"; do
6 years ago
ssh "${domains[$s]}" "find ${paths[$s]}" | sed -r "s|^|${domains[$s]}:|" >> "$fifo" &
7 years ago
done
5 years ago
mapfile -t files < <(fzf -e --inline-info +s --multi --cycle --bind='Ctrl-A:toggle-all,`:jump' < "$fifo")
7 years ago
if (( ${#files[@]} )); then
rsync --protect-args -auvzP -e ssh "${files[@]}" .
7 years ago
fi
cleanup