Skip to content

Instantly share code, notes, and snippets.

@patrick0057
Last active October 2, 2020 21:48
Show Gist options
  • Save patrick0057/407f01a0d9dd98e88876c8eedc657aa8 to your computer and use it in GitHub Desktop.
Save patrick0057/407f01a0d9dd98e88876c8eedc657aa8 to your computer and use it in GitHub Desktop.
Rancher 2.x lazy ssh

lssh function

Changelog

  • 06.03.2020 - added zsh function that works for WSL
  • 04.21.2020 - updated mac os x code to work with zsh and improved instructions.
  • 01.02.2020 - added Windows Subsystem for Linux and broke out each OS into its own section for easy copy and paste
  • 12.10.2019 - added support for Rancher 1.6 tar.gz files (requires gtar on mac)
  • 12.06.2019 - made command lazier by not requiring user to paste the IP.

Description

Quick bash function to make my life easier when sshing into Rancher nodes. Make sure to update your default web browser download directory by modifying line 2 of the script. For mac: brew install findutils

Code

Mac OS X

function lssh {
  PROSSH_DOWNLOAD_DIR="${HOME}/Downloads"
  PROSSH_FILE=$(gfind ${PROSSH_DOWNLOAD_DIR} -type f -size -500k -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
  PROSSH_USER=$1
  PROSSH_IP=$(pbpaste)
  if [ -z "${PROSSH_USER}" ]; then
    PROSSH_USER='ubuntu'
  fi
  [[ ${PROSSH_FILE} == *.zip ]] && unzip -p "${PROSSH_FILE}" "*id_rsa" > ${HOME}/.ssh/prossh
  [[ ${PROSSH_FILE} == *.tar.gz ]] && gtar -Oxf "${PROSSH_FILE}"  --wildcards "*id_rsa" > ${HOME}/.ssh/prossh

  chmod 600 ~/.ssh/prossh
  echo ${PROSSH_FILE} extracted, now sshing into box!
  ssh -i ${HOME}/.ssh/prossh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null ${PROSSH_USER}@${PROSSH_IP}
}

Linux

function lssh {
  PROSSH_DOWNLOAD_DIR='~/Downloads'
  PROSSH_FILE=$(find ${PROSSH_DOWNLOAD_DIR} -type f -size -500k -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
  PROSSH_USER=$1
  PROSSH_IP=$(xclip -o)
  if [ -z "${PROSSH_USER}" ]; then
    PROSSH_USER='ubuntu'
  fi
  [[ ${PROSSH_FILE} == *.zip ]] && unzip -p "${PROSSH_FILE}" *id_rsa > ~/.ssh/prossh
  [[ ${PROSSH_FILE} == *.tar.gz ]] && gtar -Oxf "${PROSSH_FILE}"  --wildcards "*id_rsa" > ~/.ssh/prossh

  chmod 600 ~/.ssh/prossh
  echo ${PROSSH_FILE} extracted, now sshing into box!
  ssh -i ~/.ssh/prossh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null ${PROSSH_USER}@${PROSSH_IP}
}

Windows Subsystem for Linux

function lssh {
  PROSSH_DOWNLOAD_DIR='/mnt/c/Users/<CHANGE THIS TO YOUR USERNAME>/Downloads/'
  PROSSH_FILE=$(find ${PROSSH_DOWNLOAD_DIR} -type f -size -500k -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
  PROSSH_USER=$1
  PROSSH_IP=$(powershell.exe -command "Get-Clipboard")
  if [ -z "${PROSSH_USER}" ]; then
    PROSSH_USER='ubuntu'
  fi
  [[ ${PROSSH_FILE} == *.zip ]] && unzip -p "${PROSSH_FILE}" *id_rsa > ~/.ssh/prossh
  [[ ${PROSSH_FILE} == *.tar.gz ]] && gtar -Oxf "${PROSSH_FILE}"  --wildcards "*id_rsa" > ~/.ssh/prossh

  chmod 600 ~/.ssh/prossh
  echo ${PROSSH_FILE} extracted, now sshing into box!
  ssh -i ~/.ssh/prossh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null ${PROSSH_USER}@${PROSSH_IP}
}

Windows Subsystem for Linux, zsh optimized

function lssh {
  PROSSH_DOWNLOAD_DIR='/mnt/c/Users/<CHANGE THIS TO YOUR USERNAME>/Downloads/'
  PROSSH_FILE=$(find ${PROSSH_DOWNLOAD_DIR} -type f -size -500k -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
  PROSSH_USER=$1
  PROSSH_IP=$(powershell.exe -command "Get-Clipboard")
  if [ -z "${PROSSH_USER}" ]; then
    PROSSH_USER='ubuntu'
  fi
  [[ ${PROSSH_FILE} == *.zip ]] && unzip -p "${PROSSH_FILE}" \*id_rsa > ~/.ssh/prossh
  [[ ${PROSSH_FILE} == *.tar.gz ]] && tar -Oxf "${PROSSH_FILE}"  --wildcards "*id_rsa" > ~/.ssh/prossh

  chmod 600 ~/.ssh/prossh
  echo ${PROSSH_FILE} extracted, now sshing into box!
  ssh -i ~/.ssh/prossh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -l ${PROSSH_USER//[[:space:]]/} "${PROSSH_IP//[[:space:]]/}"
}

Usage

  1. Copy and paste the function to your ~/.bash_profile or ~/.bashrc

  2. Logout and log back in or source your file for changes to take effect in your current terminal

  3. Download an ssh key for a node you want to log into, ensure that it goes to ~/Downloads and is the most recently created file

  4. Copy the IP of the node

  5. On the terminal

    lssh [user]
  6. This will log you into the node by extracting the most recently downloaded file's id_rsa and sshing to the IP in your clipboard and user you specify. If no user is specified then it will assume ubuntu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment