Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Created September 13, 2023 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/04b39532ab19eda9cd5653e527490a3d to your computer and use it in GitHub Desktop.
Save n8henrie/04b39532ab19eda9cd5653e527490a3d to your computer and use it in GitHub Desktop.
pbopy: copy over ssh to a MacOS host's clipboard
#!/bin/bash
set -Eeuf -o pipefail
readonly SSH_PORT=1234
readonly USERNAME=me
readonly SSH_KEY=/home/"${USERNAME}"/.ssh/my_key_id_rsa
readonly DEST=my_machine_name
main() {
# https://stackoverflow.com/questions/20046804/can-i-test-for-an-open-stdin-stream-from-within-a-bash-script
if [[ -t 0 ]]; then
# No stdin available
tmux save-buffer - | pbcopy
else
# stdin available
# https://stackoverflow.com/questions/12524308/bash-strip-trailing-linebreak-from-output
cat | sed --null-data '$ s/\n$//' | ssh -T -p "${SSH_PORT}" -4 -i "${SSH_KEY}" "${USERNAME}@$(tailscale ip -4 "${DEST}")"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment