blob: 34833b4e8fe1bd470cbb48a7476f1da2f0fc09b6 [file] [log] [blame]
Luca Pretec97ad882018-05-04 11:22:14 -07001#!/usr/bin/env bash
Zack Williams249ac7f2018-05-18 13:34:14 -07002
3# Copyright 2018-present Open Networking Foundation
Luca Pretec97ad882018-05-04 11:22:14 -07004#
Zack Williams249ac7f2018-05-18 13:34:14 -07005# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy
7# of the License at:
Luca Pretec97ad882018-05-04 11:22:14 -07008#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
Zack Williams249ac7f2018-05-18 13:34:14 -070012# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
Luca Pretec97ad882018-05-04 11:22:14 -070016
Zack Williams249ac7f2018-05-18 13:34:14 -070017# copy-ssh-keys.sh - Adds ssh keys to nodes given as parameters to the script,
18# after removing them from the ~/.ssh/known_hosts file on the local system.
19#
20# This script should be run interactively as it will prompt for input, and only
21# invoked once, so as not to add multiple copies of the SSH key to the remote
22# system.
Luca Pretec97ad882018-05-04 11:22:14 -070023
Zack Williams249ac7f2018-05-18 13:34:14 -070024set -e -u -o pipefail
25
26REMOTE_SSH_USER="${REMOTE_SSH_USER:-cord}"
27SSH_PUBKEY_PATH="${SSH_PUBKEY_PATH:-${HOME}/.ssh/id_rsa.pub}"
28
29SSH_PUBKEY=$(cat "${SSH_PUBKEY_PATH}")
30
31for NODE in "$@";
Zack Williams11b2e5c2018-05-18 09:50:54 -070032do
Zack Williams249ac7f2018-05-18 13:34:14 -070033 # remove key for this node from local ~/.ssh/known_hosts file
Zack Williams11b2e5c2018-05-18 09:50:54 -070034 ssh-keygen -R "${NODE}"
Zack Williams11b2e5c2018-05-18 09:50:54 -070035
Zack Williams249ac7f2018-05-18 13:34:14 -070036 # copy the ssh key to the remote system ~/.ssh/authorized_keys file
37 # shellcheck disable=SC2029
38 ssh "${REMOTE_SSH_USER}@${NODE}" "umask 0077 && mkdir -p ~/.ssh && echo \"${SSH_PUBKEY}\" >> ~/.ssh/authorized_keys"
39done