ssh-copy-id revision 180751
133965Sjdp#!/bin/sh
2218822Sdim
3218822Sdim# Shell script to install your identity.pub on a remote machine
433965Sjdp# Takes the remote machine name as an argument.
533965Sjdp# Obviously, the remote machine must accept password authentication,
633965Sjdp# or one of the other keys in your ssh-agent, for this to work.
733965Sjdp
833965SjdpID_FILE="${HOME}/.ssh/identity.pub"
933965Sjdp
1033965Sjdpif [ "-i" = "$1" ]; then
1133965Sjdp  shift
1233965Sjdp  # check if we have 2 parameters left, if so the first is the new ID file
1333965Sjdp  if [ -n "$2" ]; then
1433965Sjdp    if expr "$1" : ".*\.pub" > /dev/null ; then
1533965Sjdp      ID_FILE="$1"
1633965Sjdp    else
1733965Sjdp      ID_FILE="$1.pub"
1833965Sjdp    fi
1933965Sjdp    shift         # and this should leave $1 as the target name
2033965Sjdp  fi
21218822Sdimelse
22218822Sdim  if [ x$SSH_AUTH_SOCK != x ] ; then
2333965Sjdp    GET_ID="$GET_ID ssh-add -L"
2433965Sjdp  fi
2533965Sjdpfi
2633965Sjdp
2733965Sjdpif [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
2833965Sjdp  GET_ID="cat ${ID_FILE}"
2933965Sjdpfi
3033965Sjdp
3133965Sjdpif [ -z "`eval $GET_ID`" ]; then
3233965Sjdp  echo "$0: ERROR: No identities found" >&2
3333965Sjdp  exit 1
34130561Sobrienfi
3533965Sjdp
3633965Sjdpif [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
37218822Sdim  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
3833965Sjdp  exit 1
3933965Sjdpfi
40218822Sdim
4133965Sjdp{ eval "$GET_ID" ; } | ssh $1 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1
4233965Sjdp
4333965Sjdpcat <<EOF
4433965SjdpNow try logging into the machine, with "ssh '$1'", and check in:
4533965Sjdp
4633965Sjdp  .ssh/authorized_keys
4733965Sjdp
4833965Sjdpto make sure we haven't added extra keys that you weren't expecting.
4933965Sjdp
5033965SjdpEOF
5133965Sjdp