Skip to content

Instantly share code, notes, and snippets.

@aminvakil
Last active February 4, 2022 13:18
Show Gist options
  • Save aminvakil/c5b09cf6f426d98b11f6fec58ada509e to your computer and use it in GitHub Desktop.
Save aminvakil/c5b09cf6f426d98b11f6fec58ada509e to your computer and use it in GitHub Desktop.
Copies file between two servers without ssh keys (scp syntax)
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
pv_key=${script_dir}/copy.key
pb_key=${script_dir}/copy.key.pub
src_host=${1%:*}
dst_host=${2%:*}
src_file="${1#*:}"
dst_file="${2#*:}"
random_id=$(openssl rand -hex 6)
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
ssh ${src_host} rm -f /tmp/copy.key
ssh ${dst_host} "sed -i '\#${random_id}#d' .ssh/authorized_keys"
rm -f ${pv_key}
rm -f ${pb_key}
}
ssh-keygen -t ed25519 -N "" -f ${pv_key} -q -C ${random_id}
ssh-copy-id -f -i ${pv_key} ${dst_host}
scp ${pv_key} ${src_host}:/tmp/copy.key
ssh ${src_host} scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /tmp/copy.key ${src_file} ${dst_host}:${dst_file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment