No Description
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.

sshget 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env bash
  2. declare -r esc=$'\033'
  3. declare -r c_reset="${esc}[0m"
  4. declare -r c_red="${esc}[31m"
  5. declare -r fifo='/tmp/sshget.fifo'
  6. declare -A domains=()
  7. declare -A paths=()
  8. declare -a files=()
  9. err() {
  10. printf "${c_red}%s${c_reset}\n" "$*" >&2
  11. }
  12. die() {
  13. exit 1
  14. }
  15. usage() {
  16. LESS=-FEXR less <<'HELP'
  17. sshget <user@host1:/path/to/search ...>
  18. HELP
  19. }
  20. has() {
  21. local verbose=0
  22. if [[ $1 = '-v' ]]; then
  23. verbose=1
  24. shift
  25. fi
  26. for c; do c="${c%% *}"
  27. if ! command -v "$c" &> /dev/null; then
  28. (( verbose > 0 )) && err "$c not found"
  29. return 1
  30. fi
  31. done
  32. }
  33. has -v fzf rsync || die
  34. cleanup() {
  35. [[ -e "$fifo" ]] && rm "$fifo"
  36. }
  37. trap cleanup SIGHUP SIGINT SIGTERM
  38. mkfifo "$fifo"
  39. if (( $# < 1 )); then
  40. usage
  41. die
  42. fi
  43. for a; do
  44. host="${a%:*}"
  45. path="${a##*:}"
  46. domains+=( ["$a"]="$host" )
  47. paths+=( ["$a"]="$path" )
  48. shift
  49. done
  50. for s in "${!domains[@]}"; do
  51. ssh "${domains[$s]}" "find ${paths[$s]}" | sed -r "s|^|${domains[$s]}:|" >> "$fifo" &
  52. done
  53. mapfile -t files < <(fzf -e --inline-info +s --multi --cycle --bind='Ctrl-A:toggle-all,`:jump' < "$fifo")
  54. if (( ${#files[@]} )); then
  55. rsync --protect-args -auvzP -e ssh "${files[@]}" .
  56. fi
  57. cleanup