From e68f9dffef0a4d18926db5214843f7cc82b47eaa Mon Sep 17 00:00:00 2001 From: mostlytoast Date: Mon, 6 May 2024 18:20:46 -0500 Subject: [PATCH] implemented add to key chain for macos added append to .ssh/config --- generate-and-send-ssh-key.sh | 38 ++++++++++++++++++++++++++++--- pullrec/generate-and-send-ssh-key | 1 + 2 files changed, 36 insertions(+), 3 deletions(-) create mode 160000 pullrec/generate-and-send-ssh-key diff --git a/generate-and-send-ssh-key.sh b/generate-and-send-ssh-key.sh index 9ea9ece..9752d15 100755 --- a/generate-and-send-ssh-key.sh +++ b/generate-and-send-ssh-key.sh @@ -7,10 +7,10 @@ FILENAME=~/.ssh/id_test KEYTYPE=rsa HOST=host USER=${USER} - +CONFIG_FILE="/Users/thomassimon/.ssh/config" # Variable for config file path # use "-p " if the ssh-server is listening on a different port SSH_OPTS="-o PubkeyAuthentication=no" - +NICKNAME="" # # NO MORE CONFIG SETTING BELOW THIS LINE # @@ -21,6 +21,7 @@ function usage() { echo " -u (--user) , default: ${USER}" echo " -f (--file) , default: ${FILENAME}" echo " -h (--host) , default: ${HOST}" + echo " -n (--nick) , default: none" echo " -p (--port) , default: " echo " -k (--keysize) , default: ${KEYSIZE}" @@ -52,6 +53,10 @@ do HOST="$1" shift ;; + -n*|--nick) + NICKNAME="$1" + shift + ;; -p*|--port) SSH_OPTS="${SSH_OPTS} -p $1" shift @@ -141,7 +146,7 @@ else fi fi -RET=$? +in=$? if [ ${RET} -ne 0 ];then echo ssh-copy-id failed: ${RET} exit 1 @@ -155,6 +160,33 @@ if [ ${RET} -ne 0 ];then echo ssh-chmod failed: ${RET} exit 1 fi +if [[ "$OSTYPE" == "darwin"* ]]; then + echo "macOS detected. adding to keychain" + ssh-add "--apple-use-keychain" ${FILENAME} +fi + +RET=$? +if [ ${RET} -ne 0 ];then + echo ssh-add failed: ${RET} + exit 1 +fi +if [ ! -z "$VARIABLE" ]; then + TEXT="\nHost ${HOST}\n UseKeychain yes\n AddKeysToAgent yes\n IdentityFile ~/.ssh/${FILENAME}" + # Check if the text already exists in the file + if grep -qF "${TEXT}" "${CONFIG_FILE}"; then + echo "Text already exists in file." + else + # Append the text to the file + echo -e "${TEXT}" >> "${CONFIG_FILE}" + echo "Text appended to ${CONFIG_FILE}." + RET=$? + if [ ${RET} -ne 0 ];then + echo appending to ${CONFIG_FILE} failed: ${RET} + exit 1 + fi + fi +fi + # Cut out PubKeyAuth=no here as it should work without it now echo diff --git a/pullrec/generate-and-send-ssh-key b/pullrec/generate-and-send-ssh-key new file mode 160000 index 0000000..a6ee2f5 --- /dev/null +++ b/pullrec/generate-and-send-ssh-key @@ -0,0 +1 @@ +Subproject commit a6ee2f5d28c5f24df656ee456bc80b37a767592a