Skip to content

Instantly share code, notes, and snippets.

@otterley
Created May 12, 2011 17:34
Show Gist options
  • Save otterley/969021 to your computer and use it in GitHub Desktop.
Save otterley/969021 to your computer and use it in GitHub Desktop.
ssh precopy bash script
# Those of you who frequently log in to different hosts and prefer your experience to be consistent may appreciate an "ssh precopy"
# function. The idea is that when you intend to ssh to a host, a set of files (usually dotfiles such as .profile, .gitconfig, etc.)
# that you designate is copied immediately before the actual remote login takes place.
# Simply include the function in your .profile on your "base station" (e.g., your laptop). Then edit $HOME/.briefcase; each line
# should contain a file (relative to $HOME) you want to copy.
# Store ssh(1)'s path.
__SSH="`type -path ssh 2>/dev/null`";
function ssh()
{
local skip_sync;
if ! type -f rsync 2>&1 >/dev/null; then
# we don't have rsync.
skip_sync=1;
fi
if [ ! -f "$HOME/.briefcase" ]; then
skip_sync=1;
fi
# skip ssh options to find hostname
while getopts ":1246AaCfgKkMNnqsTtVvXxYyb:c:D:e:F:i:L:l:m:O:o:p:R:S:w:" Option; do
if [ "$Option" = "l" ]; then
# don't sync if we're logging into a different user's account
skip_sync=1;
break;
fi
done
server=`eval echo "$"$OPTIND`
# reset $OPTIND so that subsequent invocations work properly
OPTIND=1;
if echo "$server" | grep "@"; then
# don't sync if we're logging into a different user's account
skip_sync=1;
fi
if [ -z "$skip_sync" ]; then
rsync -auv -e ssh --files-from="$HOME/.briefcase" "$HOME" "$server":
fi
$__SSH "$@";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment