From a19385fe3ce01a1b74c0dfb9d575b14fd93ac8f3 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Thu, 22 Jun 2017 09:38:26 +0300 Subject: [PATCH] ssh helper --- ssh-to | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 ssh-to diff --git a/ssh-to b/ssh-to new file mode 100755 index 0000000..f5132b5 --- /dev/null +++ b/ssh-to @@ -0,0 +1,57 @@ +#!/bin/bash + +function helpExit() { + echo "SSH to Docker session." + echo " arg: container name " + echo " --help This help" + exit +} +function rsaMissing() { + echo "RSA keys missing. have you started a container here?" + exit 1 +} +function listContainers() { + docker ps -a +} +function isRunning() { + [[ $( docker inspect --format '{{ .State.Running }}' "$1" ) = true ]] +} +function containerRestart() { + docker restart "$1" +} +for (( i=1; i<=$#; i++ )); do + j=$(( $i + 1 )) + [[ "${!i}" = "--help" ]] && helpExit + [[ "${!i}" = "-h" ]] && helpExit +done +set -e +[[ -f .id_rsa.pub ]] || rsaMissing +[[ -f .id_rsa ]] || rsaMissing + +[[ -z "$1" ]] && { + listContainers + echo "Pass container name / id" + exit +} +cd $( dirname $( readlink -f $0 ) ) + +export USER=$( id -u -n ) + +nname=$1 + +isRunning $nname || containerRestart $nname +while :; do + sleep 3 + IP=$( docker inspect --format '{{ .NetworkSettings.IPAddress }}' $nname ) + [[ -z "$IP" ]] || break +done +echo $IP +while :; do + sleep 3 + ssh-keyscan $IP && break +done + +ssh -i .id_rsa -XY -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $USER@$IP + + +