Skip to content

Instantly share code, notes, and snippets.

@kg6zjl
Last active January 11, 2019 19:20
Show Gist options
  • Select an option

  • Save kg6zjl/5d8600039410e85281167c7c78cd2e71 to your computer and use it in GitHub Desktop.

Select an option

Save kg6zjl/5d8600039410e85281167c7c78cd2e71 to your computer and use it in GitHub Desktop.
SSH into Rancher Kubernetes Node
#!/usr/bin/env bash
#usage: rancher_ssh.sh node:id
#example: rancher_ssh.sh c-5581v:m-ghv14
#requirements: jq, curl
TOKEN="bearer token"
RANCHER_URL="set or use env var including api version, example: `https://rancher.dev/v3`"
function rssh() {
mkdir -p $HOME/tmp/rancher
#get keys
/usr/bin/curl -s -L -o $HOME/tmp/rancher/rancher-keys.zip $RANCHER_URL/nodes/$1/nodeconfig -H "cookie: R_SESS=$TOKEN" --compressed
#unzip and set grab key dir
KEY_PATH=$(/usr/bin/unzip -o $HOME/tmp/rancher/rancher-keys.zip -d $HOME/tmp/rancher | grep id_rsa | grep -v '.pub' | awk '{ print $2 }')
#cleanup
rm -rf $HOME/tmp/rancher/rancher-keys.zip
#get node details
node=$(/usr/bin/curl -s -L $RANCHER_URL/nodes/$1 -H "cookie: R_SESS=$TOKEN")
PUB_IP=$(echo $node | jq -r '.annotations' | grep external-ip | awk '{ print $2 }' | tr -d '",')
RUSER=$(echo $node | jq -r '.sshUser')
#ssh to node
echo "connecting to $PUB_IP as $RUSER using key $KEY_PATH"
ssh -i $KEY_PATH $RUSER@$PUB_IP
}
rssh $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment