added gce_ssh_keyscan.sh
94b2bcfa
Hari Sekhon
committed
1 changed file
gce_ssh_keyscan.sh
/gcp/gce_ssh_keyscan.sh+46
/gcp/gce_ssh_keyscan.sh
Add comment 1 Plus  #!/usr/bin/env bash
Add comment 2 Plus  # vim:ts=4:sts=4:sw=4:et
Add comment 3 Plus  #
Add comment 4 Plus  # Author: Hari Sekhon
Add comment 5 Plus  # Date: 2024-02-28 12:40:41 +0000 (Wed, 28 Feb 2024)
Add comment 6 Plus  #
Add comment 7 Plus  # https///github.com/HariSekhon/DevOps-Bash-tools
Add comment 8 Plus  #
Add comment 9 Plus  # License: see accompanying Hari Sekhon LICENSE file
Add comment 10 Plus  #
Add comment 11 Plus  # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
Add comment 12 Plus  #
Add comment 13 Plus  # https://www.linkedin.com/in/HariSekhon
Add comment 14 Plus  #
Add comment 15 Plus  
Add comment 16 Plus  set -euo pipefail
Add comment 17 Plus  [ -n "${DEBUG:-}" ] && set -x
Add comment 18 Plus  srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Add comment 19 Plus  
Add comment 20 Plus  # shellcheck disable=SC1090,SC1091
Add comment 21 Plus  . "$srcdir/lib/utils.sh"
Add comment 22 Plus  
Add comment 23 Plus  # shellcheck disable=SC2034,SC2154
Add comment 24 Plus  usage_description="
Add comment 25 Plus  SSH keyscans all the GCE VM hosts that match the given regex filter to add them to SSH known_hosts
Add comment 26 Plus  
Add comment 27 Plus  Useful to doing direct SSH to hosts and especially for Ansible to speed up not going through IAP which is slow
Add comment 28 Plus  and not having them prompt to accept the SSH keys
Add comment 29 Plus  "
Add comment 30 Plus  
Add comment 31 Plus  # used by usage() in lib/utils.sh
Add comment 32 Plus  # shellcheck disable=SC2034
Add comment 33 Plus  usage_args="[<regex_filter> <gcloud_cli_options>]"
Add comment 34 Plus  
Add comment 35 Plus  help_usage "$@"
Add comment 36 Plus  
Add comment 37 Plus  #max_args 1 "$@"
Add comment 38 Plus  
Add comment 39 Plus  ssh_known_hosts_file=~/.ssh/known_hosts
Add comment 40 Plus  
Add comment 41 Plus  "$srcdir/gce_host_ips.sh" "$@" |
Add comment 42 Plus  while read -r ip hostname; do
Add comment 43 Plus   ssh-keyscan "$ip" >> "$ssh_known_hosts_file"
Add comment 44 Plus   ssh-keyscan "$hostname" >> "$ssh_known_hosts_file"
Add comment 45 Plus  done
Add comment 46 Plus