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.

67 lines
1.1 KiB

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