Skip to content

added -p for port option and added ability to have no space after -i #8

@charlesv123

Description

@charlesv123

added -p for port option and added ability to have no space after -i

#!/bin/sh

# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.

ID_FILE="${HOME}/.ssh/id_rsa.pub"

for arg in "$@"
do
    case "$arg" in
      -i*)    
        ID_FILE=`echo $arg | sed 's/-i//g'`
        LAST=idfile
        continue
            ;;
      -p*)    
        PORT=`echo $arg | sed 's/-p//g'`
        LAST=port
        continue
            ;;
    *)  
        if [ "$LAST" == "idfile" ] && [ 0 == `expr "$arg" : ^-`  ] ; then
          ID_FILE=$arg
        elif [ "$LAST" == "port" ] ; then
          PORT=$arg
        else
          REMOTE_SERVER=$arg
        fi
        LAST=""
    esac
done

#echo ID_FILE = $ID_FILE
#echo PORT = $PORT
#echo REMOTE_SERVER = $REMOTE_SERVER

if [ -n "$ID_FILE" ]; then
    if expr "$ID_FILE" : ".*\.pub" > /dev/null ; then
      ID_FILE="$ID_FILE"
    else
      ID_FILE="$ID_FILE.pub"
    fi
else
    if [ x$SSH_AUTH_SOCK != x ] ; then
     GET_ID="$GET_ID ssh-add -L | grep -vxF 'The agent has no identities.'"
    fi
fi

if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
  GET_ID="cat ${ID_FILE}"
fi

if [ -z "`eval $GET_ID`" ]; then
  echo "$0: ERROR: No identities found" >&2
  exit 1
fi

if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$REMOTE_SERVER" ]; then
  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
  exit 1
fi

if [ -n "$PORT" ] ; then
PPORT="-p$PORT"
fi

{ eval "$GET_ID" ; } | ssh $REMOTE_SERVER $PPORT "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys; test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys" || exit 1

cat <<EOF
Now try logging into the machine, with "ssh '$1'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

EOF

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions